使用MMM实现MySQL Replication的高可用性
一.MMM介绍文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-mmm/
MMM(master-master replication manager for mysql)是一套灵活的脚本,用来监控,故障切换和管理mysql主主复制。同时还可以实现多个从服务器之间read负载均衡。还可以将复制延时较高的一台服务器的虚拟IP移除,数据备份,再文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-mmm/
同步等等。文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-mmm/
MMM的主要功能通过一下三个脚本来实现:文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-mmm/
mmm_mond:监控进程,监控所有服务器,决定节点角色。文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-mmm/
mmm_agentd:代理进程,运行在每台mysql服务器上,为监控节点提供简单的远程服务。文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-mmm/
mmm_control:为mmm_mond提供管理命令文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-mmm/
二.MMM需求文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-mmm/
host:n+1 一台或多台安装mysql,一台安装MMM monitor文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-mmm/
IP:2*(n+1) 每台主机一个ip,一个ip写角色,n个ip读角色每台文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-mmm/
monitor user:MMM monitor需要replication client权限文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-mmm/
agent user:MMM agent需要super,replication client,process权限文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-mmm/
replication user:复制权限replication slave文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-mmm/
tools user:MMM tools需要super,replication client,reload权限文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-mmm/
monitor主机需要软件环境:文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-mmm/
Perl需支持ithreads文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-mmm/
fping(如果以非root用户运行mmm_mond)文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-mmm/
Perl 模块Algorithm::Diff;Class:Singleton;DBI and DBD::mysql;File::Basename;File::Temp; Log::Dispatch;Log::Log4perl;Mail::Send;Net::Ping;Proc::Daemon;Thread::Queue; Time::HiRes文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-mmm/
所有节点需求:文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-mmm/
所有mysql服务器设置read-only=1。MMM将active_master_role主机的read-only设置为0。文章源自运维生存时间-https://www.ttlsa.com/mysql/mysql-mmm/
Perl,iproute,send_arp(solaris),perl模块(Algorithm::Diff; DBI and DBD::mysql;File::Basename;File::stat;Log::Dispatch;Log::Log4perl;Mail::Send;Net::ARP (linux);Proc::Daemon; Time::HiRes)
MMM tools附加需求:
如果想使用MMM tools(mmm_backup,mmm_restore,mmm_clone),需要使用LVM来存放mysql数据和日志。
还需要以下perl模块:Path::Class; Data::Dump
三.MMM monitor
1.状态
ONLINE 运行状态
ADMIN_OFFLINE 人工设置为离线状态
HARD_OFFLINE 离线状态(ping/mysql失败)
AWAITING_RECOVERY 主机将恢复在线
REPLICATION_DELAY 复制延时大(rep_backlog检测失败)
REPLICATION_FAIL 复制线程没有运行(rep_threads检测失败)
只要主机处在ONLINE才能获得角色。当从ONLINE状态切换到其他状态时,角色将移除。
当主机处在REPLICATION_DELAY或REPLICATION_FAIL状态,一旦恢复,将切换到ONLINE状态。除非抖动不稳定。
主机在HARD_OFFLINE状态,如果所有的问题都解决了,那么将会切换到AWAITING_RECOVERY状态。如果它故障时间小于60s,并且它没有重启或者auto_set_online>0,那么将会被自动切换到ONLINE状态,除非抖动不稳定。
活动的主服务器复制延时或复制失败将不被视为一个问题。因此活动的主服务器状态不会被置于REPLICATION_DELAY或REPLICATION_FAIL。
在节点被切换到ONLINE状态的60s内,如果复制延时或复制失败将会被忽略(默认时间为master-connect-retry值)。
如果rep_backlog和rep_threads都检测失败,将会切换到REPLICATION_FAIL状态。
2.角色
exclusive角色:互斥角色只有一个ip,并且同一时间只能分配给一个主机。可以指定一个首选主机,如果这个主机是ONLINE状态,那么角色将被赋予到这个主机。注意:不能移动被分配到首选主机的角色,因为他们将立刻再次被移动回
到它。
balanced角色:负载均衡角色可以有多个ip。没有一个主机可以比其他主机多出两个角色。
3.检测
mmm_mond对每个主机检测4项决定是否ok
ping 主机是否存活
mysql mysqld进程是否存活
rep_threads 复制线程是否运行
rep_backlog 延时少
抖动
mmm_mond 支持抖动检测。如果一个主机频繁从ONLINE切换到HARD_OFFLINE或者REPLICATION_FAIL或者REPLICATION_DELAY状态,每次切换到ONLINE状态(auto_set_online或者down的时间小于60s),将会导致角色的切换非常频繁。
为了避免这种情况mmm_mond内建了flap检测,可以通过配置文件配置。如果一个主机在flap_duration时间内宕掉了flap_count次,就认为主机处理flap状态,就不会自动被设置为ONLINE状态,将一直处于AWAITING_RECOVERY状态除非手
动设置online(mmm_control set online host)。如果auto_set_online>0,处于flapping的主机在flap_duration时间后将自动设置为ONLINE状态。
5.模式
active mode:Monitor将会自动的把角色从失败的主机上移除,并切换到其他主机上。
manual mode:Moniter会自动把负载均衡的角色分配给对应主机,但是不会自动的把角色从失败的主机上移除。可以通过move_role来手工移除。
wait mode:类似manual模式,但是当两个master都是online状态或者超过了wait_for_other_master的时间,将被切换为ACTIVE模式。
passive mode:在此模式下,monitor不会改变角色,不更新状态文件和发送任何信息给mmm agents。在此模式下可以使用set_ip来改变roles,但是这些改变在monitor切换到ACTIVE或者MANUAL模式(set_active or set_manual)前都不会
生效。在启动时检测到角色发生冲突将会进入被动模式。
6.角色切换过程
Standard role:
虚拟IP从原来的主机上移除
虚拟IP被配置到新主机上
新主机发送arp包,通知其他主机它的新IP
Active master role:
写角色从旧的主服务器上移除:
mysql设置为read_only模式
删除活动的连接
删除虚拟IP
slaves are informed They:
尽可能完成原来主机上的复制工作
切换master到新的主机上
添加写角色到新的主服务器上:
mysql设置为可写模式
配置虚拟IP
四.MMM control
可以通过mmm_control命令控制monitor守护进程,如果有多个集群,需要指定想要控制的集群名称(例如:检测C1的状态使用 “mmm_control @C1 show”),如果没有多个MMM集群,就可以忽略集群名。
ping
检测monitor是否存活
show
查看当前集群状况
checks [host|all [check|all]]
查看指定指定状态
set_online host
把节点的状态从AWAITING_RECOVERY或ADMIN_OFFLINE恢复到ONLINE。
set_offline host
为了维护将一个节点手动的置为ADMIN_OFFLINE状态,这个节点上的所有角色将会被移除,并且停止mysql复制。
mode
查看当前模式。
set_active
切换monitor到active模式
set_manual
切换monitor到manual模式
set_passive
切换monitor到passive模式
move_role role host
在集群节点间切换exclusive角色。在passive模式下无效。
move_role --force role host
可以将active_master_role切换到处在REPLICATION_FAIL或REPLICATION_DELAY状态的主机上。在passive模式下无效。
set_ip ip host
可以在passive模式下,维护角色。当monitor切换到ACTIVE或MANUAL模式下后才生效。
五.环境如下
monitor:192.168.1.189
master db1 db2:192.168.1.101 192.168.1.103
db-writer ip:192.168.1.111
db-read ip:192.168.1.112 192.168.1.113
七.配置MMM
mysql> GRANT FILE, REPLICATION SLAVE ON *.* TO 'repl'@'192.168.1.%' IDENTIFIED BY 'replpw'
mysql> GRANT PROCESS, SUPER, REPLICATION CLIENT ON *.* TO 'mmm_agent'@'192.168.1.%' identified by "agentpasswd";
主主配置
[db1]
log_bin=mysql-bin
log_bin_index=mysql-bin.index
relay_log=mysql-relay-bin
relay_log_index=mysql-relay-bin.index
binlog_format=mixed
expire_logs_days =10
max_binlog_size = 100M
server-id = 101
auto-increment-increment = 2
auto-increment-offset = 1
[db2]
log_bin=mysql-bin
log_bin_index=mysql-bin.index
relay_log=mysql-relay-bin
relay_log_index=mysql-relay-bin.index
binlog_format=mixed
expire_logs_days =10
max_binlog_size = 100M
server-id = 103
auto-increment-increment = 2
auto-increment-offset = 2
【monitor】
# vi mmm_common.conf
active_master_role writer
<host default>
cluster_interface eth0
pid_path /var/run/mmm_agentd.pid
bin_path /usr/lib/mysql-mmm/
replication_user repl
replication_password replpasswd
agent_user mmm_agent
agent_password agentpasswd
</host>
<host db1>
ip 192.168.1.101
mode master
peer db2
</host>
<host db2>
ip 192.168.1.103
mode master
peer db1
</host>
<role writer>
hosts db1, db2
ips 192.168.1.111
mode exclusive
</role>
<role reader>
hosts db1, db2
ips 192.168.1.112, 192.168.1.113
mode balanced
</role>
# vi mmm_mon.conf
include mmm_common.conf
<monitor>
ip 127.0.0.1
pid_path /var/run/mmm_mond.pid
bin_path /usr/lib/mysql-mmm/
status_path /var/lib/misc/mmm_mond.status
ping_ips 192.168.1.101,192.168.1.103
auto_set_online 60
</monitor>
<host default>
monitor_user mmm_agent
monitor_password agentpasswd
</host>
debug 1 #打开调试信息
启动monitor服务
# /etc/init.d/mysql-mmm-monitor start
【db1】
# vi mmm_common.conf
active_master_role writer
<host default>
cluster_interface eth0
pid_path /var/run/mmm_agentd.pid
bin_path /usr/lib/mysql-mmm/
replication_user repl
replication_password replpasswd
agent_user mmm_agent
agent_password agentpasswd
</host>
<host db1>
ip 192.168.1.101
mode master
peer db2
</host>
<host db2>
ip 192.168.1.103
mode master
peer db1
</host>
<role writer>
hosts db1, db2
ips 192.168.1.111
mode exclusive
</role>
<role reader>
hosts db1, db2
ips 192.168.1.112, 192.168.1.113
mode balanced
</role>
# vi mmm_agent.conf
include mmm_common.conf
this db1
启动agent服务
/etc/init.d/mysql-mmm-agent start
【db2】
# vi mmm_common.conf
active_master_role writer
<host default>
cluster_interface eth0
pid_path /var/run/mmm_agentd.pid
bin_path /usr/lib/mysql-mmm/
replication_user repl
replication_password replpasswd
agent_user mmm_agent
agent_password agentpasswd
</host>
<host db1>
ip 192.168.1.101
mode master
peer db2
</host>
<host db2>
ip 192.168.1.103
mode master
peer db1
</host>
<role writer>
hosts db1, db2
ips 192.168.1.111
mode exclusive
</role>
<role reader>
hosts db1, db2
ips 192.168.1.112, 192.168.1.113
mode balanced
</role>
# vi mmm_agent.conf
include mmm_common.conf
this db2
启动agent服务
/etc/init.d/mysql-mmm-agent start
八.查看状态
# mmm_control show
db1(192.168.1.101) master/ONLINE. Roles: reader(192.168.1.112)
db2(192.168.1.103) master/ONLINE. Roles: reader(192.168.1.113), writer(192.168.1.111)
来自外部的引用