Mysql-mmm集群技术概述
概述:
MMM(Master-Master replication manager for MySQL)是一套支持双主故障切换和双主日常管理的脚本程序。MMM使用Perl语言开发,主要用来监控和管理MySQL Master-Master(双主)复制,可以说是mysql主主复制管理器。
虽然叫做双主复制,但是业务上同一时刻只允许对一个主进行写入,另一台备选主上提供部分读服务,以加速在主主切换时刻备选主的预热,可以说MMM这套脚本程序一方面实现了故障切换的功能,另一方面其内部附加的工具脚本也可以实现多个slave的read负载均衡。
MMM提供了自动和手动两种方式移除一组服务器中复制延迟较高的服务器的虚拟ip,同时它还可以备份数据,实现两节点之间的数据同步等。由于MMM无法完全的保证数据一致性,所以MMM适用于对数据的一致性要求不是很高,但是又想最大程度的保证业务可用性的场景。
Mysql-mmm优缺点:
- 优点:高可用性,扩展性好,出现故障自动切换,对于猪猪同步,在同一时间内只提供一台数据库写操作,保证数据的一致性
- 缺点:Monitor节点是单点,可以结合Keepalived实现高可用,对于主机的数量有要求,需要实现读写分离,对程序来说是个挑战
Mysql-mmm内部工作架构;
进程类型
- mmm_mond:监控进程,负责所有的监控工作,决定和处理所有节点角色活动。此脚本需要在监管机上运行;
- mmm_agentd:运行在每个mysql服务器上(Master和Slave)的代理进程,完成监控的探针工作和执行简单的远端服务设置。此脚本需要在被监管机上运行;
- mmm_control:一个简单的脚本,提供管理mmm_mond进程的命令;
工作架构
工作原理
- mysql-mmm的监管端会提供多个虚拟IP(VIP),包括一个可写VIP,多个可读VIP;
- 通过监管的管理,这些IP会绑定在可用mysql之上;
- 当某一台mysql宕机时,监管会将VIP迁移至其他mysql;
案例搭建Mysql-mmm+mysql 5.6双主高可用集群
案例环境
- 部署master1节点的ntp服务
- 配置master2、slave1、slave2、monitor节点同步ntp时间及域名解析
- 分别在master1、master2、slave1、slave2节点上安装mysql服务
- 配置master1实现双主复制
配置master2实现双主复制 - 配置slave1和slave2实现主从复制
- 分别在master1、master2、slave1、slave2四个节点安装mysql-mmm软件程序并且配置agent指定本机的节点名称
- 在master1节点上授权monitor节点连接数据库集群
- 安装monitor节点上的mysql-mmm软件程序
- 配置 monitor节点上的mysql-mmm的配置文件并且复制到各个mysql节点
- 启动master1、master2、slave1、slave2四个节点上的mysql-mmm服务
- 启动monitor节点上的mysql-mmm服务并查看集群状态
- 修改Atlas读写分离配置文件
- 测试群集
案例搭建
配置四台mysql
192.168.137.95
/etc/my.cnf
[mysqld]
server_id = 11
log_bin = master-bin
log-slave-updates = true
auto-increment-increment = 2
auto-increment-offset = 1
192.168.137.99
[mysqld]
server_id = 44
log_bin = master-bin
log-slave-updates = true
auto-increment-increment = 2
auto-increment-offset = 2
192.168.137.96
[mysqld]
server_id = 22
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index
log-slave-updates = true
192.168.137.97
[mysqld]
server_id = 33
relay-log = relay-log-bin
relay-log-index = slave-relay-bin.index
log-slave-updates = true
配置master1、master2的主主模式
主服务器1、2均设置:
GRANT REPLICATION SLAVE ON *.* TO 'master'@'192.168.137.%' IDENTIFIED BY '123456';
FLUSH PRIVILEGES;
【Master2:】
mysql> change master to master_host='192.168.137.95',
master_user='master',
master_password='123456',
master_log_file='master-bin.000001',
master_log_pos=421;
mysql> start slave;
【Master1:】
mysql> change master to master_host='192.168.137.99',
master_user='master',
master_password='123456',
master_log_file='master-bin.000001',
master_log_pos=420;
mysql> start slave;
验证主从状态 mysql> show slave status\G;
配置mysql3、mysql4作为mysql1的从库
mysql> change master to master_host='192.168.137.95',
master_user='master',
master_password='123456',
master_log_file='master-bin.000001',
master_log_pos=421;
mysql> start slave;
安装配置mysql-mmm
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum install -y mysql-mmm*
在mysql1、2、3、4节点授权monitor访问
grant replication client on *.* to 'mmm_monitor'@'192.168.137.%' identified by 'monitor';
grant super,replication client,process on *.* to 'mmm_agent'@'192.168.9.%' identified by 'agent';
FLUSH PRIVILEGES;
修改5台主机的/etc/mysql-mmm/mmm_common.con
active_master_role writer
<host default>
cluster_interface ens33
pid_path /run/mysql-mmm-agent.pid
bin_path /usr/libexec/mysql-mmm/
replication_user master
replication_password 123456
agent_user mmm_agent
agent_password agent
</host>
<host db1>
ip 192.168.137.95
mode master
peer db2
</host>
<host db2>
ip 192.168.137.99
mode master
peer db1
</host>
<host db3>
ip 192.168.137.96
mode slave
</host>
<host db4>
ip 192.168.137.97
mode slave
</host>
<role writer>
hosts db1, db2
ips 192.168.137.250
mode exclusive
</role>
<role reader>
hosts db3, db4
ips 192.168.137.254, 192.168.137.252
mode balanced
</role>
在四台mysql服务器上修改/etc/mysql-mmm/mmm_agent.conf
include mmm_common.conf
# The 'this' variable refers to this server. Proper operation requires
# that 'this' server (db1 by default), as well as all other servers, have the
# proper IP addresses set in mmm_common.conf.
this db1 #分别修改为db1,db2,db3,db4
在监控主机上修改/etc/mysql-mmm/mmm_mon.conf
include mmm_common.conf
<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.137.95,192.168.137.99,192.168.137.96,192.168.137.97
auto_set_online 60
# 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>
<host default>
monitor_user mmm_monitor
monitor_password monitor
</host>
debug 0
启动代理
msql1:systemctl start mysql-mmm-agent
systemctl enable mysql-mmm-agent
msql2:systemctl start mysql-mmm-agent
msql3:systemctl start mysql-mmm-agent
msql4:systemctl start mysql-mmm-agent
监控:systemctl start mysql-mmm-monitor
systemctl enable mysql-mmm-monitor
配置Atlas读写分离
- 服务器1、2、3、4均设置:
grant all on *.* to test@'192.168.137.%' identified by '123456';
flush privileges;
- 修改读写分离配置文件
[mysql-proxy]
#带#号的为非必需的配置项目
#管理接口的用户名
admin-username = user
#管理接口的密码
admin-password = pwd
#Atlas后端连接的MySQL主库的IP和端口,可设置多项,用逗号分隔
proxy-backend-addresses = 192.168.137.250:3306
#Atlas后端连接的MySQL从库的IP和端口,@后面的数字代表权重,用来作负载均衡,若省略则默认为1,可设置多
项,用逗号分隔
proxy-read-only-backend-addresses = 192.168.137.96:3306@1,192.168.137.97:3306@1
#用户名与其对应的加密过的MySQL密码,密码使用PREFIX/bin目录下的加密程序encrypt加密,下行的user1和user2为示例,将其替换为你的MySQL的用户名和加密密码!
pwds = test:tF5TeinkMj8=
#设置Atlas的运行方式,设为true时为守护进程方式,设为false时为前台方式,一般开发调试时设为false,线>上运行时设为true,true后面不能有空格。
daemon = true
...
- 重启读写分离
/usr/local/mysql-proxy/bin/mysql-proxyd test restart
测试群集
[root@localhost ~]# mmm_control show
db1(192.168.137.95) master/ONLINE. Roles: writer(192.168.137.250)
db2(192.168.137.99) master/ONLINE. Roles:
db3(192.168.137.96) slave/ONLINE. Roles: reader(192.168.137.251)
db4(192.168.137.97) slave/ONLINE. Roles: reader(192.168.137.252)