关于MySQL-HA,目前有多种解决方案,比如heartbeat、drbd、mmm、共享存储,但是它们各有优缺点。heartbeat、drbd配置较为复杂,需要自己写脚本才能实现MySQL自动切换,对于不会脚本语言的人来说,这无疑是一种脑裂问题;对于mmm,生产环境中很少有人用,且mmm 管理端需要单独运行一台服务器上,要是想实现高可用,就得对mmm管理端做HA,这样无疑又增加了硬件开支;对于共享存储,个人觉得MySQL数据还是放在本地较为安全,存储设备毕竟存在单点隐患。使用MySQL双master+keepalived是一种非常好的解决方案,在MySQL-HA环境中,MySQL互为主从关系,这样就保证了两台MySQL数据的一致性,然后用keepalived实现虚拟IP,通过keepalived自带的服务监控功能来实现MySQL故障时自动切换。
下面,我把即将上线的一个生产环境中的架构与大家分享一下,看一下这个架构中,MySQL-HA是如何实现的,环境拓扑如下
MySQL-VIP:10.16.0.57
MySQL-master1:10.16.0.246
MySQL-master2:10.16.0.247
OS版本:redhat es 5.4
MySQL版本:5.1.60
Keepalived版本:1.1.19
一、MySQL master-master配置
配置方法可参考本人的“linux下mysql双主热备”文章
二、keepalived安装
两台服务器都安装keepalived,配置方法可参考本人的“ Haproxy+Keepalived实现双机高可用 ”文章
三、keepalived配置
1、10.16.0.246服务器上keepalived配置
- [root@dns2 ~]#vi /etc/keepalived/keepalived.conf
- ! Configuration File for keepalived
- global_defs {
- notification_email {
- [email protected]
- }
- notification_email_from [email protected]
- smtp_server 127.0.0.1
- smtp_connect_timeout 30
- router_id MySQL-ha
- }
- vrrp_instance VI_9 {
- state BACKUP #两台配置此处均是BACKUP
- interface eth0
- virtual_router_id 51
- priority 100 #优先级,另一台改为90
- advert_int 1
- nopreempt #不抢占,只在优先级高的机器上设置即可,优先级低的机器不设置
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- virtual_ipaddress {
- 10.16.0.57
- }
- }
- virtual_server 10.16.0.57 3306 {
- delay_loop 2 #每个2秒检查一次real_server状态
- lb_algo wrr #LVS算法
- lb_kind DR #LVS模式
- persistence_timeout 60 #会话保持时间
- protocol TCP
- real_server 10.16.0.246 3306 {
- weight 3
- notify_down /usr/local/MySQL/bin/MySQL.sh #检测到服务down后执行的脚本
- TCP_CHECK {
- connect_timeout 10 #连接超时时间
- nb_get_retry 3 #重连次数
- delay_before_retry 3 #重连间隔时间
- connect_port 3306 #健康检查端口
- }
- }
- }
编写检测服务down后所要执行的脚本
- [root@dns2 ~]#vi /usr/local/MySQL/bin/MySQL.sh
- #!/bin/sh
- killall keepalived
- [root@dns2 ~]#chmod +x /usr/local/MySQL/bin/MySQL.sh
注:此脚本是上面配置文件notify_down选项所用到的,keepalived使用notify_down选项来检查real_server的服务状态,当发现real_server服务故障时,便触发此脚本;我们可以看到,脚本就一个命令,通过killall keepalived强制杀死keepalived进程,从而实现了MySQL故障自动转移。另外,我们不用担心两个MySQL会同时提供数据更新操作,因为每台MySQL上的keepalived的配置里面只有本机MySQL的IP+VIP,而不是两台MySQL的IP+VIP
启动keepalived
- [root@dns2 ~]#service keepalived start
- [root@dns2 ~]#ps -aux | grep keepalived
测试
找一台局域网PC,然后去ping MySQL的VIP,这时候MySQL的VIP是可以ping的通的
停止MySQL服务,看keepalived健康检查程序是否会触发我们编写的脚本
2、10.16.0.247上keepalived配置
配置keepalived
这台配置和上面基本一样,但有三个地方不同:优先级为90、无抢占设置、real_server为本机IP
- [root@dns2 ~]#mkdir /etc/keepalived
- [root@dns2 ~]#vi /etc/keepalived/keepalived.conf
- ! Configuration File for keepalived
- global_defs {
- notification_email {
- [email protected]
- }
- notification_email_from [email protected]
- smtp_server 127.0.0.1
- smtp_connect_timeout 30
- router_id MySQL-ha
- }
- vrrp_instance VI_9 {
- state BACKUP
- interface eth0
- virtual_router_id 51
- priority 90
- advert_int 1
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- virtual_ipaddress {
- 10.16.0.57
- }
- }
- virtual_server 10.16.0.57 3306 {
- delay_loop 2
- lb_algo wrr
- lb_kind DR
- persistence_timeout 60
- protocol TCP
- real_server 10.16.0.247 3306 {
- weight 3
- notify_down /usr/local/MySQL/bin/MySQL.sh
- TCP_CHECK {
- connect_timeout 10
- nb_get_retry 3
- delay_before_retry 3
- connect_port 3306
- }
- }
- }
编写检测服务down后所要执行的脚本
- [root@dns2 ~]#vi /usr/local/MySQL/bin/MySQL.sh
- #!/bin/sh
- killall keepalived
- [root@dns2 ~]#chmod +x /usr/local/MySQL/bin/MySQL.sh
- 启动keepalived
- [root@dns2 ~]#service keepalived start
- [root@dns2 ~]#ps -aux | grep keepalived
测试
停止MySQL服务,看keepalived健康检查程序是否会触发我们编写的脚本
三、测试
MySQL远程登录测试
我们找一台安装有MySQL客户端的windows,然后登录VIP,看是否能登录,在登录之两台MySQL服务器都要授权允许从远程登录
- MySQL> grant all privileges on *.* to 'root'@'%' identified by '123456';
- Query OK, 0 rows affected (0.00 sec)
- MySQL> flush privileges;
- Query OK, 0 rows affected (0.00 sec)
使用客户端登录VIP测试
- C:\MySQL\bin>MySQL.exe -uroot -p123456 -h192.168.1.200 -P3306
- Welcome to the MySQL monitor. Commands end with ; or \g.
- Your MySQL connection id is 224
- Server version: 5.0.89-log Source distribution
- Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
- MySQL>
● keepalived故障转移测试
※在windows客户端一直去ping VIP,然后关闭10.16.0.246上的keepalived,正常情况下VIP就会切换到10.16.0.247上面去
※开启10.16.0.246上的keepalived,关闭10.16.0.247上的keepalived,看是否能自动切换,正常情况下VIP又会属于10.16.0.246
注:keepalived切换速度还是非常块的,整个切换过程只需1-3秒
● MySQL故障转移测试
※在10.16.0.246上关闭MySQL服务,看VIP是否会切换到10.16.0.247上
※开启10.16.0.246上的MySQL和keepalived,然后关闭10.16.0.247上的MySQL,看VIP是否会切换到10.16.0.246上
下面是用windows客户端连接的MySQL的VIP,在切换时我执行了一个MySQL查询命令,从执行show databases到显示出结果时间为3-5秒(大家可以看到上面有个错误提示,不过不用担心,因为我们的keepalived切换大概为3秒左右,这3秒左右VIP是谁都不属于的)
- MySQL> show databases;
- ERROR 2006 (HY000): MySQL server has gone away
- No connection. Trying to reconnect...
- Connection id: 592
- Current database: *** NONE ***
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | MySQL |
- | test |
- +--------------------+
- 3 rows in set (9.01 sec)
参考文献:http://database.51cto.com/art/201012/237204.htm