Scripting idea: Remotely backing up the main library data from the library, and then recovering the data from the library, starting from the library.
Environment: CentOS6.5 has installed MySQL multi-instance with Port 3306, 3307.
The two instance is copied from the master script by automatic installation.
#!/bin/bash. /etc/rc.d/init.d/functionsif [ $# -ne 0 ];then echo "usage:sh ' basename $0 '" exit 1fi#master variablesmaster_user= rootmaster_pass= "terjoy2016" master_port=3306master_ip= "192.168.1.31" rep_user= ' REP ' rep_pwd= ' rep123 ' master _data_path=/data/backupmaster_status_file=${master_data_path}/mysqllogs_ ' date +%f '. logMASTER_DATA_FILE=${ Master_data_path}/mysql_backup_ ' date +%f '. sql.gz mysql_dir=/usr/local/mysql/binmaster_mysql_cmd= "$ Mysql_dir/mysql -u$master_user -p$master_pass -h$master_ip -p$master_port "MASTER_MYSQL_ dump= "$MYSQL _dir/mysqldump -u$master_user -p$master_pass -h$master_ip -p$master_port -A -B -F --single-transaction --events "#SLAVE variablesslave_user= rootslave_pass= "terjoy2016" slave_port=3307slave_ip= "192.168.1.31" slave_mysql_cmd= "$MYSQL _dir/mysql -u$ Slave_user -p$slavE_pass -h$slave_ip -p$slave_port "to_mail=" [email protected] "sendmail=" mail -v -s "Mysql-slave-build-status" $TO _mail "[ ! -d $MASTER _data_path ] && mkdir -p $MASTER _data_path[ ' $MASTER _mysql_cmd -e "select user,host From mysql.user " 2> /dev/null |grep rep|wc -l ' -ne 1 ] && $MASTER _mysql_cmd -e "grant replication slave on *.* to " rep ' @ ' 192.168.1.% ' identified by ' rep123 '; 2> /dev/null[ $? -eq 0 ] && action "Master Library Create replication User" /bin/true $MASTER _mysql_cmd -e "flush tables with read lock; " 2> /dev/null[ $? -eq 0 ] && action "Start lock table" /bin/true | | action "Start lock table" /bin/false | | exit 1 echo "-----show master status result-----" > $MASTER _ status_file$master_mysql_cmd -e "show master status;" 2> /dev/null >> $MASTER _status_file[ $? -eq 0 ] & & action "View master status" /bin/true | | action "View master status" /bin/false | | exit 1#echo "${master_mysql_dump} | gzip > $MASTER _data_file" ${ master_mysql_dump} 2> /dev/null | gzip > $MASTER _data_file [ $? -eq 0 ] && action "Backing up the Main library" /bin/true | | action "Backing up the Main library" /bin/false | | exit 1$MASTER_MYSQL_CMD -e "unlock tables;" >/dev/null 2>&1[ $? -eq 0 ] && action " Table lock Release " /bin/true#cat $MASTER _status_file############################################################################### #recover [ -d ${MASTER_ data_path} ] && cd ${master_data_path} && rm -f Mysql_backup_ ' date +%f '. Sqlgzip -d mysql_backup_ ' date +%f '. sql.gz[ $? -eq 0 ] && action "Unzip backup file" /bin/true | | action "Unzip backup file" /bin/false | | exit 1$slave_mysql_cmd 2> /dev/null < mysql_backup_ ' Date +%F '. sql[ $? -eq 0 ] && action "Recover data to From library" /bin/true | | action "Recover data to From library" /bin/false | | exit 1master_log_file= ' tail -1 $MASTER _status_file|cut -f1 ' MASTER_LOG_POS= ' tail -1 $MASTER _status_file|cut -f2 ' #config slave$SLAVE_MYSQL_CMD -e "Change master to master_host= ' $MASTER _ip ', master_port= $MASTER _port, master_user= ' $REP _user ', master_password= ' $REP _pwd ', master_log_file= ' $MASTER _log_file ', master_log_pos= $MASTER _log_pos; " 2> /dev/nullif [ $? -eq 0 ] ;then action "Execute change master to command" /bin/true else action "Execute change master to command" /bin/false $SLAVE _mysql_cmd -e "Show slave status\g" 2> /dev/null >> $MASTER _status_file #echo $SENDMAIL < $MASTER _ Status_file " $SENDMAIL < $MASTER _status_file 2> /dev/null exit 1fi$SLAVE_MYSQL_CMD -e "start Slave; " 2> /dev/null[ $? -eq 0 ] && action "Start Copy from library" /bin/true | | action "Start Copy from library" /bin/false | | exit 1$SLAVE_MYSQL_CMD -e "Show slave status\g" 2> /dev/null |egrep "Io_running| Sql_running " >> $MASTER _status_filemasterlogfile= ' $SLAVE _mysql_cmd -e " show Slave status\g " 2> /dev/null |egrep -i " \<master_log_file\> "| awk ' {print $2} ' relaymasterlogfile= ' $SLAVE _mysql_cmd -e "Show slave status\g" 2> /dev/null |egrep -i "\<relay_master_log_file\>" | awk ' {print $2} ' readmasterlogpos= ' $SLAVE _mysql_cmd -e "Show slave status\g" 2> / dev/null |egrep -i "\<read_master_log_pos\>" | awk ' {print $2} ' execmasterlogpos= ' $SLAVE _mysql_cmd -e "Show slave status\g" 2> /dev/null |egrep -i "\< Exec_master_log_pos\> "| awk ' {print $2} ' rep_status= ' $SLAVE _mysql_cmd -e " show slave status\g " 2> /dev/null |egrep " slave_io_running| Slave_sql_running " |grep -c " Yes "' if [ $MasterLogFile == $ relaymasterlogfile ] && [ $ReadMasterLogPos == $ExecMasterLogPos ] && [ $REP _status -eq 2 ];then action "Master-slave replication status detection normal" /bin/true else action "master-slave replication status detection normal" /bin/false $SLAVE _mysql_cmd -e "Show slave status\g" 2> /dev/null > > $MASTER _status_file $SENDMAIL < $MASTER _status_file 2> /dev/null exit 1fi$SENDMAIL < $MASTER _status_file 2> /dev/null
Execute command:
./auto_master_slave.sh
650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M01/8B/30/wKioL1hGtH7hE2WJAABu0SPVIZ0314.png-wh_500x0-wm_3 -wmp_4-s_1140996623.png "title=" 1.png "alt=" Wkiol1hgth7he2wjaabu0spviz0314.png-wh_50 "/>
Installation is complete.
Check results:
650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M02/8B/30/wKioL1hGtW6x_n3wAACYzwzE8Lw771.png-wh_500x0-wm_3 -wmp_4-s_1277100374.png "style=" Float:none; "title=" 2.png "alt=" Wkiol1hgtw6x_n3waacyzwze8lw771.png-wh_50 "/>
650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M01/8B/30/wKioL1hGtW_yFmCJAAEHTgWwn1s088.png-wh_500x0-wm_3 -wmp_4-s_2881742710.png "style=" Float:none; "title=" 3.png "alt=" Wkiol1hgtw_yfmcjaaehtgwwn1s088.png-wh_50 "/>
From library status OK!
This article is from the "Xu Mingjiang blog" blog, make sure to keep this source http://dadaloveyou.blog.51cto.com/703960/1880147
One-click installation of MySQL master-slave copy shell script