mysql MMM高可用方案
MMM简介:
MMM即Master-Master Replication Manager for MySQL(mysql主主复制管理器)关于mysql主主复制配置的监控、故障转移和管理的一套可伸缩的脚本套件(在任何时候只有一个节点可以被写入),这个套件也能对居于标准的主从配置的任意数量的从服务器进行读负载均衡,所以你可以用它来在一组居于复制的服务器启动虚拟ip,除此之外,它还有实现数据备份、节点之间重新同步功能的脚本。
MySQL本身没有提供replication failover的解决方案,通过MMM方案能实现服务器的故障转移,从而实现mysql的高可用。
环境配置
MMM_monitor:172.19.0.122
MySQL_master1:172.19.0.121 (db1)
MySQL_master2:172.19.0.122(db2)
MySQL_slave1:172.19.123(db3)
VIP_Write:172.19.0.126
vip_Read1:172.19.0.127, 172.19.0.128
三台服务安装好mysql,把db1与db2做成主主同步,db3为从,是同步db1的。后面搭建好mmm后,当DB1的脱机后,db3会自动change同步db2。
1.设置hosts解析
三台服务的配置如下
[root@db1 ~]# cat /etc/hosts
172.19.0.121 db1
172.19.0.122 db2
172.19.0.123 db3
创建三个账号。
(复制账号)
GRANT REPLICATION slave,REPLICATION CLIENT ON *.* TO 'repl'@'%' IDENTIFIED BY 'repl';
(代理账号)
GRANT PROCESS,SUPER,REPLICATION CLIENT ON *.* TO 'mmm_agent'@'%' IDENTIFIED BY '123456';
(监听账号)
GRANT REPLICATION CLIENT ON *.* TO 'mmm_monitor'@'%' IDENTIFIED BY '123456';
3.安装mysql-mmm
在三台服务上分别进行安装,安装命令如下:
#wget http://download.fedoraproject.org/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm
#rpm -ivh epel-release-5-4.noarch.rpm
#yum -y install mysql-mmm*
4.配置MMM监控、代理服务
在三台服务器修改mmm_common.conf配置文件
[root@db1 ~]# cd /etc/mysql-mmm/
[root@db1 mysql-mmm]# vi mmm_common.conf
active_master_role writer
<host default>
cluster_interface eth0
pid_path /var/run/mysql-mmm/mmm_agentd.pid
bin_path /usr/libexec/mysql-mmm/
replication_user repl
replication_password repl
agent_user mmm_agent
agent_password 123456
</host>
<host db1>
ip 172.19.0.121
mode master
peer db2
</host>
<host db2>
ip 172.19.0.122
mode master
peer db1
</host>
<host db3>
ip 172.19.0.123
mode slave
</host>
<role writer>
hosts db1, db2
ips 172.19.0.126
mode exclusive
</role>
<role reader>
hosts db1, db2, db3
ips 172.19.0.127,172.19.0.128
mode balanced
</role>
其次,修改三台服务器mmm_agent.conf配置文件
[root@db1 mysql-mmm]# cat 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
[root@db2 mysql-mmm]# cat 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 db2
[root@db3 mysql-mmm]# cat 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 db3
最后修改172.19.0.122(db2) mysql-mon服务配置mmm_mon.conf配置文件,把这台做为MMM_monitor
[root@db2 mysql-mmm]# cat mmm_mon.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
[root@db1 mysql-mmm]# cat mmm_mon.conf
include mmm_common.conf
<monitor>
ip 127.0.0.1
pid_path /var/run/mysql-mmm/mmm_mond.pid
bin_path /usr/libexec/mysql-mmm
status_path /var/lib/mysql-mmm/mmm_mond.status
ping_ips 172.19.0.121,172.19.0.122,172.19.0.123
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>
<host default>
monitor_user mmm_monitor
monitor_password 123456
</host>
debug 0
这样,配置MMM监控,代理服务的工作就完成了
5.启动相关服务
在三台服务器启动以下服务
/etc/init.d/mysql-mmm-agent start
在db2中启动mysql-mon监控服务
/etc/init.d/mysql-mmm-monitor start
查看MMM状态信息
[root@db2 mysql-mmm]# mmm_control show
db1(172.19.0.121) master/ONLINE. Roles: writer(172.19.0.126)
db2(172.19.0.122) master/ONLINE. Roles: reader(172.19.0.127)
db3(172.19.0.123) slave/ONLINE. Roles: reader(172.19.0.128)
[root@db2 mysql-mmm]# mmm_control checks all
db2 ping [last change: 2014/08/22 16:57:07] OK
db2 mysql [last change: 2014/08/22 16:57:07] OK
db2 rep_threads [last change: 2014/08/22 16:57:07] OK
db2 rep_backlog [last change: 2014/08/22 16:57:07] OK: Backlog is null
db3 ping [last change: 2014/08/22 16:57:07] OK
db3 mysql [last change: 2014/08/22 17:11:48] OK
db3 rep_threads [last change: 2014/08/22 16:57:07] OK
db3 rep_backlog [last change: 2014/08/22 16:57:07] OK: Backlog is null
db1 ping [last change: 2014/08/22 16:57:07] OK
db1 mysql [last change: 2014/08/22 16:57:07] OK
db1 rep_threads [last change: 2014/08/22 16:57:07] OK
db1 rep_backlog [last change: 2014/08/22 16:57:07] OK: Backlog is null
以上说明成功,为了数据一致性,我们采用半同步方案
本文转自 jxzhfei 51CTO博客,原文链接:http://blog.51cto.com/jxzhfei/1543409