[root@node2 ~]# cat check_mysql.sh
#!/bin/bash
MYSQL=/usr/bin/mysql
MYSQL_HOST=127.0.0.1
MYSQL_USER=root
MYSQL_PASSWORD=123456
CHECK_TIME=3
#mysql is working MYSQL_OK is 1 , mysql down MYSQL_OK is 0
MYSQL_OK=1
function check_mysql_helth (){
$MYSQL -h $MYSQL_HOST -u $MYSQL_USER -e "show status;" --password=123456 >/dev/null 2>&1
if [ $? = 0 ] ;then
MYSQL_OK=1
else
MYSQL_OK=0
fi
return $MYSQL_OK
}
while [ $CHECK_TIME -ne 0 ]
do
let "CHECK_TIME -= 1"
check_mysql_helth
if [ $MYSQL_OK = 1 ] ; then
CHECK_TIME=0
exit 0
fi
if [ $MYSQL_OK -eq 0 ] && [ $CHECK_TIME -eq 0 ]
then
pkill keepalived
exit 1
fi
sleep 1
done
启动Keepalived:
[root@node1 ~]#service keepalived start
启动日志:
[root@node1 ~]# tail -f /var/log/messages
Jun 27 11:48:47 node2 Keepalived: Starting VRRP child process, pid=21545
Jun 27 11:48:47 node2 Keepalived_vrrp: Registering Kernel netlink reflector
Jun 27 11:48:47 node2 Keepalived_vrrp: Registering Kernel netlink command channel
Jun 27 11:48:47 node2 Keepalived_vrrp: Registering gratutious ARP shared channel
Jun 27 11:48:47 node2 Keepalived_vrrp: Opening file '/etc/keepalived/keepalived.conf'.
Jun 27 11:48:47 node2 Keepalived_vrrp: Configuration is using : 65535 Bytes
Jun 27 11:48:47 node2 Keepalived_vrrp: Using LinkWatch kernel netlink reflector...
Jun 27 11:48:47 node2 Keepalived_vrrp: VRRP_Instance(mha) Entering BACKUP STATE
Jun 27 11:48:47 node2 Keepalived_vrrp: VRRP sockpool: [ifindex(2), proto(112), fd(9,10)]
Jun 27 11:48:47 node2 Keepalived_vrrp: VRRP_Script(check_run) succeeded
Jun 27 11:49:03 node2 Keepalived_vrrp: VRRP_Instance(mha) Transition to MASTER STATE
Jun 27 11:49:08 node2 Keepalived_vrrp: VRRP_Instance(mha) Entering MASTER STATE
Jun 27 11:49:08 node2 Keepalived_vrrp: VRRP_Instance(mha) setting protocol VIPs.
Jun 27 11:49:08 node2 Keepalived_vrrp: VRRP_Instance(mha) Sending gratuitous ARPs on eth0 for 10.2.16.250
Jun 27 11:49:13 node2 Keepalived_vrrp: VRRP_Instance(mha) Sending gratuitous ARPs on eth0 for 10.2.16.250
查看 VIP:
[root@node1 ~]# ip addr
1: lo: mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:c5:22:55 brd ff:ff:ff:ff:ff:ff
inet 10.2.16.253/24 brd 10.2.16.255 scope global eth0
inet 10.2.16.250/24 brd 10.2.16.255 scope global eth0
inet6 fe80::20c:29ff:fec5:2255/64 scope link
valid_lft forever preferred_lft forever
3: eth1: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:c5:22:5f brd ff:ff:ff:ff:ff:ff
inet 10.0.0.1/24 brd 10.0.0.255 scope global eth1
inet6 fe80::20c:29ff:fec5:225f/64 scope link
valid_lft forever preferred_lft forever