-- 從庫 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | binarylog | | mysql | | performance_schema | | relaylog | | repl | | test | +--------------------+ 7 rows in set (0.00 sec) mysql> use repl Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> select * from repl; +------+ | id | +------+ | 1 | +------+ 1 row in set (0.00 sec) >一主一從同步成功!
-- 檢查nc能否正常使用 [root@mysql02 keepalived-1.2.24]# nc -bash: nc: command not found -- 未安裝nc包 ,yum安裝 [root@mysql02 keepalived-1.2.24]# yum -y install nc
[root@mysql02 keepalived-1.2.24]# vim /data/chk_mysql.sh [root@mysql02 ~]# cat /data/chk_mysql.sh #!/bin/bash # check mysql server status # mysql端口 PORTS="3306"
function check_ports { for port in $PORTS;do nc -z 127.0.0.1 $port | grep -q succeeded [ "${PIPESTATUS[1]}" -eq 0 ] && mark=${mark}1 done # 如果mark值為空說明端口都不通。 # 如果mark等于1,說明有端口是通的。 echo $mark }
ret1=$(check_ports) # 如果mysql端口不通,會嘗試重啟一次mysql if [ "$ret1" != 1 ];then service mysql stop mysqld_safe --defaults-file=/etc/my.cnf & q! #無用命令,只是為了跳出上面的命令 sleep 1 ret2=$(check_ports) # 如果還是有端口不通,表示mysql服務不正常,則停掉keepalived,使VIP發生切換 [ "$ret2" != 1 ] && /etc/init.d/keepalived stop fi