在MySQL主主集群中有机器关机等情况导致同步失效,自己写了一个shell脚本,以方便自己以后维护:
脚本内容如下:
#!/bin/bash
#设置主节点、从节点IP和mysql数据库管理员用户名密码信息 同步用户名密码信息
_MASTERHOST=192.168.1.207
_SLAVEHOST=192.168.1.208
_USER=root
_MASTERPASD=root
_SLAVEPASD=root
_TBUSER=ab
_TBPASD=123
#配置从到主的同步
mysql -h $_MASTERHOST -uroot -p$_MASTERPASD -e "flush tables with read lock;"
master_log_file=`mysql -u root -h $_MASTERHOST -p$_MASTERPASD -e "show master status;" | awk 'NR==2 {print $1}'`
master_log_position=`mysql -u root -h $_MASTERHOST -p$_MASTERPASD -e "show master status;" | awk 'NR==2 {print $2}'`
mysql -h $_MASTERHOST -uroot -p$_MASTERPASD -e "unlock tables;"
mysql -h $_SLAVEHOST -uroot -p$_SLAVEPASD -e "stop slave;"
mysql -h $_SLAVEHOST -uroot -p$_SLAVEPASD -e "CHANGE MASTER TO MASTER_HOST='$_MASTERHOST',MASTER_PORT=3306,MASTER_USER='$_TBUSER',MASTER_PASSWORD='$_TBPASD',MASTER_LOG_FILE='$master_log_file',MASTER_LOG_POS=$master_log_position;"
mysql -h $_SLAVEHOST -uroot -p$_SLAVEPASD -e "start slave;"
#配置主到从的同步
mysql -h $_SLAVEHOST -uroot -p$_SLAVEPASD -e "flush tables with read lock;"
slave_log_file=`mysql -u root -h $_SLAVEHOST -p$_SLAVEPASD -e "show master status;" | awk 'NR==2 {print $1}'`
slave_log_position=`mysql -u root -h $_SLAVEHOST -p$_SLAVEPASD -e "show master status;" | awk 'NR==2 {print $2}'`
mysql -h $_SLAVEHOST -uroot -p$_SLAVEPASD -e "unlock tables;"
mysql -h $_MASTERHOST -uroot -p$_MASTERPASD -e "stop slave;"
mysql -h $_MASTERHOST -uroot -p$_MASTERPASD -e "CHANGE MASTER TO MASTER_HOST='$_SLAVEHOST',MASTER_PORT=3306,MASTER_USER='$_TBUSER',MASTER_PASSWORD='$_TBPASD',MASTER_LOG_FILE='$slave_log_file',MASTER_LOG_POS=$slave_log_position;"
mysql -h $_MASTERHOST -uroot -p$_MASTERPASD -e "start slave;"
#检查
mysql -h $_MASTERHOST -uroot -p$_MASTERPASD -e "show slave status\G;" | awk '$0 ~/Host/ || $0 ~/State/ || $0 ~/Running/'
mysql -h $_SLAVEHOST -uroot -p$_SLAVEPASD -e "show slave status\G;" | awk '$0 ~/Host/ || $0 ~/State/ || $0 ~/Running/'
分享名称:mysql5.6主主集群自动同步脚本
文章URL:
http://jkwzsj.com/article/ppdhss.html