[iyunv@bj02-ops-mgotest02 local]# vim /root/keepalived_check_mysql.sh
#!/bin/bash
MYSQL=/usr/local/mysql/bin/mysql
CHECK_TIME=3
#mysql is working MYSQL_OK is 1 , mysql down MYSQL_OK is 0
MYSQL_OK=1
function check_mysql_helth (){
$MYSQL --defaults-file=/root/client.cnf -e "show status;" >/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
/etc/init.d/keepalived stop
exit 1
fi
sleep 1
done
[iyunv@bj02-ops-mgotest02 local]# chmod +x /root/keepalived_check_mysql.sh
### 五、启动keeplaived服务 ###
[iyunv@bj02-ops-mgotest02 local]# /etc/init.d/keepalived start
Starting keepalived: [ OK ]
[iyunv@bj02-ops-mgotest02 local]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 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
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
link/ether 00:50:56:b9:37:e2 brd ff:ff:ff:ff:ff:ff
inet 10.125.141.138/24 brd 10.125.141.255 scope global eth0
inet 10.125.141.249/32 scope global eth0
[iyunv@bj02-ops-mgotest02 local]#
[iyunv@bj02-ops-mgotest02 ~]# tail -f /var/log/messages
......
......
Dec 8 17:09:26 bj02-ops-mgotest02 Keepalived_vrrp[13360]: Sending gratuitous ARP on eth0 for 10.125.141.249
Dec 8 17:09:26 bj02-ops-mgotest02 Keepalived_vrrp[13360]: Sending gratuitous ARP on eth0 for 10.125.141.249
Dec 8 17:09:26 bj02-ops-mgotest02 Keepalived_healthcheckers[13359]: Netlink reflector reports IP 10.125.141.249 added
Dec 8 17:09:31 bj02-ops-mgotest02 Keepalived_vrrp[13360]: Sending gratuitous ARP on eth0 for 10.125.141.249
Dec 8 17:09:31 bj02-ops-mgotest02 Keepalived_vrrp[13360]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on eth0 for 10.125.141.249
Dec 8 17:09:31 bj02-ops-mgotest02 Keepalived_vrrp[13360]: Sending gratuitous ARP on eth0 for 10.125.141.249
Dec 8 17:09:31 bj02-ops-mgotest02 Keepalived_vrrp[13360]: Sending gratuitous ARP on eth0 for 10.125.141.249
Dec 8 17:09:31 bj02-ops-mgotest02 Keepalived_vrrp[13360]: Sending gratuitous ARP on eth0 for 10.125.141.249
Dec 8 17:09:31 bj02-ops-mgotest02 Keepalived_vrrp[13360]: Sending gratuitous ARP on eth0 for 10.125.141.249
Dec 8 17:13:09 bj02-ops-mgotest02 -bash: HISTORY: PPID=8695 PID=8697 SID=8649 UID=0 User=root ip add
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| testa |
| testb |
+--------------------+
6 rows in set (0.00 sec)
mysql>
[iyunv@bj02-ops-mgotest03 ~]# mysql
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| testa |
| testb |
+--------------------+
6 rows in set (0.01 sec)
mysql>
停止主库,测试。
[iyunv@bj02-ops-mgotest02 local]# /etc/init.d/mysqld stop
Shutting down MySQL (Percona Server)............. SUCCESS!
[iyunv@bj02-ops-mgotest02 local]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 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
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
link/ether 00:50:56:b9:37:e2 brd ff:ff:ff:ff:ff:ff
inet 10.125.141.138/24 brd 10.125.141.255 scope global eth0
[iyunv@bj02-ops-mgotest02 ~]# tail -f /var/log/messages
Dec 8 17:23:15 bj02-ops-mgotest02 -bash: HISTORY: PPID=12914 PID=12916 SID=12873 UID=0 User=root >/var/log/messages
Dec 8 17:23:17 bj02-ops-mgotest02 -bash: HISTORY: PPID=12914 PID=12916 SID=12873 UID=0 User=root tail -f /var/log/messages
Dec 8 17:23:20 bj02-ops-mgotest02 -bash: HISTORY: PPID=8695 PID=8697 SID=8649 UID=0 User=root /etc/init.d/mysqld stop
Dec 8 17:23:23 bj02-ops-mgotest02 Keepalived[13358]: Stopping
Dec 8 17:23:23 bj02-ops-mgotest02 Keepalived_vrrp[13360]: VRRP_Instance(VI_1) sent 0 priority
Dec 8 17:23:23 bj02-ops-mgotest02 Keepalived_vrrp[13360]: VRRP_Instance(VI_1) removing protocol VIPs.
Dec 8 17:23:23 bj02-ops-mgotest02 Keepalived_healthcheckers[13359]: Stopped
Dec 8 17:23:24 bj02-ops-mgotest02 Keepalived_vrrp[13360]: Stopped
Dec 8 17:23:24 bj02-ops-mgotest02 Keepalived[13358]: Stopped Keepalived v1.3.2 (12/08,2016)
在原有的从库上查看。
[iyunv@bj02-ops-mgotest03 ~]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 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
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
link/ether 00:50:56:b9:03:58 brd ff:ff:ff:ff:ff:ff
inet 10.125.141.139/24 brd 10.125.141.255 scope global eth0
inet 10.125.141.249/32 scope global eth0
[iyunv@bj02-ops-mgotest03 ~]#
[iyunv@bj02-ops-mgotest03 ~]# tail -f /var/log/messages
Dec 8 17:23:25 bj02-ops-mgotest03 Keepalived_vrrp[21675]: Sending gratuitous ARP on eth0 for 10.125.141.249
Dec 8 17:23:25 bj02-ops-mgotest03 Keepalived_vrrp[21675]: Sending gratuitous ARP on eth0 for 10.125.141.249
Dec 8 17:23:25 bj02-ops-mgotest03 Keepalived_healthcheckers[21674]: Netlink reflector reports IP 10.125.141.249 added
Dec 8 17:23:30 bj02-ops-mgotest03 Keepalived_vrrp[21675]: Sending gratuitous ARP on eth0 for 10.125.141.249
Dec 8 17:23:30 bj02-ops-mgotest03 Keepalived_vrrp[21675]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on eth0 for 10.125.141.249
Dec 8 17:23:30 bj02-ops-mgotest03 Keepalived_vrrp[21675]: Sending gratuitous ARP on eth0 for 10.125.141.249
Dec 8 17:23:30 bj02-ops-mgotest03 Keepalived_vrrp[21675]: Sending gratuitous ARP on eth0 for 10.125.141.249
Dec 8 17:23:30 bj02-ops-mgotest03 Keepalived_vrrp[21675]: Sending gratuitous ARP on eth0 for 10.125.141.249
Dec 8 17:23:30 bj02-ops-mgotest03 Keepalived_vrrp[21675]: Sending gratuitous ARP on eth0 for 10.125.141.249
附加测试。修复原有的主库,停止现有的主库,看看能切换不?
[iyunv@bj02-ops-mgotest02 local]# /etc/init.d/mysqld start
Starting MySQL (Percona Server).... SUCCESS!
[iyunv@bj02-ops-mgotest02 local]# /etc/init.d/keepalived start
Starting keepalived: [ OK ]
[iyunv@bj02-ops-mgotest02 local]#
停止现有的主库。
[iyunv@bj02-ops-mgotest03 ~]# /etc/init.d/mysqld stop
Shutting down MySQL (Percona Server)............... SUCCESS!
[iyunv@bj02-ops-mgotest03 ~]#
[iyunv@bj02-ops-mgotest03 ~]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 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
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
link/ether 00:50:56:b9:03:58 brd ff:ff:ff:ff:ff:ff
inet 10.125.141.139/24 brd 10.125.141.255 scope global eth0
[iyunv@bj02-ops-mgotest03 ~]# tail -f /var/log/messages
......
......
Dec 8 17:23:30 bj02-ops-mgotest03 Keepalived_vrrp[21675]: Sending gratuitous ARP on eth0 for 10.125.141.249
Dec 8 17:24:36 bj02-ops-mgotest03 -bash: HISTORY: PPID=15262 PID=15264 SID=15217 UID=0 User=root ip add
Dec 8 17:28:00 bj02-ops-mgotest03 -bash: HISTORY: PPID=15262 PID=15264 SID=15217 UID=0 User=root /etc/init.d/mysqld stop
Dec 8 17:28:03 bj02-ops-mgotest03 Keepalived[21673]: Stopping
Dec 8 17:28:03 bj02-ops-mgotest03 Keepalived_vrrp[21675]: VRRP_Instance(VI_1) sent 0 priority
Dec 8 17:28:03 bj02-ops-mgotest03 Keepalived_vrrp[21675]: VRRP_Instance(VI_1) removing protocol VIPs.
Dec 8 17:28:03 bj02-ops-mgotest03 Keepalived_healthcheckers[21674]: Stopped
Dec 8 17:28:04 bj02-ops-mgotest03 Keepalived_vrrp[21675]: Stopped
Dec 8 17:28:04 bj02-ops-mgotest03 Keepalived[21673]: Stopped Keepalived v1.3.2 (12/08,2016)
查看现在的主库。
[iyunv@bj02-ops-mgotest02 local]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 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
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
link/ether 00:50:56:b9:37:e2 brd ff:ff:ff:ff:ff:ff
inet 10.125.141.138/24 brd 10.125.141.255 scope global eth0
inet 10.125.141.249/32 scope global eth0
[iyunv@bj02-ops-mgotest02 local]#
[iyunv@bj02-ops-mgotest02 ~]# tail -f /var/log/messages
Dec 8 17:26:54 bj02-ops-mgotest02 Keepalived_vrrp[15922]: VRRP_Script(check_run) succeeded
Dec 8 17:27:09 bj02-ops-mgotest02 -bash: HISTORY: PPID=8695 PID=8697 SID=8649 UID=0 User=root mysql
Dec 8 17:28:03 bj02-ops-mgotest02 Keepalived_vrrp[15922]: VRRP_Instance(VI_1) Transition to MASTER STATE
Dec 8 17:28:04 bj02-ops-mgotest02 Keepalived_vrrp[15922]: VRRP_Instance(VI_1) Entering MASTER STATE
Dec 8 17:28:04 bj02-ops-mgotest02 Keepalived_vrrp[15922]: VRRP_Instance(VI_1) setting protocol VIPs.
Dec 8 17:28:04 bj02-ops-mgotest02 Keepalived_vrrp[15922]: Sending gratuitous ARP on eth0 for 10.125.141.249
Dec 8 17:28:04 bj02-ops-mgotest02 Keepalived_vrrp[15922]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on eth0 for 10.125.141.249
Dec 8 17:28:04 bj02-ops-mgotest02 Keepalived_vrrp[15922]: Sending gratuitous ARP on eth0 for 10.125.141.249
Dec 8 17:28:04 bj02-ops-mgotest02 Keepalived_vrrp[15922]: Sending gratuitous ARP on eth0 for 10.125.141.249
Dec 8 17:28:04 bj02-ops-mgotest02 Keepalived_vrrp[15922]: Sending gratuitous ARP on eth0 for 10.125.141.249
Dec 8 17:28:04 bj02-ops-mgotest02 Keepalived_vrrp[15922]: Sending gratuitous ARP on eth0 for 10.125.141.249
Dec 8 17:28:04 bj02-ops-mgotest02 Keepalived_healthcheckers[15921]: Netlink reflector reports IP 10.125.141.249 added
Dec 8 17:28:09 bj02-ops-mgotest02 Keepalived_vrrp[15922]: Sending gratuitous ARP on eth0 for 10.125.141.249
Dec 8 17:28:09 bj02-ops-mgotest02 Keepalived_vrrp[15922]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on eth0 for 10.125.141.249