一.MMM概述
1.MMM介绍
MMM(Master-Master replication manager for MySQL,MySQL主主复制管理器)
是一套支持双主故障切换和双主日常管理的脚本程序。MMM 使用 Perl 语言开发,主要用来监控和
管理 MySQL Master-Master (双主)复制,虽然叫做双主复制,但是业务上同一时刻只允许对一
个主进行写入,另一台备选主上提供部分读服务,以加速在主主切换时备选主的预热,可以说
MMM这套脚本程序一方面实现了故障切换的功能,另一方面其内部附加的工具脚本也可以实现多
个 Slave 的 read 负载均衡。
MMM提供了自动和手动两种方式移除一组服务器中复制延迟较高的服务器的虚拟ip,同时它还可
以备份数据,实现两节点之间的数据同步等。由于MMM无法完全保证数据的一致性,所以MMM适
用于对数据的一致性要求不是很高,但是又想最大程度地保证业务可用性的场景。
MMM是一套灵活的脚本程序,基于perl实现,用来对 mysql replication 进行监控和故障迁移,并
能管理 MySQL Master-Master 复制的配置。
2.MMM的架构
关于 MMM 高可用架构的说明如下:
(1)mmm_mon:监控进程,负责所有的监控工作,决定和处理所有节点角色活动。此脚本需要
在监控主机上运行。
(2)mmm_agent:运行在每个MySQL服务器上的代理进程,完成监控的探针工作和执行简单的
远端服务设置。此脚本需要在被监管机上运行。
(3)mmm_control:一个简单的脚本,提供管理 mmm_mon 进程的命令。
(4)mysql-mmm 的监管端会提供多个虚拟 IP(VIP),包括一个可写 VIP,多个可读 VIP,通过
监管的管理,这些 IP 会绑定在可用 MySQL 之上,当某一台 MySQL 宕机时,监管会将 VIP 迁移
至其他 MySQL。
在整个监管过程中,需要在 MySQL 中添加相关授权用户,以便让 MySQL 可以支持监控主机的维护。 授权的用户包括一个 mmm_monitor 用户和一个 mmm_agent 用户。
二.MMM 搭建
1.搭建前准备
准备四台安装有 MySQL 5.7版本数据库的虚拟机和一台做 moniter 服务器
master01(db1) 192.168.10.10 mysql5.7、mysql-mmm
master02(db2) 192.168.10.20 mysql5.7、mysql-mmm
slave01(db3) 192.168.10.30 mysql5.7、mysql-mmm
slave02(db4) 192.168.10.40 mysql5.7、mysql-mmm
monitor 192.168.10.50 mysql-mmm
2.初始化操作
systemctl stop firewalld
setenforce 0
3.搭建 MySQL 多主多从模式
3.1修改 master 配置文件(第一台)
vim /etc/my.cnf #添加[mysqld]下面的配置
......
[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character_set_server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1 #每台 Mysql 主机的 server-id 不能相同
log-error=/usr/local/mysql/data/mysql_error.log #错误日志
general_log=ON #通用查询日志
general_log_file=/usr/local/mysql/data/mysql_general.log
slow_query_log=ON #慢查询日志
slow_query_log_file=mysql_slow_query.log
long_query_time=5
binlog-ignore-db=mysql,information_schema #不需要同步的库名
log_bin=mysql_bin #开启二进制日志用于主从数据复制
log_slave_updates=true #允许slave从master复制数据时可以写入到自己的二进制日志
sync_binlog=1
#"双1设置",MySQL 在每写一次二进制日志时都会同步到磁盘中去
innodb_flush_log_at_trx_commit=1
#"双1设置",每次事务提交时MySQL都会把缓存的数据写入日志文件,并且刷到磁盘中去
auto_increment_increment=2 #自增字段一次递增多少
auto_increment_offset=1 #自增字段的起始值
[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character_set_server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1
log-error=/usr/local/mysql/data/mysql_error.log
general_log=ON
general_log_file=/usr/local/mysql/data/mysql_general.log
slow_query_log=ON
slow_query_log_file=mysql_slow_query.log
long_query_time=5
binlog-ignore-db=mysql,information_schema
log_bin=mysql_bin
log_slave_updates=true
sync_binlog=1
innodb_flush_log_at_trx_commit=1
auto_increment_increment=2
auto_increment_offset=1
![](https://img-blog.csdnimg.cn/img_convert/81ecbdea12ae26a67b81a0bf0ad76b78.png)
3.2把配置文件复制到其它 3 台数据库服务器上并启动服务器
scp /etc/my.cnf root@192.168.80.20:/etc/
scp /etc/my.cnf root@192.168.80.30:/etc/
scp /etc/my.cnf root@192.168.80.40:/etc/
![](https://img-blog.csdnimg.cn/img_convert/b23ef543b388a6ed95b79122b917e200.png)
注意:每台装有数据库的虚拟机的配置文件中的 server_id 都要修改
修改好之后重启 MySQL 服务(四台全部重启)
systemctl restart mysqld
3.3配置主主复制,两台主服务器相互复制
grant replication slave on *.* to 'replication'@'192.168.10.%' identified by '123456';
#在两台主服务器上都执行授予从的权限,从服务器上不需要执行
show master status;
#在两台主服务器上查看,记录日志文件名称和同步点
![](https://img-blog.csdnimg.cn/img_convert/7f6ea6f1c31a192c67e71a78e42760c7.png)
(1)在 master上配置同步(第一台主)
change master to master_host='192.168.10.20', master_user='replication', master_password='123456', master_log_file='mysql_bin.000001', master_log_pos=460;
start slave;
show slave status\G
![](https://img-blog.csdnimg.cn/img_convert/025ef9079dba9eeac4b24c572f2fdbe2.png)
(2))在 master上配置同步(第二台主)
change master to master_host='192.168.10.10', master_user='replication', master_password='123456', master_log_file='mysql_bin.000001', master_log_pos=460;
start slave;
show slave status\G
![](https://img-blog.csdnimg.cn/img_convert/0056f9ebf05582cc017ee5d6e071f913.png)
要确保两台主服务器的 Slave_IO_Running 和 Slave_SQL_Running 后面接的都是 yes !!!
![](https://img-blog.csdnimg.cn/img_convert/1964740abdb4bed281db8e0998b23a12.png)
3.4配置主从复制,在两台从服务器上做
(1)在 slave 上做配置(第一台从)
change master to master_host='192.168.10.10', master_user='replication', master_password='123456', master_log_file='mysql_bin.000001', master_log_pos=460;
start slave;
show slave status\G
![](https://img-blog.csdnimg.cn/img_convert/af3c724b4331b3d6dd42b576599b3480.png)
(2)在 slave 上做配置(第二台从)
change master to master_host='192.168.10.20', master_user='replication', master_password='123456', master_log_file='mysql_bin.000001', master_log_pos=460;
start slave;
show slave status\G
![](https://img-blog.csdnimg.cn/img_convert/0df4a8f02b093d062b024f9d2831a8a6.png)
两台从服务器的 Slave_IO_Running 和 Slave_SQL_Running 后面接的也要都是 yes !!!
![](https://img-blog.csdnimg.cn/img_convert/55e36dbc493137b27096d65e5051d7d0.png)
3.5测试主主、主从 同步情况
create database db_test;
show databases;
![](https://img-blog.csdnimg.cn/img_convert/fd2e1b3046ff06a7462c5845f1d8cf2f.png)
4.安装配置 MySQL-MMM
4.1在所有服务器上安装 MySQL-MMM
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum -y install epel-release
yum -y install mysql-mmm*
![](https://img-blog.csdnimg.cn/img_convert/610b12bacbfa60d15d00263e4a8da9b1.png)
4.2在 master 上对 MySQL-MMM 进行配置(第一台主)
cd /etc/mysql-mmm/
vim mmm_common.conf
...... #修改以下一些配置
<host default>
cluster_interface ens33
......
replication_user replication
replication_password 123456
agent_user mmm_agent
agent_password 123456
<host db1>
ip 192.168.10.10
mode master
peer db1
</host>
<host db2>
ip 192.168.10.20
mode master
peer db2
</host>
<host db3>
ip 192.168.10.30
mode slave
</host>
<host db4>
ip 192.168.10.40
mode slave
</host>
<role writer>
hosts db1, db2
ips 192.168.10.100
mode exclusive #只有一个 host 可以进行写操作模式
</role>
<role reader>
hosts db3, db4
ips 192.168.10.101, 192.168.10.102
mode balanced #多个 slave 主机可以进行读操作模式
</role>
![](https://img-blog.csdnimg.cn/img_convert/8b3662ff95dc087dad64f845383b1026.png)
4.3把配置文件复制到其它 4 台主机,所有主机该配置文件内容都是一样的
scp mmm_common.conf root@192.168.10.20:/etc/mysql-mmm/
scp mmm_common.conf root@192.168.10.30:/etc/mysql-mmm/
scp mmm_common.conf root@192.168.10.40:/etc/mysql-mmm/
scp mmm_common.conf root@192.168.10.50:/etc/mysql-mmm/
![](https://img-blog.csdnimg.cn/img_convert/9fb7995b91e61b3a2db2a086da47b012.png)
4.4修改所有数据库服务器的代理配置文件 mmm_agent.conf
vim /etc/mysql-mmm/mmm_agent.conf
include mmm_common.conf
.......................
this db1 #根据不同的主机分别修改为 db1,db2,db3,db4
![](https://img-blog.csdnimg.cn/img_convert/bc3d15ef2fb0223abaaf656957b7595e.png)
4.5在 monitor 监控服务器上修改监控配置文件 mmm_mon.conf
vim /etc/mysql-mmm/mmm_mon.conf
include mmm_common.conf
<monitor>
.....
ping_ips 192.168.10.10,192.168.10.20,192.168.10.30,192.168.10.40
#指定所有数据库服务器的 IP
auto_set_online 10 #指定自动上线时间
</monitor>
<host default>
monitor_user mmm_monitor #指定 mmm_monitor 的用户名
monitor_password 123456 #指定 mmm_monitor 的密码
</host>
![](https://img-blog.csdnimg.cn/img_convert/9dd3d1d4a8f29bfa853fec3919940424.png)
5.在数据库上进行授权操作
5.1在所有数据库上为 mmm_agent(代理进程)授权
grant replication client on *.* to 'mmm_monitor'@'192.168.10.%' identified by '123456';
5.2在所有数据库上为 mmm_moniter(监控进程)授权
grant replication client on *.* to 'mmm_monitor'@'192.168.10.%' identified by '123456';
flush privileges;
![](https://img-blog.csdnimg.cn/img_convert/c0f30d6b54e58ee00a4ac335b3aabc84.png)
6.启动服务并进行测试
6.1在所有数据库服务器上启动 mysql-mmm-agent
systemctl start mysql-mmm-agent.service
systemctl enable mysql-mmm-agent.service
![](https://img-blog.csdnimg.cn/img_convert/165692e3a8e0369a8891cbad2715bd35.png)
6.2在 monitor 服务器上启动 mysql-mmm-monitor
systemctl start mysql-mmm-monitor.service
![](https://img-blog.csdnimg.cn/img_convert/947c21a11f935523465b1a386c069405.png)
6.3在 monitor 服务器上测试群集
(1)查看各节点的情况
mmm_control show
![](https://img-blog.csdnimg.cn/img_convert/391697ff4a568eb5b77ce4d4b12220e3.png)
(2)检测监控功能是否都完善,需要各种OK
mmm_control checks all
![](https://img-blog.csdnimg.cn/img_convert/8ab426bd1f36808a1981017417efa410.png)
(3)指定绑定 VIP 的主机
mmm_control move_role writer db2
(4)故障测试
mmm_control move_role writer db1
![](https://img-blog.csdnimg.cn/img_convert/662c18c33842ac30699732069ead8c6e.png)
(5)停止 master 确认 VIP 是否移动到 master02 上
systemctl stop mysqld
mmm_control show
![](https://img-blog.csdnimg.cn/img_convert/1657ec1e98c9f5c185640d97c237eea2.png)
注意:master01 主服务器恢复服务后,不会抢占
(6)停止一台从服务器,另一台将接管两个虚拟IP,以保证业务不停止
systemctl stop mysqld
mmm_control show
![](https://img-blog.csdnimg.cn/img_convert/33b03bc691bfe0cb8ee7825c661904cb.png)
7.客户端测试
7.1在 master 服务器上为 monitor 服务器地址授权登录(第一台)
grant all on *.* to 'testdba'@'192.168.10.50' identified by '123456';
flush privileges;
![](https://img-blog.csdnimg.cn/img_convert/78cda7f478adf443b0e48c079064c4d3.png)
7.2在 monitor 服务器上使用 VIP 登录
yum install -y mariadb-server mariadb
systemctl start mariadb.service
mysql -utestdba -p -h 192.168.10.100
#密码123456
![](https://img-blog.csdnimg.cn/img_convert/5d05cdc30d2dc5397bcfc277b9e149ca.png)
7.3创建数据,测试同步情况
create database testdba;
![](https://img-blog.csdnimg.cn/img_convert/58749807dc84763e6859710a783bdea4.png)