利用Keepalived+mysql构建高可用MySQL双主自动切转
2016-04-07 10:51
525 查看
转载:http://www.it300.com/index.php/article-15266.html
关于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是如何实现的,环境拓扑如下:
一、MySQLmaster-master配置
1、修改MySQL配置文件
两台MySQL均如要开启binlog日志功能,开启方法:在MySQL配置文件[MySQLd]段中加上log-bin=MySQL-bin选项
两台MySQL的server-ID不能一样,默认情况下两台MySQL的serverID都是1,需将其中一台修改为2即可
Master1配置:
Master2配置:
2、将192.168.1.201设为192.168.1.202的主服务器
在192.168.1.201上新建授权用户
在192.168.1.202上将192.168.1.201设为自己的主服务器
3、将192.168.1.202设为192.168.1.201的主服务器
在192.168.1.202上新建授权用户
在192.168.1.201上,将192.168.1.202设为自己的主服务器
4、MySQL同步测试
如上述均正确配置,现在任何一台MySQL上更新数据都会同步到另一台MySQL,MySQL同步在此不再演示
二、keepalived安装及配置
1、192.168.1.201服务器上keepalived安装及配置
安装keepalived
配置keepalived
我们自己在新建一个配置文件,默认情况下keepalived启动时会去/etc/keepalived目录下找配置文件
编写检测服务down后所要执行的脚本
注:此脚本是上面配置文件notify_down选项所用到的,keepalived使用notify_down选项来检查real_server的服务状态,当发现real_server服务故障时,便触发此脚本;我们可以看到,脚本就一个命令,通过pkillkeepalived强制杀死keepalived进程,从而实现了MySQL故障自动转移。另外,我们不用担心两个MySQL会同时提供数据更新操作,因为每台MySQL上的keepalived的配置里面只有本机MySQL的IP+VIP,而不是两台MySQL的IP+VIP
启动keepalived
测试
找一台局域网PC,然后去pingMySQL的VIP,这时候MySQL的VIP是可以ping的通的
停止MySQL服务,看keepalived健康检查程序是否会触发我们编写的脚本
2、192.168.1.202上keepalived安装及配置
安装keepalived,安装方法参照192.168.1.201的安装方法
配置keepalived
这台配置和上面基本一样,但有三个地方不同:优先级为90、无抢占设置、real_server为本机IP
编写检测服务down后所要执行的脚本
启动keepalived
测试
停止MySQL服务,看keepalived健康检查程序是否会触发我们编写的脚本
三、测试
MySQL远程登录测试
我们找一台安装有MySQL客户端的windows,然后登录VIP,看是否能登录,在登录之两台MySQL服务器都要授权允许从远程登录
使用客户端登录VIP测试
keepalived故障转移测试
※在windows客户端一直去pingVIP,然后关闭192.168.1.201上的keepalived,正常情况下VIP就会切换到192.168.1.202上面去
※开启192.168.1.201上的keepalived,关闭192.168.1.202上的keepalived,看是否能自动切换,正常情况下VIP又会属于192.168.1.201
注:keepalived切换速度还是非常块的,整个切换过程只需1-3秒
●MySQL故障转移测试
※在192.168.1.201上关闭MySQL服务,看VIP是否会切换到192.168.1.202上
※开启192.168.1.201上的MySQL和keepalived,然后关闭192.168.1.202上的MySQL,看VIP是否会切换到192.168.1.201上
下面是用windows客户端连接的MySQL的VIP,在切换时我执行了一个MySQL查询命令,从执行showdatabases到显示出结果时间为3-5秒(大家可以看到上面有个错误提示,不过不用担心,因为我们的keepalived切换大概为3秒左右,这3秒左右VIP是谁都不属于的)
后话:世间万事万物,都不具备绝对的完美,就像上面的MySQL-HA一样,keepalived只能做到对3306的健康检查,但是做不到比如像MySQL复制中的slave-SQL、slave-IO进程的检查。所以要想做到一些细致的健康检查,还得需要借助额外的监控工具,比如nagios,然后用nagios实现短信、邮件报警,从而能够有效地解决问题。
关于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是如何实现的,环境拓扑如下:
1 | MySQL -VIP :192.168.1.200 |
2 | MySQL-master1:192.168.1.201 |
3 | MySQL-master2:192.168.1.202 |
4 |
5 | OS版本:CentOS5.4 |
6 | MySQL版本:5.0.89 |
7 | Keepalived版本:1.1.20 |
1、修改MySQL配置文件
两台MySQL均如要开启binlog日志功能,开启方法:在MySQL配置文件[MySQLd]段中加上log-bin=MySQL-bin选项
两台MySQL的server-ID不能一样,默认情况下两台MySQL的serverID都是1,需将其中一台修改为2即可
Master1配置:
01 | #vim/etc/my.cnf |
02 |
03 | log -bin =mysql -bin //开启binlog日志功能 |
04 | log=/usr/local/mysql/var/mysql.log//会打印mysql的所以sql语句 |
05 |
06 | server -id =1// |
07 |
08 | binlog -do -db =test//需要同步的库名称 |
09 | auto -increment -increment =2 |
10 | auto -increment -offset =2<spanstyle= "font-family:Simsun;" ><spanstyle= "white-space:normal;" ></span></span> |
01 | #vim/etc/my.cnf |
02 |
03 | log -bin =mysql -bin //开启binlog日志功能 |
04 | log=/usr/local/mysql/var/mysql.log//会打印mysql的所以sql语句 |
05 |
06 | server -id =2 |
07 |
08 | binlog -do -db =test//需要同步的库名称 |
09 | auto -increment -increment =2 |
10 | auto -increment -offset =2 |
在192.168.1.201上新建授权用户
01 | MySQL>grantreplicationslaveon*.*to 'replication' @ '%' identifiedby 'replication' ; |
02 | QueryOK,0rowsaffected(0.00sec) |
03 |
04 | MySQL>showmasterstatus; |
05 | +------------------+----------+--------------+------------------+ |
06 | |File|Position|Binlog_Do_DB|Binlog_Ignore_DB| |
07 | +------------------+----------+--------------+------------------+ |
08 | |MySQL -bin .000003|374||| |
09 | +------------------+----------+--------------+------------------+ |
10 | 1rowin set (0.00sec) |
01 | MySQL>changemastertomaster_host= '192.168.1.201' ,master_user= 'replication' ,master_password= 'replication' ,master_log_file= 'MySQL-bin.000003' ,master_log_pos=374; |
02 | QueryOK,0rowsaffected(0.05sec) |
03 |
04 | MySQL>startslave; |
05 | QueryOK,0rowsaffected(0.00sec) |
06 |
07 | MySQL>showslavestatus\G |
08 | ***************************1.row*************************** |
09 | Slave_IO_State:Waitingformastertosendevent |
10 | Master_Host:192.168.1.201 |
11 | Master_User:replication |
12 | Master_Port:3306 |
13 | Connect_Retry:60 |
14 | Master_Log_File:MySQL -bin .000003 |
15 | Read_Master_Log_Pos:374 |
16 | Relay_Log_File:MySQL-master2 -relay -bin .000002 |
17 | Relay_Log_Pos:235 |
18 | Relay_Master_Log_File:MySQL -bin .000003 |
19 | Slave_IO_Running:Yes |
20 | Slave_SQL_Running:Yes |
21 | Replicate_Do_DB: |
22 | Replicate_Ignore_DB: |
23 | Replicate_Do_Table: |
24 | Replicate_Ignore_Table: |
25 | Replicate_Wild_Do_Table: |
26 | Replicate_Wild_Ignore_Table: |
27 | Last_Errno:0 |
28 | Last_Error: |
29 | Skip_Counter:0 |
30 | Exec_Master_Log_Pos:374 |
31 | Relay_Log_Space:235 |
32 | Until_Condition:None |
33 | Until_Log_File: |
34 | Until_Log_Pos:0 |
35 | Master_SSL_Allowed:No |
36 | Master_SSL_CA_File: |
37 | Master_SSL_CA_Path: |
38 | Master_SSL_Cert: |
39 | Master_SSL_Cipher: |
40 | Master_SSL_Key: |
41 | Seconds_Behind_Master:0 |
42 | 1rowin set (0.00sec) |
在192.168.1.202上新建授权用户
01 | MySQL>grantreplicationslaveon*.*to 'replication' @ '%' identifiedby 'replication' ; |
02 | QueryOK,0rowsaffected(0.00sec) |
03 |
04 | MySQL>showmasterstatus; |
05 | +------------------+----------+--------------+------------------+ |
06 | |File|Position|Binlog_Do_DB|Binlog_Ignore_DB| |
07 | +------------------+----------+--------------+------------------+ |
08 | |MySQL -bin .000003|374||| |
09 | +------------------+----------+--------------+------------------+ |
10 | 1rowin set (0.00sec) |
01 | MySQL>changemastertomaster_host= '192.168.1.202' ,master_user= 'replication' ,master_password= 'replication' ,master_log_file= 'MySQL-bin.000003' ,master_log_pos=374; |
02 | QueryOK,0rowsaffected(0.05sec) |
03 |
04 | MySQL>startslave; |
05 | QueryOK,0rowsaffected(0.00sec) |
06 |
07 | MySQL>showslavestatus\G |
08 | ***************************1.row*************************** |
09 | Slave_IO_State:Waitingformastertosendevent |
10 | Master_Host:192.168.1.202 |
11 | Master_User:replication |
12 | Master_Port:3306 |
13 | Connect_Retry:60 |
14 | Master_Log_File:MySQL -bin .000003 |
15 | Read_Master_Log_Pos:374 |
16 | Relay_Log_File:MySQL-master1 -relay -bin .000002 |
17 | Relay_Log_Pos:235 |
18 | Relay_Master_Log_File:MySQL -bin .000003 |
19 | Slave_IO_Running:Yes |
20 | Slave_SQL_Running:Yes |
21 | Replicate_Do_DB: |
22 | Replicate_Ignore_DB: |
23 | Replicate_Do_Table: |
24 | Replicate_Ignore_Table: |
25 | Replicate_Wild_Do_Table: |
26 | Replicate_Wild_Ignore_Table: |
27 | Last_Errno:0 |
28 | Last_Error: |
29 | Skip_Counter:0 |
30 | Exec_Master_Log_Pos:374 |
31 | Relay_Log_Space:235 |
32 | Until_Condition:None |
33 | Until_Log_File: |
34 | Until_Log_Pos:0 |
35 | Master_SSL_Allowed:No |
36 | Master_SSL_CA_File: |
37 | Master_SSL_CA_Path: |
38 | Master_SSL_Cert: |
39 | Master_SSL_Cipher: |
40 | Master_SSL_Key: |
41 | Seconds_Behind_Master:0 |
42 | 1rowin set (0.00sec) |
如上述均正确配置,现在任何一台MySQL上更新数据都会同步到另一台MySQL,MySQL同步在此不再演示
二、keepalived安装及配置
1、192.168.1.201服务器上keepalived安装及配置
安装keepalived
01 | #wget |
02 | #tarxvzfkeepalived-1.2.2.tar.gz |
03 | #cdkeepalived-1.2.2 |
04 | #./configure |
05 | #make&&makeinstall |
06 | #cp/usr/local/etc/rc.d/init.d/keepalived/etc/init.d/ |
07 | cp /usr/local/etc/sysconfig/keepalived/etc/sysconfig/ |
08 | chmod+x/etc/init.d/keepalived |
09 | chkconfig- -add keepalived |
10 | chkconfigkeepalivedon |
11 | mkdir/etc/keepalived |
12 | ln -s /usr/local/sbin/keepalived/usr/sbin/ |
我们自己在新建一个配置文件,默认情况下keepalived启动时会去/etc/keepalived目录下找配置文件
01 | #vi/etc/keepalived/keepalived.conf |
02 | global_defs{ |
03 | notification_email{ |
04 | haohailuo@163.com |
05 | } |
06 | notification_email_fromhaohailuo@163.com |
07 | smtp_server127.0.0.1 |
08 | smtp_connect_timeout30 |
09 | router_idMySQL -ha |
10 | } |
11 |
12 | vrrp_instanceVI_1{ |
13 | stateBACKUP #两台配置此处均是BACKUP |
14 | interfaceeth0 |
15 | virtual_router_id51 |
16 | priority100 #优先级,另一台改为90 |
17 | advert_int1 |
18 | nopreempt #不抢占,只在优先级高的机器上设置即可,优先级低的机器不设置 |
19 | authentication{ |
20 | auth_typePASS |
21 | auth_pass1111 |
22 | } |
23 | virtual_ipaddress{ |
24 | 192.168.1.200 |
25 | } |
26 | } |
27 |
28 | virtual_server192.168.1.2003306{ |
29 | delay_loop2 #每个2秒检查一次real_server状态 |
30 | lb_algowrr #LVS算法 |
31 | lb_kindDR #LVS模式 |
32 | persistence_timeout60 #会话保持时间 |
33 | protocolTCP |
34 | real_server192.168.1.2013306{ |
35 | weight3 |
36 | notify_down/usr/local/MySQL/bin/MySQL.sh #检测到服务down后执行的脚本 |
37 | TCP_CHECK{ |
38 | connect_timeout10 #连接超时时间 |
39 | nb_get_retry3 #重连次数 |
40 | delay_before_retry3 #重连间隔时间 |
41 | connect_port3306 #健康检查端口 |
42 | } |
43 | } |
44 | } |
1 | #vi/usr/local/MySQL/bin/MySQL.sh |
2 | #!/bin/sh |
3 | pkillkeepalived |
4 | #chmod+x/usr/local/MySQL/bin/MySQL.sh |
启动keepalived
1 | #/usr/local/keepalived/sbin/keepalived–D |
2 | #ps-aux|grepkeepalived |
找一台局域网PC,然后去pingMySQL的VIP,这时候MySQL的VIP是可以ping的通的
停止MySQL服务,看keepalived健康检查程序是否会触发我们编写的脚本
2、192.168.1.202上keepalived安装及配置
安装keepalived,安装方法参照192.168.1.201的安装方法
配置keepalived
这台配置和上面基本一样,但有三个地方不同:优先级为90、无抢占设置、real_server为本机IP
01 | #vi/etc/keepalived/keepalived.conf |
02 | global_defs{ |
03 | notification_email{ |
04 | haohailuo@163.com |
05 | } |
06 | notification_email_fromhaohailuo@163.com |
07 | smtp_server127.0.0.1 |
08 | smtp_connect_timeout30 |
09 | router_idMySQL -ha |
10 | } |
11 |
12 | vrrp_instanceVI_1{ |
13 | stateBACKUP |
14 | interfaceeth0 |
15 | virtual_router_id51 |
16 | priority90 |
17 | advert_int1 |
18 | authentication{ |
19 | auth_typePASS |
20 | auth_pass1111 |
21 | } |
22 | virtual_ipaddress{ |
23 | 192.168.1.200 |
24 | } |
25 | } |
26 |
27 | virtual_server192.168.1.2003306{ |
28 | delay_loop2 |
29 | lb_algowrr |
30 | lb_kindDR |
31 | persistence_timeout60 |
32 | protocolTCP |
33 | real_server192.168.1.2023306{ |
34 | weight3 |
35 | notify_down/usr/local/MySQL/bin/MySQL.sh |
36 | TCP_CHECK{ |
37 | connect_timeout10 |
38 | nb_get_retry3 |
39 | delay_before_retry3 |
40 | connect_port3306 |
41 | } |
42 | } |
43 | } |
1 | #vi/usr/local/MySQL/bin/MySQL.sh |
2 | #!/bin/sh |
3 | pkillkeepalived |
4 | #chmod+x/usr/local/MySQL/bin/MySQL.sh |
1 | #/usr/local/keepalived/sbin/keepalived–D |
2 | #ps-aux|grepkeepalived |
停止MySQL服务,看keepalived健康检查程序是否会触发我们编写的脚本
三、测试
MySQL远程登录测试
我们找一台安装有MySQL客户端的windows,然后登录VIP,看是否能登录,在登录之两台MySQL服务器都要授权允许从远程登录
1 | MySQL>grantallprivilegeson*.*to 'root' @ '%' identifiedby '123456' ; |
2 | QueryOK,0rowsaffected(0.00sec) |
3 |
4 | MySQL>flushprivileges; |
5 | QueryOK,0rowsaffected(0.00sec) |
1 | C:\MySQL\bin>MySQL.exe -uroot -p123456-h192.168.1.200-P3306 |
2 | WelcometotheMySQLmonitor.Commandsendwith;or\g. |
3 | YourMySQLconnectionidis224 |
4 | Serverversion:5.0.89 -log Sourcedistribution |
5 |
6 | Type 'help;' or '\h' forhelp. Type '\c' to clear thecurrentinputstatement. |
7 |
8 | MySQL> |
※在windows客户端一直去pingVIP,然后关闭192.168.1.201上的keepalived,正常情况下VIP就会切换到192.168.1.202上面去
※开启192.168.1.201上的keepalived,关闭192.168.1.202上的keepalived,看是否能自动切换,正常情况下VIP又会属于192.168.1.201
注:keepalived切换速度还是非常块的,整个切换过程只需1-3秒
●MySQL故障转移测试
※在192.168.1.201上关闭MySQL服务,看VIP是否会切换到192.168.1.202上
※开启192.168.1.201上的MySQL和keepalived,然后关闭192.168.1.202上的MySQL,看VIP是否会切换到192.168.1.201上
下面是用windows客户端连接的MySQL的VIP,在切换时我执行了一个MySQL查询命令,从执行showdatabases到显示出结果时间为3-5秒(大家可以看到上面有个错误提示,不过不用担心,因为我们的keepalived切换大概为3秒左右,这3秒左右VIP是谁都不属于的)
01 | MySQL>showdatabases; |
02 | ERROR2006(HY000):MySQLserverhasgoneaway |
03 | Noconnection.Tryingtoreconnect... |
04 | Connectionid:592 |
05 | Currentdatabase:***NONE*** |
06 |
07 | +--------------------+ |
08 | |Database| |
09 | +--------------------+ |
10 | |information_schema| |
11 | |MySQL| |
12 | |test| |
13 | +--------------------+ |
14 | 3rowsin set (9.01sec) |
相关文章推荐
- NopCommerce架构分析之(五)Model绑定Action参数
- 一些常用的IOS开发网站
- NopCommerce架构分析之(四)基于路由实现灵活的插件机制
- 检测通过什么平台打开网站
- NopCommerce架构分析(一)Autofac依赖注入类生成容器
- 秒杀系统架构优化思路
- 网站
- IIS6.0发布网站(二)
- android 蓝牙代码架构及其uart 到rfcomm流程
- 2.2.1 postfix的体系架构——postfix的模块化设计原理
- 5款最好的免费在线网站CSS验证器
- 我与即时通讯- 企业即时通讯(IM)服务架构
- 为什么需要架构图,怎么画?
- 网站——循环图片切换效果(最近做网站,已经快炸了)
- WEB服务器4--IIS中网站、Web应用程序和虚拟目录
- ZooKeeper集群的安装、配置、高可用测试
- 在网站插入优酷播放器的方法
- 第75课:Spark SQL基于网站Log的综合案例实战每天晚上20:00YY频道现场授课频道68917580
- Windows Server 2008 -2003 数据存储层的数据高可用保证
- 手机访问网站自动跳转到手机版