masterA配置:
yum -y install keepalived
vim /etc/keepalived/keepalived.conf
router_id LVS_MASTER-A
interface ens33
nopreempt
track_script {
mysql
}
vim /opt/mysql.sh //编写脚本
脚本内容:
#!/bin/bash
counter=$(netstat -na|grep "LISTEN"|grep "3306"|wc -l)
if [ "${counter}" -eq 0 ]; then
systemctl stop keepalived
fi
chmod +x /opt/mysql.sh
systemctl start keepalived //重启keepalived
ip a | grep ens33
tail -f /var/log/messages
masterB配置:
yum -y install keepalived
vim /etc/keepalived/keepalived.conf
router_id LVS_MASTER-B
interface ens32
priority 99
track_script {
mysql
}
vim /opt/mysql.sh
脚本内容:
#!/bin/bash
counter=$(netstat -na|grep "LISTEN"|grep "3306"|wc -l)
if [ "${counter}" -eq 0 ]; then
systemctl stop keepalived
fi
chmod +x /opt/mysql.sh
systemctl start keepalived
tail -f /var/log/messages
测试VIP转移
masterA配置
systemctl stop mariadb
ip a | grep ens33
ps aux | grep mysql
masterB 配置
ip a | grep ens32
tail -f /var/log/messages
在远程客户端测试
所有mysql服务器授权
mysql -uroot -p000000
grant all on *.* to 'root'@'192.168.96.%' identified by '123456';
flush privileges;
通过VIP登录测试:
mysql -uroot -p123456 -h 192.168.96.16