简介
MMM(Master-Master replication manager for MySQL)是一套支持双主故障切换和双主日常管理的脚本程序。MMM使用Perl语言开发,主要用来监控和管理MySQL Master-Master(双主)复制,虽然叫做双主复制,但是业务上同一时刻只允许对一个主进行写入,另一台备选主上提供部分读服务,以加速在主主切换时刻备选主的预热,可以说MMM这套脚本程序一方面实现了故障切换的功能,另一方面其内部附加的工具脚本也可以实现多个slave的read负载均衡。
MMM提供了自动和手动两种方式移除一组服务器中复制延迟较高的服务器的虚拟ip,同时它还可以备份数据,实现两节点之间的数据同步等。由于MMM无法完全的保证数据一致性,所以MMM适用于对数据的一致性要求不是很高,但是又想最大程度的保证业务可用性的场景。对于那些对数据的一致性要求很高的业务,非常不建议采用MMM这种高可用架构。
MMM项目来自 Google:http://code.google.com/p/mysql-master-master
官方网站为:http://mysql-mmm.org
系统环境
主机名 | 操作系统 | IP地址 | VIP地址 | 角色 |
---|---|---|---|---|
mysql-mmm服务器 | CentOS 7.0 x86_64 | 192.168.96.169 | ||
master01服务器 | CentOS 7.0 x86_64 | 192.168.96.166 | 192.168.96.170 | db1 |
master02服务器 | CentOS 7.0 x86_64 | 192.168.96.165 | 192.168.96.170 | db2 |
slave01服务器 | CentOS 7.0 x86_64 | 192.168.96.167 | 192.168.96.171 | db3 |
slave02服务器 | CentOS 7.0 x86_64 | 192.168.96.168 | 192.168.96.172 | db4 |
- 所有服务器均关闭防火墙及Selinux
systemctl stop firewalld setenforce 0
开始部署
一、master01、master02、slave01、slave02四台mysql服务器操作一致,步骤如下
1.选择使用阿里云数据源(国内源速度快)
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
2.安装epel-release源
yum -y install epel-release
3.清空原缓存并生成新的缓存文件
yum clean all && yum makecache
4.安装mariadb数据库和mysql-mmm软件包
yum -y install mariadb-server mariadb mysql-mmm*
5.修改mariadb数据库主配置文件
vi /etc/my.cnf
[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
6.将配置文件传输到其它3台数据库服务器上
scp /etc/my.cnf [email protected]:/etc/
scp /etc/my.cnf [email protected]:/etc/
scp /etc/my.cnf [email protected]:/etc/
注意:分别修改mysql-master02、mysql-slave01、mysql-slave01的配置文件server_id参数
7.启动mariadb服务
systemctl enable mariadb
systemctl start mariadb
二、配置主主复制(master01、master02主服务器相互同步)
master01服务器
1.登陆mariadb
mysql
2.记录master02的日志文件名称和偏移值
show master status;
3.为master02授予从的权限
grant replication slave on *.* to 'replication'@'192.168.96.%' identified by '123456';
change master to master_host='192.168.96.165',master_user='replication',master_password='123456',master_log_file='mysql_bin.000001',master_log_pos=335;
4.开启同步
start slave;
5.查看master01的服务器同步状态
show slave status\G;
master02主服务器
1.登陆mariadb
mysql
2.记录master01的日志文件名称和偏移值
show master status;
3.为mysql-master01授予从的权限
grant replication slave on *.* to 'replication'@'192.168.96.%' identified by '123456';
change master to master_host='192.168.96.166',master_user='replication',master_password='123456',master_log_file='mysql_bin.000001',master_log_pos=335;
4.开启同步
start slave;
5.查看从服务器同步状态
show slave status\G;
6.master01服务器上创建数据库测试主主同步
#master01服务器上创建数据库并去master02服务器上检查是否也有了该数据库
create database db_test;
主从复制(slave01、slave02两台服务器操作一样(重复第1至4步))
1.登陆mariadb
mysql
2.在两台从mysql服务器上操作(注意master01的日志文件和偏移量参数)
change master to master_host='192.168.96.166',master_user='replication',master_password='123456',master_log_file='mysql_bin.000002',master_log_pos=335;
3.开启同步
start slave;
4.查看从服务器同步状态
show slave status\G;
slave01从服务器:
slave02从服务器:
5.创建数据库测试主主同步
#在任意一台主服务器上创建数据库,再去检查其他三台数据库是否同步了
create database db_01;
三、安装mysql-MMM服务器
1.选择使用阿里云数据源(国内速度快)
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
2.安装epel-release源
yum -y install epel-release
3.清空原缓存并生成新的缓存文件
yum clean all && yum makecache
4.安装mariadb数据库和mysql-mmm软件包
yum -y install mariadb-server mariadb mysql-mmm*
5.编辑mysql-mmm的配置文件
cd /etc/mysql-mmm/
vi mmm_common.conf
1 active_master_role writer
2
3
4 cluster_interface ens33
5 pid_path /run/mysql-mmm-agent.pid
6 bin_path /usr/libexec/mysql-mmm/
7 replication_user replication
8 replication_password 123456
9 agent_user mmm_agent
10 agent_password 123456
11
12
13
14 ip 192.168.96.166
15 mode master
16 peer db2
17
18
19
20 ip 192.168.96.165
21 mode master
22 peer db1
23
24
25
26 ip 192.168.96.167
27 mode slave
28
29
30
31 ip 192.168.96.168
32 mode slave
33
34
35
36 hosts db1, db2
37 ips 192.168.96.170
38 mode exclusive
39
40
41
42 hosts db3, db4
43 ips 192.168.96.171, 192.168.96.172
44 mode balanced
45
6.所有的服务器中mmm_common.conf文件一样,所以这里直接传输到每一台主机上
scp mmm_common.conf [email protected]:/etc/mysql-mmm/ #master02服务器
scp mmm_common.conf [email protected]:/etc/mysql-mmm/ #master01服务器
scp mmm_common.conf [email protected]:/etc/mysql-mmm/ #slave01服务器
scp mmm_common.conf [email protected]:/etc/mysql-mmm/ #slave02服务器
7.在monitor服务器上编辑mmm_mon.conf配置文件
cd /etc/mysql-mmm/
vi mmm_mon.conf
include mmm_common.conf
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.96.165 , 192.168.96.166 192.168.96.167, 192.168.96.168 #mysql数据库服务器地址
auto_set_online 10
# The kill_host_bin does not exist by default, though the monitor will
# throw a warning about it missing. See the section 5.10 "Kill Host
# Functionality" in the PDF documentation.
#
# kill_host_bin /usr/libexec/mysql-mmm/monitor/kill_host
#
monitor_user mmm_monitor #监控机用户名
monitor_password 123456 #密码
debug 0
8.启动mysql-mmm-monitor
systemctl start mysql-mmm-monitor
四、配置四台mysql服务器的mysql-mmm代理
1.在四台mysql服务器上为mmm_agent授权
grant super, replication client, process on *.* to 'mmm_agent'@'192.168.96.%' identified by '123456';
2.在所有数据库上为mmm_moniter授权
grant replication client on *.* to 'mmm_monitor'@'192.168.96.%' identified by '123456';
3.刷新权限
flush privileges;
4.修改所有数据库的mmm_agent.conf
vi /etc/mysql-mmm/mmm_agent.conf
#重要提醒:按顺序分别修改其他MySQL服务器为db2、db3、db4
this db1
5.在所有mysql服务器上启动mysql-mmm-agent服务
systemctl enable mysql-mmm-agent
systemctl start mysql-mmm-agent
五、再回到mysql-mmm服务器上
1.查看各节点的情况
mmm_control show
db1(192.168.96.166) master/ONLINE. Roles: writer(192.168.96.170)
db2(192.168.96.165) master/ONLINE. Roles:
db3(192.168.96.167) slave/ONLINE. Roles: reader(192.168.96.172)
db4(192.168.96.168) slave/ONLINE. Roles: reader(192.168.96.171)
2.检查所有选项
mmm_control checks all
#检查的所有项目均为OK就代表没有问题
db4 ping [last change: 2018/07/12 00:09:29] OK
db4 mysql [last change: 2018/07/11 22:15:13] OK
db4 rep_threads [last change: 2018/07/11 22:05:28] OK
db4 rep_backlog [last change: 2018/07/11 22:05:28] OK: Backlog is null
db2 ping [last change: 2018/07/11 22:05:28] OK
db2 mysql [last change: 2018/07/11 22:05:28] OK
db2 rep_threads [last change: 2018/07/11 22:05:28] OK
db2 rep_backlog [last change: 2018/07/11 22:05:28] OK: Backlog is null
db3 ping [last change: 2018/07/11 22:05:28] OK
db3 mysql [last change: 2018/07/11 22:13:59] OK
db3 rep_threads [last change: 2018/07/11 22:05:28] OK
db3 rep_backlog [last change: 2018/07/11 22:05:28] OK: Backlog is null
db1 ping [last change: 2018/07/11 22:05:28] OK
db1 mysql [last change: 2018/07/11 22:14:54] OK
db1 rep_threads [last change: 2018/07/11 22:05:28] OK
db1 rep_backlog [last change: 2018/07/11 22:05:28] OK: Backlog is null
3.该命令可以手动切换主服务器
mmm_control move_role writer db2
六、模拟故障测试
1. 停止db1服务器的mariadb服务,再检查状态(虚拟地址:192.168.96.170 是否移动到 db2 上)
systemctl stop mariadb
2. 再查看mysql-mmm服务器状态
mmm_control show
db1(192.168.96.166) master/HARD_OFFLINE. Roles:
db2(192.168.96.165) master/ONLINE. Roles: writer(192.168.96.170)
db3(192.168.96.167) slave/ONLINE. Roles: reader(192.168.96.172)
db4(192.168.96.168) slave/ONLINE. Roles: reader(192.168.96.171)
这里db1的虚拟ip地址已经漂移到 了db2上
3. 暂停db3服务器的mariadb服务,再检查状态
systemctl stop mariadb
4. 再查看mysql-mmm服务器状态
mmm_control show
db1(192.168.96.166) master/HARD_OFFLINE. Roles:
db2(192.168.96.165) master/ONLINE. Roles: writer(192.168.96.170)
db3(192.168.96.167) slave/HARD_OFFLINE. Roles:
db4(192.168.96.168) slave/ONLINE. Roles: reader(192.168.96.171), reader(192.168.96.172)
恢复db1、db3服务器,再检查状态
mmm_control show
db1(192.168.96.166) master/ONLINE. Roles:
db2(192.168.96.165) master/ONLINE. Roles: writer(192.168.96.170)
db3(192.168.96.167) slave/ONLINE. Roles: reader(192.168.96.171)
db4(192.168.96.168) slave/ONLINE. Roles: reader(192.168.96.172)
这里注意,当db1服务器恢复,并不会抢占vip地址,而是作为备用机在等待着,好了,写到这里结束了。