-- 从库 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