基础架构如下
准备干净的实验环境
[root@mysql1 ~]# systemctl stop firewalld
[root@mysql1 ~]# cat /etc/sysconfig/selinux |grep "SELINUX=disabled"
SELINUX=disabled
[root@mysql1 ~]# setenforce 0
setenforce: SELinux is disabled
[root@mysql1 ~]# getenforce
Disabled
首先,搭建221和222机器mysql互为主从
这里可以参考我之前的帖子mysql主从复制
mysql双主双从
我们本次实验只是两台机器互为主从,即互为master和slave
搭建完成后登录mysql 可以查看状态:
mysql1机器:
mysql2:
可以自行验证修改数据,两边数据库是否同步
随后
开始安装配置 keepalived (mysql1& mysql2)
mysql1:
[root@mysql1 ~]# yum -y install keepalived
[root@mysql1 ~]# vim /etc/keepalived/keepalived.conf! Configuration File for keepalivedglobal_defs {router_id mysql1script_user rootenable_script_securityuser root
}vrrp_script check_run {script "/etc/keepalived/keepalived_check_mysql.sh"interval 5weight 2
}vrrp_instance VI_1 {state MASTERinterface ens33virtual_router_id 88priority 100advert_int 1authentication {auth_type PASSauth_pass yryr}track_script {check_run}virtual_ipaddress {192.168.29.100/24 dev ens33 label ens33:1}
}
mysql2:
[root@mysql2 ~]# yum -y install keepalived
[root@mysql2 ~]# vim /etc/keepalived/keepalived.conf! Configuration File for keepalived
global_defs {router_id mysql2script_user rootenable_script_security
}vrrp_script check_run {script "/etc/keepalived/keepalived_check_mysql.sh"interval 5weight 2
}vrrp_instance VI_1 {state BACKUPinterface ens33virtual_router_id 88priority 50advert_int 1authentication {auth_type PASSauth_pass yryr}track_script {check_run}virtual_ipaddress {192.168.29.100/24 dev ens33 label ens33:1}
}
两台机器上keepalived都配置了一个mysql的检查脚本 ,现在我们来加上:
[root@mysql2 ~]# vim /etc/keepalived/keepalived_check_mysql.sh#!/bin/bash
MYSQL=/usr/bin/mysql
MYSQL_HOST=localhost
MYSQL_USER=root
MYSQL_PASSWORD=123456
CHECK_TIME=3MYSQL_OK=1check_mysql_helth (){ echo "Checking MySQL at $(date)" >> /var/log/keepalived_mysql_check.log$MYSQL -h $MYSQL_HOST -u $MYSQL_USER -p${MYSQL_PASSWORD} -e "show status" >> /var/log/keepalived_mysql_check.log 2>&1if [ $? -eq 0 ]; then MYSQL_OK=1else MYSQL_OK=0 fi return $MYSQL_OK
}while [ $CHECK_TIME -ne 0 ]; do check_mysql_helth if [ $MYSQL_OK -eq 1 ]; then exit 0 filet CHECK_TIME--sleep 1
doneecho "MySQL health check failed at $(date)" >> /var/log/keepalived_mysql_check.log
/usr/bin/systemctl stop keepalived
exit 1
授权:
[root@mysql1 ~]# chmod +x /etc/keepalived/keepalived_check_mysql.sh
重启服务:
[root@mysql1 local]# systemctl restart keepalived[root@mysql2 local]# systemctl restart keepalived
查看:
可以看到我们的vip 100 已经飘到 master机器上
[root@mysql1 local]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lovalid_lft forever preferred_lft foreverinet6 ::1/128 scope host valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000link/ether 00:0c:29:5f:08:ee brd ff:ff:ff:ff:ff:ffinet 192.168.29.221/24 brd 192.168.29.255 scope global noprefixroute ens33valid_lft forever preferred_lft foreverinet 192.168.29.100/24 scope global secondary ens33:1valid_lft forever preferred_lft foreverinet6 fe80::b09a:b99e:bc92:62ee/64 scope link tentative noprefixroute dadfailed valid_lft forever preferred_lft foreverinet6 fe80::9073:a63f:e689:8462/64 scope link noprefixroute valid_lft forever preferred_lft forever
后续可以停止mysql 服务来验证vip状态~