在主从上都创建当mysql停止工作时自动关闭本机的keepalived的脚本:
cat /root/sh/mysql.sh #!/bin/bash MYSQL=/usr/local/mysql/bin/mysql MYSQL_HOST=localhost MYSQL_USER=root MYSQL_PASSWORD="admin" CHECK_TIME=3 #mysql is working MYSQL_OK is 0 , mysql down MYSQL_OK is 1 MYSQL_OK=1 function check_mysql_helth (){ $MYSQL -h $MYSQL_HOST -u $MYSQL_USER -p $MYSQL_PASSWORD -e "show status;" > /dev/null 2>&1 if [ $? = 0 ] ;then MYSQL_OK=0 else MYSQL_OK=1 fi return $MYSQL_OK } while [ $CHECK_TIME -ne 0 ] do let "CHECK_TIME -= 1" check_mysql_helth if [ $MYSQL_OK = 0 ] ; then CHECK_TIME=0 exit 0 fi if [ $MYSQL_OK -eq 1 ] && [ $CHECK_TIME -eq 1 ] then /etc/init.d/keepalived stop exit 1 fi sleep 1 done6.故障转移测试