MySQL + Keepalived 双主热备搭建( 八 )

# MySQL账号密码mysql_user="root"mysql_pass="123456"# 配置更变日志change_log="/mysql/log/3308/state_change.log"# 主库B状态日志slave_status_log="/mysql/log/3308/slave_status_log.log"# MySQL连接字符串mysql_conn="mysql -u${mysql_user} -p${mysql_pass} -S /mysql/data/3308/mysql.sock"source ~/.bash_profileecho -e "`date "+%F%H:%M:%S"`-----keepalived change to MASTER-----" >> $change_logecho -e "`date "+%F%H:%M:%S"`----------" >> $slave_status_log$mysql_conn -e "show slave statusG;" >> $slave_status_logSlave_IO_Running=`$mysql_conn -e "show slave statusG;"|egrep -w "Slave_IO_Running|Slave_SQL_Running"|awk 'NR==1{print}' | awk '{print $2}'`Slave_SQL_Running=`$mysql_conn -e "show slave statusG;"|egrep -w "Slave_IO_Running|Slave_SQL_Running"|awk 'NR==2{print}' | awk '{print $2}'`Master_Log_File=`$mysql_conn -e "show slave statusG;" |egrep -w "Master_Log_File|Read_Master_Log_Pos|Exec_Master_Log_Pos"|awk 'NR==1{print}' | awk '{print $2}'`Read_Master_Log_Pos=`$mysql_conn -e "show slave statusG;" |egrep -w "Master_Log_File|Read_Master_Log_Pos|Exec_Master_Log_Pos"|awk 'NR==2{print}' | awk '{print $2}'`Exec_Master_Log_Pos=`$mysql_conn -e "show slave statusG;" |egrep -w "Master_Log_File|Read_Master_Log_Pos|Exec_Master_Log_Pos"|awk 'NR==3{print}' | awk '{print $2}'`action() {echo -e "`date "+%F%H:%M:%S"`-----set read_only = 0 on `hostname`-slave-----" >> $change_log$mysql_conn -e "set global read_only = 0;" 2>> $change_log$mysql_conn -e "stop slave;" 2>> $change_logecho "`hostname`-slave keepalived 转为 MASTER 状态,线上数据库切换至`hostname`-slave" >> $change_logecho -e "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@n" >> $change_log}if [ "$Slave_IO_Running" = "Yes" -a "$Slave_SQL_Running" = "Yes" ];thenif [ $Read_Master_Log_Pos = $Exec_Master_Log_Pos ];thenecho -e "`date "+%F%H:%M:%S"`-----Master_Log_File=$Master_Log_File . Exec_Master_Log_Pos($Exec_Master_Log_Pos) is equal Read_Master_Log_Pos($Read_Master_Log_Pos)" >> $change_logaction$mysql_conn -e "reset slave all;" 2>> $change_logelseecho -e "`date "+%F%H:%M:%S"`-----Master_Log_File=$Master_Log_File . Exec_Master_Log_Pos($Exec_Master_Log_Pos) is behind Read_Master_Log_Pos($Read_Master_Log_Pos), The wAIts time is more than 10s,now force change." >> $change_logsleep 10action$mysql_conn -e "reset slave all;" 2>> $change_logexit 0fiaction elseecho -e "`hostname`-slave's slave status is wrong,now force change. Master_Log_File=$Master_Log_File Read_Master_Log_Pos=$Read_Master_Log_PosExec_Master_Log_Pos=$Exec_Master_Log_Pos" >> $change_logactionfi
复制代码 
给脚本赋与执行权限:
 
chmod u+x /etc/keepalived/notify_master_mysql.sh
复制代码启动 Keepalived在主库 A 和主库 B 上分别启动 keepalived 。
 
systemctl start keepalived
复制代码 
查看 keepalived 状态 。
 
[root@mysql-master keepalived-2.2.4]# systemctl status keepalived.service ● keepalived.service - LVS and VRRP High Availability Monitor


推荐阅读