MMM群集架构概述
MMM(Master-Master replication managerfor Mysql,Mysql主主复制管理器)是一套灵活的脚本程序,基于perl实现,
用来对mysql replication进行监控和故障迁移,并能管理mysql Master-Master复制的配置(同一时间只有一个节点是可写的)。
MMM高可用架构说明
mmm_mond:监控进程,负责所有的监控工作,决定和处理所有节点角色活动。此脚本需要在监管机上运行。
mmm_agentd:运行在每个mysql服务器上的代理进程,完成监控的探针工作和执行简单的远端服务设置。此脚本需要在被监管机上运行。
mmm_control:一个简单的脚本,提供管理mmm_mond进程的命令。
mysql-mmm的监管端会提供多个虚拟IP(VIP),包括一个可写VIP,多个可读VIP,通过监管的管理,这些IP会绑定在可用mysql之上,当某一台mysql宕机时,监管会将VIP迁移至其他mysql。
在整个监管过程中,需要在mysql中添加相关授权用户,以便让mysql可以支持监理机的维护。授权的用户包括一个mmm_monitor用户和一个mmm_agent用户。
MMM的优缺点
优点: 高可用性,扩展性好,出现故障自动切换,对于主主同步,在同一时间只提供一台数据库写操作,保证的数据的一致性。
缺点: Monitor节点是单点,可以结合Keepalived实现高可用,对主机的数量有要求,需要实现读写分离,对程序来说是个挑战。
实验环境部署
服务器角色 IP地址 代理名称 虚拟IP
主服务器1 192.168.142.131 db1 vip192.168.142.130
(master1)
主服务器2 192.168.142.134 db2
(master2)
从服务器1 192.168.142.130 db3 vip192.168.142.251, 192.168.142.252
(slave1)
从服务器2 192.168.142.135 db4
(slave2)
监控服务器 192.168.142.136
(monitor)
第一步:在四台服务器上都需要安装MySQL数据库
1.配置ALI云源,然后安装epel-release源
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
2.搭建本地yum源
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
3.修改ml主配置文件
[root@localhost ~]
[mysqld]
log_error=/var/lib/mysql/mysql.err
log=/var/lib/mysql/mysql_log.log
log_slow_queries=/var/lib/mysql_slow_queris.log
binlog-ignore-db=mysql,information_schema
character_set_server=utf8
log_bin=mysql_bin
server_id=1
log_slave_updates=true
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=1
[root@localhost ~]
[root@localhost ~]
4.复制配置文件到其他三台数据库服务器,注意修改server_id
[root@localhost ~]
5.进入数据库,并查看日志文件信息
[root@localhost ~]
MariaDB [(none)]> show master status;
+------------------+----------+--------------+--------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+--------------------------+
| mysql_bin.000001 | 245| | mysql,information_schema |
+------------------+----------+--------------+--------------------------+
1 row in set (0.00 sec)
6.在m1和m2上互相授予访问的权限,并授权同步日志
MariaDB [(none)]> grant replication slave on *.* to 'replication'@'192.168.142.%' identified by '123456';
MariaDB [(none)]> change master to master_host='192.168.142.134',master_user='replication',master_password='123456',master_log_file='mysql_bin.000001',master_log_pos=245;
MariaDB [(none)]> change master to master_host='192.168.142.131',master_user='replication',master_password='123456',master_log_file='mysql_bin.000001',master_log_pos=245;
7.在m1上开启同步功能
MariaDB [(none)]> start slave;
8.查看同步状态,两台主服务器都要看到Yes
MariaDB [(none)]> show slave status\G;
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
9.在m1上创建数据库
MariaDB [(none)]> create database school;
10.在m2上查看同步数据库
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| school |
| test |
+--------------------+
5 rows in set (0.00 sec)
11.在两台从上做-注意日志文件和位置参数的改变(都指向m1)
MariaDB [(none)]> change master to master_host='192.168.142.131',master_user='replication',master_password='123456',master_log_file='mysql_bin.000001',master_log_pos=245;
12.开启同步功能
MariaDB [(none)]> start slave;
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| school |
| test |
+--------------------+
5 rows in set (0.00 sec)
13.在四台服务器上安装MMM相关软件
[root@localhost ~]
14.配置mmm_common.conf配置文件
[root@localhost ~]
<host default>
cluster_interface ens33
pid_path /run/mysql-mmm-agent.pid
bin_path /usr/libexec/mysql-mmm/
replication_user replicantion
replication_password 123456
agent_user mmm_agent
agent_password 123456
</host>
<host db1>
ip 192.168.142.131
mode master
peer db2
</host>
<host db2>
ip 192.168.142.134
mode master
peer db1
</host>
<host db3>
ip 192.168.142.130
mode slave
</host>
<host db4>
ip 192.168.142.135
mode slave
</host>
<role writer>
hosts db1, db2
ips 192.168.142.250
mode exclusive
</role>
<role reader>
hosts db3, db4
ips 192.168.142.251, 192.168.142.252
mode balanced
</role>
15.从m1上面复制配置文件到其他三台服务器
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
第二步:配置monitor监控服务器
1.安装epel-release源以及MMM相关软件
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
[root@localhost ~]
2.从m1上面复制配置文件到监控服务器
[root@localhost ~]
3.配置mmm_common.conf配置文件
[root@localhost ~]
<monitor>
ip 127.0.0.1
pid_path /run/mysql-mmm-monitor.pid
bin_path /usr/libexec/mysql-mmm
status_path /var/lib/mysql-mmm/mmm_mond.status
ping_ips 192.168.142.131,192.168.142.134,192.168.142.130,192.168.142.135
auto_set_online 10
4.在所有数据库服务器上为mmm_agent授权
MariaDB [(none)]> grant super, replication client, process on *.* to 'mmm_agent'@'192.168.142.%' identified by '123456';
Query OK, 0 rows affected (0.02 sec)
5.在所有数据库服务器上为mmm_moniter授权
MariaDB [(none)]> grant replication client on *.* to 'mmm_monitor'@'192.168.18.%' identified by '123456';
Query OK, 0 rows affected (0.02 sec)
6.刷新数据库
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.02 sec)
7.在所有数据库服务器上设定代理名称
[root@localhost ~]
this db1
this db2
this db3
this db4
8.所有数据库服务器上启动代理功能并设定开机自启动
[root@localhost ~]
[root@localhost ~]
9.在monitor服务器开启监控服务
[root@localhost mysql-mmm]
10.查看各节点的情况
[root@localhost ~]
db1(192.168.142.131) master/ONLINE. Roles: writer(192.168.142.250)
db2(192.168.142.134) master/ONLINE. Roles:
db3(192.168.142.130) slave/ONLINE. Roles: reader(192.168.142.252)
db4(192.168.142.135) slave/ONLINE. Roles: reader(192.168.142.251)
11.检测所有服务器状态
[root@localhost ~]
db4 ping [last change: 2019/11/25 18:23:03] OK
db4 mysql [last change: 2019/11/25 18:23:03] OK
db4 rep_threads [last change: 2019/11/25 18:23:03] OK
db4 rep_backlog [last change: 2019/11/25 18:23:03] OK: Backlog is null
db2 ping [last change: 2019/11/25 18:23:03] OK
db2 mysql [last change: 2019/11/25 18:59:01] OK
db2 rep_threads [last change: 2019/11/25 18:59:01] OK
db2 rep_backlog [last change: 2019/11/25 18:59:01] OK: Backlog is null
db3 ping [last change: 2019/11/25 18:23:03] OK
db3 mysql [last change: 2019/11/25 18:59:01] OK
db3 rep_threads [last change: 2019/11/25 18:59:01] OK
db3 rep_backlog [last change: 2019/11/25 18:59:01] OK: Backlog is null
db1 ping [last change: 2019/11/25 18:23:03] OK
db1 mysql [last change: 2019/11/25 18:59:01] OK
db1 rep_threads [last change: 2019/11/25 18:59:01] OK
db1 rep_backlog [last change: 2019/11/25 18:59:01] OK: Backlog is null
第三步:故障测试
1.模拟m1服务器宕机,停止服务
[root@localhost ~]
2.当m1服务器宕机后,m2接收虚拟IP继续提供服务
[root@localhost ~]
db1(192.168.142.131) master/ONLINE. Roles:
db2(192.168.142.134) master/ONLINE. Roles: writer(192.168.142.250)
db3(192.168.142.130) slave/ONLINE. Roles: reader(192.168.142.252)
db4(192.168.142.135) slave/ONLINE. Roles: reader(192.168.142.251)
3.模拟s1服务器宕机,停止服务
[root@localhost ~]
4.当s1服务器宕机后,s2接收虚拟IP继续提供服务
[root@localhost ~]
db2(192.168.142.134) master/ONLINE. Roles:
db3(192.168.142.130) slave/HARD_OFFLINE. Roles:
db4(192.168.142.135) slave/ONLINE. Roles: reader(192.168.142.251), reader(192.168.142.252
5.在m1服务器上为监控服务器地址授权登录
MariaDB [(none)]> grant all on *.* to 'root'@'192.168.142.136' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
6.在监控服务器上安装数据库客户端
[root@localhost ~]
7.在监控服务器上使用指定用户登录数据库,并创建数据信息
[root@localhost ~]
Enter password:
MariaDB [(none)]> create database BDQN;
Query OK, 1 row affected (0.01 sec)
8.在所有数据库上都能查看及时同步到的数据信息
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| BDQN |
| mysql |
| performance_schema |
| school |
| test |
+--------------------+
6 rows in set (0.00 sec)
到此MMM群集架构已完成,谢谢阅读!!!