前言
MMM(Master-Master replication managerfor Mysql,Mysql主主复制管理器)是一套灵活的脚本程序,基于perl实现,用来对mysql replication进行监控和故障迁移,并能管理mysql Master-Master复制的配置(同一时间只有一个节点是可写的)。
MMM
优缺点
优点:高可用性,扩展性好,出现故障自动切换,对于主主同步,在同一时间只提供一台数据库写操作,保证的数据的一致性。
缺点:Monitor节点是单点,可以结合Keepalived实现高可用。
工作原理
mysql-mmm的监管端会提供多个虚拟IP(VIP),包括一个可写VIP,多个可读VIP,通过监管的管理,这些IP会绑定在可用服务器之上,当某一台服务器宕机时,监管会将VIP迁移至其他服务器。
实现过程
实验拓扑

双主一从配置
DB1配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
[root@node1 ~]
datadir = /mydata/data
log-bin= /mydata/binlogs/master-bin
relay-log= /mydata/relaylogs/relay-bin
binlog_format=mixed
server- id = 1
auto_increment_offset=1
auto_increment_increment=2
log_slave_updates = 1
sync_binlog = 1
skip-name-resolve
read_only = 1
[root@node1 ~]
|
授权可用复制用户记录二进制日志位置

DB2配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
[root@node2 ~]
datadir = /mydata/data
log-bin= /mydata/binlogs/master-bin
relay-log= /mydata/relaylogs/relay-bin
binlog_format=mixed
server- id = 11
auto_increment_offset=2
auto_increment_increment=2
log_slave_updates = 1
sync_binlog = 1
skip-name-resolve
read_only = 1
[root@node2 ~]
|
授权可用复制用户记录二进制日志位置

DB3配置
1
2
3
4
5
6
7
8
9
|
[root@scholar ~]
datadir = /mydata/data
relay-log= /mydata/relaylogs/relay-bin
server- id = 111
read_only = 1
skip-name-resolve
[root@scholar ~]
|
连接各服务器
DB1连接DB2

DB2连接DB1

DB3连接DB1

主从测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
MariaDB [(none)]> create database testdb;
Query OK, 1 row affected (0.14 sec)
MariaDB [(none)]> create table testdb.tb1(name char(20) not null);
Query OK, 0 rows affected (0.19 sec)
MariaDB [(none)]> show tables in testdb;
+------------------+
| Tables_in_testdb |
+------------------+
| tb1 |
+------------------+
1 row in set (0.13 sec)
|
数据一致,主从复制部分完成
MMM配置
安装所需程序包
授权监控及代理用户

Monitor配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
[root@scholar ~]
active_master_role writer
<host default>
cluster_interface eth0
pid_path /var/run/mysql-mmm/mmm_agentd .pid
bin_path /usr/libexec/mysql-mmm/
replication_user repluser
replication_password replpass
agent_user mmm_agent
agent_password agent_passw
< /host >
<host db1>
ip 172.16.10.123
mode master
peer db2
< /host >
<host db2>
ip 172.16.10.124
mode master
peer db1
< /host >
<host db3>
ip 172.16.10.125
mode slave
< /host >
<role writer>
hosts db1, db2
ips 172.16.10.30
mode exclusive
< /role >
<role reader>
hosts db1, db2, db3
ips 172.16.10.31,172.16.10.32,172.16.10.33
mode balanced
< /role >
|
1
2
3
4
5
6
7
|
[root@scholar ~]
mmm_common.conf 100% 776 0.8KB /s 00:00
[root@scholar ~]
mmm_common.conf 100% 776 0.8KB /s 00:00
[root@scholar ~]
mmm_common.conf 100% 776 0.8KB /s 00:00
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
[root@scholar ~]
include mmm_common.conf
<monitor>
ip 127.0.0.1
pid_path /var/run/mysql-mmm/mmm_mond .pid
bin_path /usr/libexec/mysql-mmm
status_path /var/lib/mysql-mmm/mmm_mond .status
ping_ips 172.16.10.123,172.16.10.124,172.16.10.125
auto_set_online 60
< /monitor >
<host default>
monitor_user mmm_monitor
monitor_password monitor_pass
< /host >
debug 0
|
DB1-DB3配置
1
2
3
4
5
6
7
8
9
10
|
[root@node1 ~]
include mmm_common.conf
this db1
|
启动服务
1
2
3
4
5
6
|
[root@node1 ~]
Starting MMM Agent Daemon: [ OK ]
[root@scholar ~]
Starting MMM Monitor Daemon: [ OK ]
|
查看当前状态

查看各节点VIP状态

高可用测试
模拟db1故障,查看节点状态

再看db1的VIP状态

VIP已被转移到其他节点,其他方面有兴趣请自行测试,这里就不一一展示了
The end
MySQL/MariaDB基于MMM实现读写分离及高可用实验就先说到这里了,有兴趣的朋友可以继续探究基于Keepalived实现Monitor的高可用,这里就不做深究了,实验过程中遇到问题可留言交流。以上仅为个人学习整理,如有错漏,大神勿喷~~~
本文转自 北城书生 51CTO博客,原文链接:http://blog.51cto.com/scholar/1664551