你的位置:首页 > 数据库

[数据库]如何利用脚本实现MySQL的快速部署以及一机多实例的部署


MySQL有三个版本:二进制,源码包,RPM。

下面讲讲二进制包的安装过程

下载地址:

http://dev.mysql.com/downloads/mysql/

选择Linux-Generic

 

我这里选择的是mysql-5.6.28-linux-glibc2.5-x86_64.tar.gz

解压后,里面有个文件INSTALL-BINARY,其实给出了二进制包的部署过程

shell> groupadd mysqlshell> useradd -r -g mysql -s /bin/false mysqlshell> cd /usr/localshell> tar zxvf /path/to/mysql-VERSION-OS.tar.gzshell> ln -s full-path-to-mysql-VERSION-OS mysqlshell> cd mysqlshell> chown -R mysql .shell> chgrp -R mysql .shell> scripts/mysql_install_db --user=mysqlshell> chown -R root .shell> chown -R mysql datashell> bin/mysqld_safe --user=mysql &# Next command is optionalshell> cp support-files/mysql.server /etc/init.d/mysql.server

 

相对于实际生产环境的部署,上面在初始化数据库的过程中少了一步-即指定配置文件,如果配置文件确认了,数据目录,日志目录都确认了,MySQL二进制版本的部署还是相当容易的一件事情。

下面写了一个脚本,基于后面提供的配置文件,执行格式如下:

sh 4.sh /root/mysql-5.6.28-linux-glibc2.5-x86_64.tar.gz /mysql3306 3306

其中 4.sh是脚本,/root/mysql-5.6.28-linux-glibc2.5-x86_64.tar.gz是二进制包的绝对路径,/mysql3306是basedir,3306是需设置的端口,

利用该脚本,只需要预先定义好配置文件,就可进行MySQL数据库的快速部署以及一台服务器上多个实例的部署。

#!/bin/bash#需传入三个参数,第一个是mysql二进制压缩包的路径(绝对路径),譬如/root/mysql-5.6.28-linux-glibc2.5-x86_64.tar.gz,#第二个是mysql的basedir,即需要创建在哪个目录下,第三个是设置的端口号filename=$1basedir=$2port=$3groupadd mysqluseradd -r -g mysql -s /bin/false mysqlcd /usr/localtar zxvf $filename#file是获取mysql二进制包的名称,譬如mysql-5.6.28-linux-glibc2.5-x86_64.tar.gz#dir是mysql压缩包的路径,不含包名本身,譬如/root,因为后续的配置文件my.cnf也是放到这个路径下file=`basename $filename`dir=`dirname $filename`#获取解压后的名字,即mysql-5.6.28-linux-glibc2.5-x86_64after_tar_file=${file:0:-7}#将二进制包改名为 mysql+端口号,这样也便于后续的区分mv $after_tar_file mysql"$port"cd mysql"$port"#将原始的配置文件(需和mysql压缩包放到同层目录下,在本例中是/root/my.cnf)copy到解压并改名后的mysql二进制目录下,修改为my+端口号.cnfcp $dir/my.cnf ./my"$port".cnfuser_cnf=my"$port".cnf#下面主要是将原始配置文件中的路径修改为自己设定的路径,即传入的第二个参数#整个的挑战在于传入的路径带有"/",在sed替换时会有问题,所有用了一个取巧的思路,即先将"/"替换为"|",进行sed替换,然后再将文件中的"|"修改回"/"basedir_new=${basedir/\//|}sed -i "s/\/project\/class2/$basedir_new/g" $user_cnfsed -i "s/|/\//g" $user_cnf#设置server_id,取当前的秒值server_id=`date +%s`sed -i /^server_id/s/.*/server_id="$server_id"/ $user_cnf#设置端口号sed -i /^port/s/.*/port="$port"/ $user_cnf#创建必要的目录并修改权限mkdir -p "$basedir"/mysql/run/ && mkdir -p "$basedir"/mysql/data && mkdir -p "$basedir"/mysql/share && \mkdir -p "$basedir"/mysql/log && mkdir -p "$basedir"/mysql/tmpchown -R mysql $basedirchgrp -R mysql $basedir#下面这个是非必要的,具体看后面的总结cp share/english/errmsg.sys "$basedir"/mysql/share/#初始化时--force也是非必要的,具体可见后面的总结scripts/mysql_install_db --user=mysql --defaults-file="$user_cnf" --forcebin/mysqld_safe --defaults-file="$user_cnf" --user=mysql &

 

下面给出了配置文件的一个参考,大家可根据实际情况进行相应的修改

[mysqld_safe]pid-file=/project/class2/mysql/run/mysqld.pid[mysql]port=3306prompt=\\u@\\d \\r:\\m:\\s>default-character-set=utf8no-auto-rehash[client]port=3306socket=/project/class2/mysql/run/mysql.sock[mysqld]#dirbasedir=/project/class2/mysqldatadir=/project/class2/mysql/datatmpdir=/tmplc_messages_dir=/project/class2/mysql/sharelog-error=/project/class2/mysql/log/alert.logslow_query_log_file=/project/class2/mysql/log/slow.loggeneral_log_file=/project/class2/mysql/log/general.logsocket=/project/class2/mysql/run/mysql.sock#innodbinnodb_data_home_dir=/project/class2/mysql/datainnodb_log_group_home_dir=/project/class2/mysql/datainnodb_data_file_path=ibdata1:2G;ibdata2:16M:autoextendinnodb_buffer_pool_size=10Ginnodb_buffer_pool_instances=4innodb_log_files_in_group=2innodb_log_file_size=1Ginnodb_log_buffer_size=200Minnodb_flush_log_at_trx_commit=1innodb_additional_mem_pool_size=20Minnodb_max_dirty_pages_pct=60innodb_io_capacity=1000innodb_thread_concurrency=16innodb_read_io_threads=8innodb_write_io_threads=8innodb_open_files=60000innodb_file_format=Barracudainnodb_file_per_table=1innodb_flush_method=O_DIRECTinnodb_change_buffering=insertsinnodb_adaptive_flushing=1innodb_old_blocks_time=1000innodb_stats_on_metadata=0innodb_read_ahead=0innodb_use_native_aio=0innodb_lock_wait_timeout=5innodb_rollback_on_timeout=0innodb_purge_threads=1innodb_strict_mode=1transaction-isolation=READ-COMMITTED#myisamkey_buffer=64Mmyisam_sort_buffer_size=64Mconcurrent_insert=2delayed_insert_timeout=300#replicationmaster-info-file=/project/class2/mysql/log/master.inforelay-log=/project/class2/mysql/log/relaylogrelay_log_info_file=/project/class2/mysql/log/relay-log.inforelay-log-index=/project/class2/mysql/log/mysqld-relay-bin.indexslave_load_tmpdir=/project/class2/mysql/tmpslave_type_conversions="ALL_NON_LOSSY"slave_net_timeout=4skip-slave-startsync_master_info=1000sync_relay_log_info=1000#binloglog-bin=/project/class2/mysql/log/mysql-binserver_id=2552763370binlog_cache_size=32Kmax_binlog_cache_size=2Gmax_binlog_size=500Mbinlog-format=ROWsync_binlog=1000log-slave-updates=1expire_logs_days=0#serverdefault-storage-engine=INNODBcharacter-set-server=utf8lower_case_table_names=1skip-external-lockingopen_files_limit=65536safe-user-createlocal-infile=1#sqlmod="STRICT_ALL_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE"log_slow_admin_statements=1log_warnings=1long_query_time=1slow_query_log=1general_log=0query_cache_type=0query_cache_limit=1Mquery_cache_min_res_unit=1Ktable_definition_cache=65536thread_stack=512Kthread_cache_size=256read_rnd_buffer_size=128Ksort_buffer_size=256Kjoin_buffer_size=128Kread_buffer_size=128Kport=3306skip-name-resolveskip-sslmax_connections=4500max_user_connections=4000max_connect_errors=65536max_allowed_packet=128Mconnect_timeout=8net_read_timeout=30net_write_timeout=60back_log=1024

 

总结:

在初始化的过程中,如果报以下错误:

FATAL ERROR: Neither host 'keepalived02' nor 'localhost' could be looked up with/mysql3306/mysql/bin/resolveipPlease configure the 'hostname' command to return a correcthostname.If you want to solve this at a later stage, restart this scriptwith the --force option

但是在bash终端上执行hostname命令又确实有值返回,可加--force参数,如下所示:

scripts/mysql_install_db --user=mysql --defaults-file="$user_cnf" --force

如果报以下错误:

[ERROR] Can't find messagefile '/mysql3306/mysql/share/errmsg.sys'

可将二进制版本中share/english/errmsg.sys文件COPY到/mysql3306/mysql/share/下。