mysql+keepalive实现浮动地址自动切换,由于keepalive无自带健康检查功能,所以必须自动编写健康检查守护进程(监控DB1和DB2数据库的监控状态,来保证浮动地址双机自动切换。)
一,部署说明及拓扑架构:
1、mysql安装在非root用户下(Mysql 版本5.7.18)
2、keepalive安装在root用户下
3、两台服务器安装mysql+keepalive,DB1真实ip地址为:10.112.188.70;DB2真实IP地址为:10.112.188.71;两台数据库除了建立同步账号rep1外,需建立用于守护进程账号check权限只有selecte。
4、DB1与DB2采用双主模式进行部署,但正常情况下只有DB1采用keepalive虚拟浮动IP:10.112.188.69提供服务;一旦DB2异常则由keepalive虚拟浮动IP地址:10.112.188.69自动切换至DB2主机上提供服务。
5、keepalive虚拟浮动地址切换思路(编写守护进程需要);
1)正常情况下只有DB1采用keepalive虚拟浮动IP:10.112.188.69提供服务;DB2中keepalive为stop状态。DB1采用守护进程检查自身mysql状态是否异常:mysql -ucheck-p'check1' -S /iddbs/mysql.sock -e "select version();"一旦DB1自身数据库异常则自动stop keepalive 否则保证 keepalive是启动状态;且还需监控浮动虚拟地址:10.112.188.69下的mysql是否能连接成功,若能连接不成功启动keepalive(备注:监控浮动虚拟地址主要用途是:当DB1异常发生且恢复后(此时DB2已经自动启动keepalive采用虚拟IP地址接管提供服务),监控DB2是否已经释放浮动IP,保证IP地址不冲突。)
2)正常情况下只有DB1采用keepalive虚拟浮动IP:10.112.188.69提供服务;DB2中keepalive为stop状态。DB2采用守护进程监控DB1(真实IP:10.112.188.70)Mysql数据状态(是否异常)采用mysql -ucheck -p'check1' -h 10.112.188.70 -e "select version();" 一旦DB1中MYSQL数据库出现异常(或者宕机)DB2则启动keepalive并浮动VIP为:10.112.188.69
6、keepalive浮动地址切换逻辑图:
二、mysql双主部署及权限账号建立:
1、同步账号建立:
在10.112.188.70上建立:
mysql> grant replication slave on *.* to 'rep1'@'10.112.188.71' identified by 'rep1';
在10.112.188.71
mysql> grant replication slave on *.* to 'rep1'@'10.112.188.70' identified by 'rep1';
2、守护进程账号建立:
在DB1和DB2上均建立守护进程状态查询权限,只授予select:
mysql> GRANT select ON *.* TO 'check'@'10.112.188.70' identified by 'check';
mysql> GRANT select ON *.* TO 'check'@'10.112.188.71' identified by 'check';
3、同步操作以下在DB2上操作:
1)在DB1和DB2库配置文件中my.cnf开启log-bin
server-id = 1
# Uncomment the following if you want to log updates
log-bin=/iddbsdata/mysql-bin
2)mysql> show variables like 'log%'; #查看主库的binlog开关是否生效(ON状态)
+---------------------------------+---------------------------------------------+
| Variable_name | Value |
+---------------------------------+---------------------------------------------+
| log | OFF |
| log_bin | ON |
3)查看binlog节点位置。
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000002 | 1160 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
4)同步(以10.112.188.71同步10.112.188.70为例,及DB2同步DB1):
mysql>CHANGE MASTER TO
>MASTER_HOST='10.112.188.71',
>MASTER_PORT=3307,
>MASTER_USER='rep1',
>MASTER_PASSWORD='rep1',
>MASTER_LOG_FILE='mysql-bin.000002',
>MASTER_LOG_POS=1160;
4、在DB1上重复进行3操作。
5、注意,由于DB1和DB2属于是新库(新安装无数据),在同步之前省去了mysqldump,如果是有数据的数据库必须进行mysqldump操作才能进行数据同步。
三、keepalive部署(需在root用户下)
1、安装部署:
yum install -y openssl openssl-devel
gunzip keepalived-1.2.12.tar.gz
tar -xvf keepalived-1.2.12.tar
cd keepalived-1.2.12
./configure --prefix=/usr/local/keepalived
make
make install
ls -lart /usr/local/keepalived/
cp /usr/local/keepalived/sbin/keepalived /usr/sbin
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d
mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived
cd /etc
cp keepalived.conf keepalived.conf.bak
2、配置文件
[root@xqcldb001 /etc/keepalived]#cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc } # notification_email_from Alexandre.Cassen@firewall.loc # smtp_server 192.168.200.1 # smtp_connect_timeout 30 router_id mysql } vrrp_instance VI_1 { state BACKUP interface bond0 virtual_router_id 60 priority 100 nopreempt advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { #10.208.218.99 dev eth0 label eth0:1 10.112.188.69/26 } } virtual_server 10.112.188.69 3307 { delay_loop 6 lb_algo rr lb_kind NAT net_mask 255.255.255.0 persistence_timeout 50 protocol TCP real_server 10.112.188.70 3307 { weight 1 # notify_down /iddbs/app/check_mysql.sh TCP_CHECK { connect_timeout 10 nb_get_retry 3 connect_port 8066 } } }
[root@xqcldb002 /etc/keepalived]#cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { acassen@firewall.loc failover@firewall.loc sysadmin@firewall.loc } # notification_email_from Alexandre.Cassen@firewall.loc # smtp_server 192.168.200.1 # smtp_connect_timeout 30 router_id mysql } vrrp_instance VI_1 { state BACKUP interface bond0 virtual_router_id 60 priority 100 nopreempt advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { #10.208.218.99 dev eth0 label eth0:1 10.112.188.69/26 } } virtual_server 10.112.188.69 3307 { delay_loop 6 lb_algo rr lb_kind NAT net_mask 255.255.255.0 persistence_timeout 50 protocol TCP real_server 10.112.188.71 3307 { weight 1 # notify_down /iddbs/app/check_mysql.sh TCP_CHECK { connect_timeout 10 nb_get_retry 3 connect_port 8066 } } }
备注经测试VRRP state BACKUP两台必须均为BACKUP,否则一旦重启keepalive会导致网卡IP错误。
四、keepalive守护进程
#!/bin/sh while true do /iddbs/bin/mysql -uroot -p'Siina@678' -S /iddbsdata/mysql.sock -e "select version();">/dev/null 2>&1 if [ $? -eq 0 ] then /etc/init.d/keepalived start >/dev/null 2>&1 else /etc/init.d/keepalived stop >/dev/null 2>&1 fi sleep 1 /iddbs/bin/mysql -h10.112.188.69 -ucheck -p'check1' -P 3307 -e "select version();">/dev/null 2>&1 if [ $? -eq 0 ] then /etc/init.d/keepalived stop >/dev/null 2>&1 else /etc/init.d/keepalived start >/dev/null 2>&1 fi sleep 2 done
#!/bin/sh while true do /iddbs/bin/mysql -h10.112.188.70 -ucheck -p'check1' -P 3307 -e "select version();">/dev/null 2>&1 if [ $? -eq 0 ] then /etc/init.d/keepalived stop >/dev/null 2>&1 sleep 2 else /etc/init.d/keepalived start >/dev/null 2>&1 fi sleep 3 done
守护进程启动:[root@xqcldb001 ~]#sh check_mysql.sh &
五、双主同步及模拟数据库异常keepalive自动切换浮动IP
1、正常情况下:
正常情况在备用服务器上bond0网卡上只有一个地址。
xqcldb002:/iddbs>ip a
10: bond0: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP
link/ether 70:79:90:a1:0b:48 brd ff:ff:ff:ff:ff:ff
inet 10.112.188.71/26 brd 10.112.188.127 scope global bond0
inet6 fe80::7279:90ff:fea1:b48/64 scope link
valid_lft forever preferred_lft forever
正常情况在主服务器上bond0网卡上有一个地址和一个虚拟IP地址
xqcldb001:/iddbs>ip a
10: bond0: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP
link/ether 70:79:90:7d:bf:3e brd ff:ff:ff:ff:ff:ff
inet 10.112.188.70/26 brd 10.112.188.127 scope global bond0
inet 10.112.188.69/26 scope global secondary bond0
inet6 fe80::7279:90ff:fe7d:bf3e/64 scope link
valid_lft forever preferred_lft forever
2、正常情况下:10.112.188.70数据库异常(模拟宕机):
此种情况下70(DB1)bond0网卡上有一个真实地址,已经无浮动IP地址10.112.188.69
xqcldb001:/iddbs>sh mysql_start stop
stop Mysql. [ OK ]
xqcldb001:/iddbs>ip a
10: bond0: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP
link/ether 70:79:90:7d:bf:3e brd ff:ff:ff:ff:ff:ff
inet 10.112.188.70/26 brd 10.112.188.127 scope global bond0
inet6 fe80::7279:90ff:fe7d:bf3e/64 scope lin
71(DB2)bond0网卡上有一个真实地址,浮动IP地址10.112.188.69
xqcldb002:/iddbs>ip a
10: bond0: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP
link/ether 70:79:90:a1:0b:48 brd ff:ff:ff:ff:ff:ff
inet 10.112.188.71/26 brd 10.112.188.127 scope global bond0
inet 10.112.188.69/26 scope global secondary bond0
inet6 fe80::7279:90ff:fea1:b48/64 scope link
valid_lft forever preferred_lft forever
3、数据库恢复:
xqcldb001:/iddbs>sh mysql_start start
starting Mysql.... [ OK ]
xqcldb001:/iddbs>
xqcldb001:/iddbs>
xqcldb001:/iddbs>ps -ef | grep mysql
root 2182 24200 0 11:30 pts/2 00:00:00 sh check_mysql.sh #守护进程
iddbs 4485 1 0 11:35 pts/2 00:00:00 /bin/sh /iddbs/bin/mysqld_safe --defaults-file=/iddbs/my.cnf
iddbs 4890 4485 93 11:35 pts/2 00:00:06 /iddbs/bin/mysqld --defaults-file=/iddbs/my.cnf --basedir=/iddbs --datadir=/iddbsdata --plugin-dir=/iddbs/lib/plugin --log-error=/iddbsdata/mysql-error.log --pid-file=/iddbsdata/mysql.pid --socket=/iddbsdata/mysql.sock --port=3307
iddbs 4949 3619 0 11:36 pts/2 00:00:00 grep mysql
地址已经自动切换至DB1:
xqcldb001:/iddbs>ip a
10: bond0: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP
link/ether 70:79:90:7d:bf:3e brd ff:ff:ff:ff:ff:ff
inet 10.112.188.70/26 brd 10.112.188.127 scope global bond0
inet 10.112.188.69/26 scope global secondary bond0
inet6 fe80::7279:90ff:fe7d:bf3e/64 scope link
valid_lft forever preferred_lft forever
DB2只有真实地址而无虚拟浮动地址:
xqcldb002:/iddbs>ip a
10: bond0: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP
link/ether 70:79:90:a1:0b:48 brd ff:ff:ff:ff:ff:ff
inet 10.112.188.71/26 brd 10.112.188.127 scope global bond0
inet6 fe80::7279:90ff:fea1:b48/64 scope link
valid_lft forever preferred_lft forever