脚本思路: 在从库上远程备份主库数据,再在从库上进行数据恢复,同时启动从库.
环境: CentOS6.5 已安装MySQL多实例,端口为3306 ,3307.
现通过自动安装主从脚本将两实例做主从复制。
#!/bin/bash . /etc/rc.d/init.d/functions if [ $# -ne 0 ];then echo "Usage:sh `basename $0`" exit 1 fi #MASTER VARIABLES MASTER_USER=root MASTER_PASS="terjoy2016" MASTER_PORT=3306 MASTER_IP="192.168.1.31" REP_USER=‘rep‘ REP_PWD=‘rep123‘ MASTER_DATA_PATH=/data/backup MASTER_STATUS_FILE=${MASTER_DATA_PATH}/mysqllogs_`date +%F`.log MASTER_DATA_FILE=${MASTER_DATA_PATH}/mysql_backup_`date +%F`.sql.gz MYSQL_DIR=/usr/local/mysql/bin MASTER_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 VARIABLES SLAVE_USER=root SLAVE_PASS="terjoy2016" SLAVE_PORT=3307 SLAVE_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 "主库创建复制用户" /bin/true $MASTER_MYSQL_CMD -e "flush tables with read lock;" 2> /dev/null [ $? -eq 0 ] && action "开始锁表" /bin/true || action "开始锁表" /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 "查看MASTER状态" /bin/true || action "查看MASTER状态" /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 "备份主库" /bin/true || action "备份主库" /bin/false || exit 1 $MASTER_MYSQL_CMD -e "unlock tables;" >/dev/null 2>&1 [ $? -eq 0 ] && action "表锁释放" /bin/true #cat $MASTER_STATUS_FILE ############################################################################### #recover [ -d ${MASTER_DATA_PATH} ] && cd ${MASTER_DATA_PATH} && rm -f mysql_backup_`date +%F`.sql gzip -d mysql_backup_`date +%F`.sql.gz [ $? -eq 0 ] && action "解压备份文件" /bin/true || action "解压备份文件" /bin/false || exit 1 $SLAVE_MYSQL_CMD 2> /dev/null < mysql_backup_`date +%F`.sql [ $? -eq 0 ] && action "恢复数据至从库" /bin/true || action "恢复数据至从库" /bin/false || exit 1 MASTER_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/null if [ $? -eq 0 ] ;then action "执行CHANGE MASTER TO命令" /bin/true else action "执行CHANGE MASTER TO命令" /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 1 fi $SLAVE_MYSQL_CMD -e "start slave;" 2> /dev/null [ $? -eq 0 ] && action "启动从库复制" /bin/true || action "启动从库复制" /bin/false || exit 1 $SLAVE_MYSQL_CMD -e "show slave status\G" 2> /dev/null |egrep "IO_Running|SQL_Running" >>$MASTER_STATUS_FILE MasterLogFile=`$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 "主从复制状态检测正常" /bin/true else action "主从复制状态检测正常" /bin/false $SLAVE_MYSQL_CMD -e "show slave status\G" 2> /dev/null >> $MASTER_STATUS_FILE $SENDMAIL < $MASTER_STATUS_FILE 2> /dev/null exit 1 fi $SENDMAIL < $MASTER_STATUS_FILE 2> /dev/null
执行命令:
./auto_master_slave.sh
安装完成。
检查结果:
从库状态正常!
时间: 2024-10-09 19:50:52