您的位置:首页 > 数据库 > SQL

MMM+MySQL Replication 架构的搭建演示

2015-10-08 14:15 716 查看
MMM即Master-Master Replication Manager for MySQL(mysql主主复制管理器)关于mysql主主复制配置的监控、故障转移和管理的一套可伸缩的脚本套件(在任何时候只有一个节点可以被写 入),这个套件也能对居于标准的主从配置的任意数量的从服务器进行读负载均衡,所以你可以用它来在一组居于复制的服务器启动虚拟ip,除此之外,它还有实 现数据备份、节点之间重新同步功能的脚本。

MySQL本身没有提供replication failover的解决方案,通过MMM方案能实现服务器的故障转移,从而实现mysql的高可用。

MMM项目来自 Google:http://code.google.com/p/mysql-master-master

官方网站为:http://mysql-mmm.org

MMM主要功能由下面三个脚本提供

l mmm_mond 负责所有的监控工作的监控守护进程,决定节点的移除等等
l mmm_agentd 运行在mysql服务器上的代理守护进程,通过简单远程服务集提供给监控节点
l mmm_control 通过命令行管理mmm_mond进程.

下面是配置环境:
系统:CentOS 6.5 64位
软件:mysql-5.5.32.tar.gz cmake-2.8.8.tar.gz
防火墙:打开3306 9989
MMM_monitor:10.0.0.210
MySQL_mastre1:10.0.0.210 ----(MySQL-M1)
MySQL_mastre2:10.0.0.211 ----(MySQL-M2)
MySQL_slave1:10.0.0.213 ----(MySQL-S1)
VIP_Write:10.0.0.217---(VIP)
VIP_Read:10.0.0.218---(VIP)

注意:监控节点最好单独放在一台独立服务器上。

一、设置hosts解析
三台服务器的配置如下:

[root@M1 ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.0.0.210 M1
10.0.0.211 M2
10.0.0.213 S1

二、配置MySQL主从服务器
首先创建三个帐号,如下所示

mysql> grant replication slave,replication client on *.* to 'repl'@'%' identified by 'repl';
mysql> grant process,super,replication client on *.* to 'mmm_agent'@'%' identified by '123456';
mysql> grant replication client on *.* to 'mmm_monitor'@'%' identified by '123456';

说明:

第一个帐号repl(复制帐号),是用于主主复制的。

第二个帐号mmm_agent(代理帐号),是MMM代理用来变成只读模式和同步master等的。
第三个帐号mmm_monitor(监听帐号),是MMM监控服务器用来对MySQL服务器做健康检查的。

首先要搭建好 M1<---->M2之前的同步,还要搭建好 S1从库---->M1的同步。
三、安装MySQL-MMM
在三台服务器上分别进行安装,安装命令如下:

wget http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-5.noarch.rpm
rpm -ivh epel-release-6-8.noarch.rpm
yum -y install mysql-mmm*

[root@M1 ~]# rpm -qa|grep mysql-mmm
mysql-mmm-2.2.1-2.el6.noarch
mysql-mmm-agent-2.2.1-2.el6.noarch
mysql-mmm-tools-2.2.1-2.el6.noarch
mysql-mmm-monitor-2.2.1-2.el6.noarch

四、配置MMM监控、代理服务器
1、在三台服务器上(M1,M2,S1)修改mmm_common.conf配置文件(三台服务器的此配置文件内容相同)。

注:如果监控节点是单独的服务器的话,那mmm_common.conf配置文件与三台服务器的相同。如果是拷贝到三台机器上面,要注意这个配置文件的权限(640)。不然无法启动
修改后的内容如下:
路径:

[root@M1 mysql-mmm]# pwd
/etc/mysql-mmm

[root@M1 mysql-mmm]# cat 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 M1>
ip 10.0.0.210
mode master
peer M2
</host>

<host M2>
ip 10.0.0.211
mode master
peer M1
</host>

<host S1>
ip 10.0.0.213
mode slave
</host>

#<host db3>
# ip 192.168.100.51
# mode slave
#</host>

<role writer>
hosts M1,M2
ips 10.0.0.217
mode exclusive #排他模式,在这种情况下任何时候只能有一个主机拥有该角色。
</role>

<role reader>
hosts M1,M2,S1
ips 10.0.0.218
mode balanced #平衡模式,该模式下可以多个主机同时拥有此角色。
</role>
[root@M1 mysql-mmm]#

修改M1,M2,S1三台服务上的mmm_agent.conf配置文件。
修改内容如下:
[root@M1 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 M1

[root@M2 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 M2

[root@S1 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 S1

最后在MON(M1)监控节点上修改配置文件mmm_mon.conf
[root@M1 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 10.0.0.210,10.0.0.211,10.0.0.213 #可以ping的真实代理服务器的IP
auto_set_online 10 #发现节点丢失则过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

五、启动各服务器的相关服务
首先启动M1,M2,S1的客户端
/etc/init.d/mysql-mmm-agent start
然后,在MON(M1)上启动监控服务
/etc/init.d/mysql-mmm-monitor start
最后,在MON(M1)监控服务上查看MMM状态信息:
[root@M1 ~]# mmm_control show
M1(10.0.0.210) master/ONLINE. Roles: writer(10.0.0.217)
M2(10.0.0.211) master/ONLINE. Roles:
S1(10.0.0.213) slave/ONLINE. Roles: reader(10.0.0.218)

[root@M1 ~]# mmm_control checks all
S1 ping [last change: 2015/10/08 10:01:41] OK
S1 mysql [last change: 2015/10/08 10:01:41] OK
S1 rep_threads [last change: 2015/10/08 10:01:41] OK
S1 rep_backlog [last change: 2015/10/08 10:01:41] OK: Backlog is null
M1 ping [last change: 2015/10/08 10:01:41] OK
M1 mysql [last change: 2015/10/08 10:01:41] OK
M1 rep_threads [last change: 2015/10/08 10:01:41] OK
M1 rep_backlog [last change: 2015/10/08 10:01:41] OK: Backlog is null
M2 ping [last change: 2015/10/08 10:01:41] OK
M2 mysql [last change: 2015/10/08 13:10:04] OK
M2 rep_threads [last change: 2015/10/08 10:01:41] OK
M2 rep_backlog [last change: 2015/10/08 10:01:41] OK: Backlog is null

注意:其中M1负责写(IP是10.0.0.217),S1负责读(IP是10.0.0.218)这里特别说明一下,这个读写分离要配合前端程序来用,也就是说你的程序要支持读写分离,要把这个VIP告诉开发人员,让他们在配置文件里指定,MMM软件并不具有读写分离的功能,这个与官方的MySQL-PROXY和淘宝的Amoeba是不一样的。

模拟宕机切换测试

当M1宕机,会自动切换到M2为write。如下:
[root@M1 ~]# mmm_control show
M1(10.0.0.210) master/HARD_OFFLINE. Roles:
M2(10.0.0.211) master/ONLINE. Roles: writer(10.0.0.217)
S1(10.0.0.213) slave/ONLINE. Roles: reader(10.0.0.218)
当从库S1宕机后,也会自动切换到M1或M2上面

注:本试验主要来源于 贺春旸 MySQL管理之道
本文出自 “坚持就是胜利” 博客,转载请与作者联系!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: