# 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_profile
echo -e "`date "+%F%H:%M:%S"`-----keepalived change to MASTER-----" >> $change_log
echo -e "`date "+%F%H:%M:%S"`----------" >> $slave_status_log
$mysql_conn -e "show slave statusG;" >> $slave_status_log
Slave_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_log
echo "`hostname`-slave keepalived 转为 MASTER 状态,线上数据库切换至`hostname`-slave" >> $change_log
echo -e "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@n" >> $change_log
}
if [ "$Slave_IO_Running" = "Yes" -a "$Slave_SQL_Running" = "Yes" ];then
if [ $Read_Master_Log_Pos = $Exec_Master_Log_Pos ];then
echo -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_log
action
$mysql_conn -e "reset slave all;" 2>> $change_log
else
echo -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_log
sleep 10
action
$mysql_conn -e "reset slave all;" 2>> $change_log
exit 0
fi
action
else
echo -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_log
action
fi
复制代码
给脚本赋与执行权限:
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
推荐阅读
- 被记大过了,一个操作把MySQL主从复制整崩了……
- 大厂高频:讲一下MySQL主从复制
- MySQL面试常见问题解析:掌握这10个问题,事半功倍!
- MySQL如何支撑每秒百万QPS?
- 从MySQL到OBOracle:如何处理自增列?
- MySQL分库分表全攻略:从小白到大神的进阶指南!
- 一次MySQL主从同步异常,扒个底朝天都没排查出来……
- MySQL关联查询时,为什么建议小表驱动大表?这样做有什么好处
- MySQL 驱动中虚引用 GC 耗时优化与源码分析
- Mysql的存储引擎有哪些?