通过具体的查询语句来判断数据库服务的可用性,如果查询失败,则判断mysqld进程本身的状态,如果不正常,则直接停止当前节点的keepalived,将VIP转移到另外一个节点,如果正常,则等待30s,再次执行查询语句,还是失败,则将当前的master节点设置为read_only,并kill掉当前的客户端连接,然后停止当前的keepalived。
master2
[root@master2 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived vrrp_instance VI_1 { state BACKUP interface eno16777736 virtual_router_id 51 priority 90 advert_int 1 authentication { auth_type PASS auth_pass 1111 } notify_master /etc/keepalived/notify_master_mysql.sh #此条指令告诉keepalived发现自己转为MASTER后执行的脚本 virtual_ipaddress { 192.168.244.10/24 } }
其中,/etc/keepalived/notify_master_mysql.sh的内容如下:
#!/bin/bash ###当keepalived监测到本机转为MASTER状态时,执行该脚本 change_log=/etc/keepalived/logs/state_change.log mysql_con='mysql -uroot -p123456' echo -e "`date "+%F %H:%M:%S"` -----keepalived change to MASTER-----" >> $change_log slave_info() { ###统一定义一个函数取得slave的position、running、和log_file等信息 ###根据函数后面所跟参数来决定取得哪些数据 if [ $1 = slave_status ];then slave_stat=`${mysql_con} -e "show slave status\G;"|egrep -w "Slave_IO_Running|Slave_SQL_Running"` Slave_IO_Running=`echo $slave_stat|awk '{print $2}'` Slave_SQL_Running=`echo $slave_stat|awk '{print $4}'` elif [ $1 = log_file -a $2 = pos ];then log_file_pos=`${mysql_con} -e "show slave status\G;"|egrep -w "Master_Log_File|Read_Master_Log_Pos|Relay_Master_Log_File|Exec_Master_Log_Pos"` Master_Log_File=`echo $log_file_pos|awk '{print $2}'` Read_Master_Log_Pos=`echo $log_file_pos|awk '{print $4}'` Relay_Master_Log_File=`echo $log_file_pos|awk '{print $6}'` Exec_Master_Log_Pos=`echo $log_file_pos|awk '{print $8}'` fi } action() { ###经判断'应该&可以'切换时执行的动作 echo -e "`date "+%F %H:%M:%S"` -----set read_only = 0 on DB2-----" >> $change_log ###解除read_only属性 ${mysql_con} -e "set global read_only = 0;" 2>> $change_log echo "DB2 keepalived转为MASTER状态,线上数据库切换至DB2"|mail -s "DB2 keepalived change to MASTER"\ slowtech@126.com 2>> $change_log echo -e "---------------------------------------------------------\n" >> $change_log } slave_info slave_status if [ $Slave_SQL_Running = Yes ];then i=0 #一个计数器 slave_info log_file pos ###判断从master接收到的binlog是否全部在本地执行(这样仍无法完全确定从库已追上主库,因为无法完全保证io_thread没有延时(由网络传输问题导致的从库落后的概率很小) until [ $Master_Log_File = $Relay_Master_Log_File -a $Read_Master_Log_Pos = $Exec_Master_Log_Pos ] do if [ $i -lt 10 ];then #将等待exec_pos追上read_pos的时间限制为10s echo -e "`date "+%F %H:%M:%S"` -----Relay_Master_Log_File=$Relay_Master_Log_File,Exec_Master_Log_Pos=$Exec_Master_Log_Pos is behind Master_Log_File=$Master_Log_File,Read_Master_Log_Pos=$Read_Master_Log_Pos, wait......" >> $change_log #输出消息到日志,等待exec_pos=read_pos i=$(($i+1)) sleep 1 slave_info log_file pos else echo -e "The waits time is more than 10s,now force change. Master_Log_File=$Master_Log_File Read_Master_Log_Pos=$Read_Master_Log_Pos Relay_Master_Log_File=$Relay_Master_Log_File Exec_Master_Log_Pos=$Exec_Master_Log_Pos" >> $change_log action exit 0 fi done action else slave_info log_file pos echo -e "DB2's slave status is wrong,now force change. Master_Log_File=$Master_Log_File Read_Master_Log_Pos=$Read_Master_Log_Pos Relay_Master_Log_File=$Relay_Master_Log_File Exec_Master_Log_Pos=$Exec_Master_Log_Pos" >> $change_log action fi