Mysql有时需要大规模部署时,我们可以使用之前的数据库做为模板然后通过rpm打包工具传递到其它主机进行批量部署。
1、首先,下载rpmbuild
yum install rpm-build -y
它是Red Hat用于打RPM包的工具。我们在编辑打包程序时,可以看到/root/rpmbuild目录下有如下文件夹。
BUILD BUILDROOT RPMS SOURCES SPECS SRPMS
2、然后,下载源码包
http://cdn.mysql.com/archives/mysql-5.6/mysql-5.6.20.tar.gz
并将其复制到指定的目录/root/rpmbuild/SOURCES/mysql-5.6.20.tar.gz
3、安装编译MySQL需要的依赖包
yum -y install make gcc-c++ cmake bison-devel ncurses-devel
4、编写spec脚本
其实编写spec文件本质上是在写一个回调函数,rpmbuild根据spec的回调函数,生成一个Shell脚本,打包的过程就是执行Shell脚本的过程。我们在脚本中使用CFLAGS将源码包转变为二进制安装包。
5、生成rpm包
Tar zxvf mysql-5.6.20.tar.gz
Cd mysql_5.6.20
mkdir 5.6.20_mysql/rpm
cp mysql.spec 5.6.20_mysql/rpm
rpmbuild –bb ./rpm/mysql.spec 编译安装打包
执行结束后生成的rpm包就在RPMS/x86_64下了
定义配置模板,需要生成唯一的server_id
6、配置ftp
Yum install vsftp
Vi /etc/vsftpd/vsftpd.conf
userlist_enable=NO
将root从user_list中去掉
将root从/etc/vsftpdftpusers中去掉
Service vsftpd start
7、准备所需数据
需要的数据有rpm包、my.cnf和data文件。
Tar cf data.tar ibdata1 mysql/ performance_schema/ test/ 准备数据包
将mysql_5.6.20.tar.gz包、my.cnf包和data.tar包放在/var/ftp/pub目录供其它server下载。
8、配置自动化脚本
自动化脚本主要包括rpm包安装,ftp下载,mysql’用户和组添加、文件夹赋权、数据包解压、rpm包安装和服务配置。
9、拷贝到目标服务器执行
Scp auto_mysql.txt ip:/root/
Chmod 755 auto.sh
./auto.sh &
注脚本文件:
mysql.spec
Name: mysql
Version:5.6.20
Release: %(echo $RELEASE)%{?dist}
License: GPL
URL: http://downloads.mysql.com/archives/get/file/mysql-5.5.37.tar.gz
Group: applications/database
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root
BuildRequires: cmake
Packager: [email protected]
Autoreq: no
#Source: %{name}-%{version}.tar.gz
prefix: /usr/local/mysql
Summary: MySQL 5.6.20
%description
The MySQL(TM) software delivers a very fast, multi-threaded, multi-user,
and robust SQL (Structured Query Language) database server. MySQL Server
is intended for mission-critical, heavy-load production systems as well
as for embedding into mass-deployed software.
%define MYSQL_USER mysql
%define MYSQL_GROUP mysql
%define __os_install_post %{nil}
%build
cd $OLDPWD/../
CFLAGS="-O3 -g -fno-exceptions -static-libgcc -fno-omit-frame-pointer -fno-strict-aliasing"
CXX=g++
CXXFLAGS="-O3 -g -fno-exceptions -fno-rtti -static-libgcc -fno-omit-frame-pointer -fno-strict-aliasing"
export CFLAGS CXX CXXFLAGS
cmake . \
-DSYSCONFDIR:PATH=%{prefix} \
-DCMAKE_INSTALL_PREFIX:PATH=%{prefix} \
-DCMAKE_BUILD_TYPE:STRING=Release \
-DENABLE_PROFILING:BOOL=ON \
-DWITH_DEBUG:BOOL=OFF \
-DWITH_VALGRIND:BOOL=OFF \
-DENABLE_DEBUG_SYNC:BOOL=OFF \
-DWITH_EXTRA_CHARSETS:STRING=all \
-DWITH_SSL:STRING=bundled \
-DWITH_UNIT_TESTS:BOOL=OFF \
-DWITH_ZLIB:STRING=bundled \
-DWITH_PARTITION_STORAGE_ENGINE:BOOL=ON \
-DWITH_INNOBASE_STORAGE_ENGINE:BOOL=ON \
-DWITH_ARCHIVE_STORAGE_ENGINE:BOOL=ON \
-DWITH_BLACKHOLE_STORAGE_ENGINE:BOOL=ON \
-DWITH_PERFSCHEMA_STORAGE_ENGINE:BOOL=ON \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DENABLED_LOCAL_INFILE:BOOL=ON \
-DWITH_EMBEDDED_SERVER=0 \
-DINSTALL_LAYOUT:STRING=STANDALONE \
-DCOMMUNITY_BUILD:BOOL=ON \
-DMYSQL_SERVER_SUFFIX=‘-r5436‘;
make -j `cat /proc/cpuinfo | grep processor| wc -l`
%install
cd $OLDPWD/../
make DESTDIR=$RPM_BUILD_ROOT install
%clean
rm -rf $RPM_BUILD_ROOT
%files
%defattr(-, %{MYSQL_USER}, %{MYSQL_GROUP})
%attr(755, %{MYSQL_USER}, %{MYSQL_GROUP}) %{prefix}/*
%pre
%post
ln -s %{prefix}/lib %{prefix}/lib64
%preun
%changelog
my.cnf
[mysqld_safe]
pid-file=/usr/local/mysql/run/mysqld.pid
#malloc-lib=/usr/local/mysql/lib/libjemalloc.so
[mysql]
port=3306
prompt=\\[email protected]\\d \\r:\\m:\\s>
default-character-set=gbk
no-auto-rehash
[client]
port=3306
socket=/usr/local/mysql/run/mysql.sock
[mysqld]
#dir
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
tmpdir=/tmp
lc_messages_dir=/usr/local/mysql/share
log-error=/usr/local/mysql/log/alert.log
slow_query_log_file=/usr/local/mysql/log/slow.log
general_log_file=/usr/local/mysql/log/general.log
socket=/usr/local/mysql/run/mysql.sock
#innodb
innodb_data_home_dir=/usr/local/mysql/data
innodb_log_group_home_dir=/usr/local/mysql/data
innodb_data_file_path=ibdata1:2G;ibdata2:16M:autoextend
innodb_buffer_pool_size=10G
innodb_buffer_pool_instances=4
innodb_log_files_in_group=2
innodb_log_file_size=1G
innodb_log_buffer_size=200M
innodb_flush_log_at_trx_commit=1
innodb_additional_mem_pool_size=20M
innodb_max_dirty_pages_pct=60
innodb_io_capacity=1000
innodb_thread_concurrency=16
innodb_read_io_threads=8
innodb_write_io_threads=8
innodb_open_files=60000
innodb_file_format=Barracuda
innodb_file_per_table=1
innodb_flush_method=O_DIRECT
innodb_change_buffering=inserts
innodb_adaptive_flushing=1
innodb_old_blocks_time=1000
innodb_stats_on_metadata=0
innodb_read_ahead=0
innodb_use_native_aio=0
innodb_lock_wait_timeout=5
innodb_rollback_on_timeout=0
innodb_purge_threads=1
innodb_strict_mode=1
transaction-isolation=READ-COMMITTED
#myisam
key_buffer=64M
myisam_sort_buffer_size=64M
concurrent_insert=2
delayed_insert_timeout=300
#replication
master-info-file=/usr/local/mysql/log/master.info
relay-log=/usr/local/mysql/log/relaylog
relay_log_info_file=/usr/local/mysql/log/relay-log.info
relay-log-index=/usr/local/mysql/log/mysqld-relay-bin.index
slave_load_tmpdir=/usr/local/mysql/tmp
slave_type_conversions="ALL_NON_LOSSY"
slave_net_timeout=4
skip-slave-start
sync_master_info=1000
sync_relay_log_info=1000
#binlog
log-bin=/usr/local/mysql/log/mysql-bin
server_id=2552763370
binlog_cache_size=32K
max_binlog_cache_size=2G
max_binlog_size=500M
binlog-format=ROW
sync_binlog=1000
log-slave-updates=1
expire_logs_days=0
#server
default-storage-engine=INNODB
character-set-server=gbk
lower_case_table_names=1
skip-external-locking
open_files_limit=65536
safe-user-create
local-infile=1
#sqlmod="STRICT_ALL_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE"
performance_schema=0
log_slow_admin_statements=1
log_warnings=1
long_query_time=1
slow_query_log=1
general_log=0
query_cache_type=0
query_cache_limit=1M
query_cache_min_res_unit=1K
table_definition_cache=65536
#table_cache=65536
thread_stack=512K
thread_cache_size=256
read_rnd_buffer_size=128K
sort_buffer_size=256K
join_buffer_size=128K
read_buffer_size=128K
port=3306
skip-name-resolve
skip-ssl
max_connections=4500
max_user_connections=4000
max_connect_errors=65536
max_allowed_packet=128M
connect_timeout=8
net_read_timeout=30
net_write_timeout=60
back_log=1024
server_id=1921681201
# vi mysql_install.sh
#!/bin/bash
# mysql auto-install script
yum install -y cmake gcc g++ bison ncurses-devel zlib
#Step 2: Create group & user
group=$(more /etc/group|grep mysql)
if [ "$group" != "" ]; then
echo "group already created!"
else
groupadd mysql
fi
user=$(more /etc/passwd|grep mysql)
if [ "$user" != "" ]; then
echo "user already created!"
else
useradd -g mysql mysql
fi
#Step 3: Get source
ftp -n<<EOF
open 10.243.29.65
user root test!2013
binary
cd /var/ftp/pub
mget *
EOF
#Step 3: Install
unique_id=`ifconfig | grep ‘inet addr:‘| grep -v ‘127.0.0.1‘ | cut -d: -f2 | awk ‘{ print $1}‘ | sed -e ‘s/\.//g‘`
echo ‘server_id=‘$unique_id >> my.cnf
rpm -ivh mysql-5.6.20-.el6.x86_64.rpm
cp my.cnf /usr/local/mysql
chown -R mysql:mysql /usr/local/mysql
tar xvf data.tar -C /usr/local/mysql/data/
chown -R mysql:mysql /usr/local/mysql
mkdir -p /usr/local/mysql/log
chown -R mysql:mysql /usr/local/mysql/log
#step 4: Start MySQL
cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld
chmod 755 /etc/init.d/mysqld
chkconfig mysqld on
/etc/init.d/mysqld start