转载地址:http://database.51cto.com/art/201012/237204.htm
今天我们将介绍利用keepalived构建高可用MySQL-HA,保证两台MySQL数据的一致性,然后用keepalived实现虚拟IP,通过keepalived自带的服务监控功能来实现MySQL故障时自动切换。
AD:
关于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版本:CentOS 5.4
6. MySQL版本:5.0.89
7. Keepalived版本:1.1.20
一、MySQL master-master配置
1、修改MySQL配置文件
两台MySQL均如要开启binlog日志功能,开启方法:在MySQL配置文件[MySQLd]段中加上log-bin=MySQL-bin选项
两台MySQL的server-ID不能一样,默认情况下两台MySQL的serverID都是1,需将其中一台修改为2即可
2、将192.168.1.201设为192.168.1.202的主服务器
在192.168.1.201上新建授权用户
1. MySQL> grant replication slave on *.* to 'replication'@'%' identified by 'replication';
2. Query OK, 0 rows affected (0.00 sec)
3.
4. MySQL> show master status;
5. +------------------+----------+--------------+------------------+
6. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
7. +------------------+----------+--------------+------------------+
8. | MySQL-bin.000003 | 374 | | |
9. +------------------+----------+--------------+------------------+
10. 1 row in set (0.00 sec)
在192.168.1.202上将192.168.1.201设为自己的主服务器
1. MySQL> change master to master_host='192.168.1.201',master_user='replication',master_password='replication',master_log_file='MySQL-bin.000003',master_log_pos=374;
2. Query OK, 0 rows affected (0.05 sec)
3.
4. MySQL> start slave;
5. Query OK, 0 rows affected (0.00 sec)
6.
7. MySQL> show slave status\G
8. *************************** 1. row ***************************
9. Slave_IO_State: Waiting for master to send event
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. 1 row in set (0.00 sec)
3、将192.168.1.202设为192.168.1.201的主服务器
在192.168.1.202上新建授权用户
1. MySQL> grant replication slave on *.* to 'replication'@'%' identified by 'replication';
2. Query OK, 0 rows affected (0.00 sec)
3.
4. MySQL> show master status;
5. +------------------+----------+--------------+------------------+
6. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
7. +------------------+----------+--------------+------------------+
8. | MySQL-bin.000003 | 374 | | |
9. +------------------+----------+--------------+------------------+
10. 1 row in set (0.00 sec)
在192.168.1.201上,将192.168.1.202设为自己的主服务器
1. MySQL> change master to master_host='192.168.1.202',master_user='replication',master_password='replication',master_log_file='MySQL-bin.000003',master_log_pos=374;
2. Query OK, 0 rows affected (0.05 sec)
3.
4. MySQL> start slave;
5. Query OK, 0 rows affected (0.00 sec)
6.
7. MySQL> show slave status\G
8. *************************** 1. row ***************************
9. Slave_IO_State: Waiting for master to send event
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. 1 row in set (0.00 sec)
4、MySQL同步测试
如上述均正确配置,现在任何一台MySQL上更新数据都会同步到另一台MySQL,MySQL同步在此不再演示