架构图如下:

wKiom1ONp32RH7W7AAL5RCx3n4E759.jpg

# 架构图说明:上图的读写分离部分并不包括在mysql-mmm的架构中,是我自己画进去的,如果要实现上述的架构,还需要在前端编写读写分离程序。

MySQL-MMM介绍:

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

    MySQL本身没有提供replication failover的解决方案,通过MMM方案能实现服务器的故障转移,从而实现mysql的高可用。MMM不仅能提供浮动IP的功能,更牛的是如果当前的主服务器挂掉后,会将你后端的从自动转向新的主服务器进行同步复制,不用手工更改同步资料。

资料来源: http://mysql-mmm.org/   http://blog.chinaunix.net/uid-20639775-id-3337488.html


方案优缺点:

优点:安全性、稳定性高,可扩展性好,高可用,当主服务器挂掉以后,另一个主立即接管,其他的从服务器能自动切换,不用人工干预。

缺点:至少三个节点,对主机的数量有要求,需要实现读写分离,对程序来说是个挑战。

      monitor角色属于单点.


适用场景:

MMM的适用场景为数据库访问量大,业务增长快,并且能实现读写分离的场景。


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

        mmm_mond  负责所有的监控工作的监控守护进程,决定节点的移除等等

        mmm_agentd  运行在mysql服务器上的代理守护进程,通过简单远程服务集提供给监控节点

        mmm_control  通过命令行管理mmm_mond进程

部署实施:

1、环境介绍

角色
IPserver-idwrite-VIPread-VIP
monitor192.168.0.100


master-01192.168.0.1021
192.168.0.188192.168.0.202
master-02192.168.0.1032192.168.0.188192.168.0.203
slave192.168.0.101100
192.168.0.201

其中:mysql主主已经配置完毕,不会配置的童鞋查看我的其他博客进行配置。

      mysql主从也已经配置完毕,slave的master-ip指向为 master-01

  主从同步账户为: rep        密码为 :123456 -->在后边的配置文件中会用到

2、在数据库节点上,分别执行下列命令进行安装mysql-mmm

cd /mnt/tools/
wget http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
rpm -Uvh epel-release-6-8.noarch.rpm 
yum install -y mysql-mmm-agent

   在monitor节点上,执行下面命令

cd /mnt/tools/
wget http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
rpm -Uvh epel-release-6-8.noarch.rpm 
yum install -y mysql-mmm*

3. 在master-01,master-02,slave 上授权不同角色

# 由于我的配置文件忽略了同步 mysql和information_schema数据库,所以才需要分别执行!
# 否则的话,在主执行一次,其余数据库就会自动同步过去了。
mysql> GRANT REPLICATION CLIENT ON *.* TO 'mmm_monitor'@'192.168.0.%' IDENTIFIED BY 'gang123'; 
Query OK, 0 rows affected (0.01 sec)

mysql> GRANT SUPER, REPLICATION CLIENT, PROCESS ON *.* TO 'mmm_agent'@'192.168.0.%' IDENTIFIED BY 'gang123';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

4. 在所有服务器上配置/etc/mysql-mmm/mmm_common.conf

# 可以修改一台,然后同步到其他服务器,记住,是所有服务器,包括DB,monitor

[root@master-01 tools]# vim /etc/mysql-mmm/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        rep
    replication_password    123456
    agent_user              mmm_agent
    agent_password          gang123
</host>
#定义master-01主机 peer定义主主角色成员 mode指定读/写角色
<host db1>
    ip      192.168.0.102  
    mode    master
    peer    db2
</host>
#定义master-02主机 peer定义主主角色成员 mode指定读/写角色
<host db2>
    ip      192.168.0.103
    mode    master
    peer    db1
</host>
#定义slave主机 
<host db3>
    ip      192.168.100.101
    mode    slave
</host>
# 定义 write-VIP
<role writer>
    hosts   db1, db2
    ips     192.168.0.188
    mode    exclusive
</role>
# 定义 read-VIP
<role reader>
    hosts   db1, db2
    ips     192.168.0.201 192.168.0.202 192.168.0.203
    mode    balanced
</role>

5. 修改所有MySQL数据库主机的/etc/mysql-mmm/mmm_agent.conf配置文件,使之和mmm_commom.conf中匹配

例如,修改master-02为

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
#此处为db2...

6. 修改monitor主机的配置文件/etc/mysql-mmm/mmm_mon.conf

[root@slave tools]# vim /etc/mysql-mmm/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            192.168.0.101 192.168.0.102 192.168.0.103
    auto_set_online     15
# ping_ips 表示mmm-monitor服务监控的数据库服务器
# auto_set_online 表示自动切换时间间隔,15秒失去联系,启动切换角色
    # 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    gang123       #数据库授权密码
</host>

debug 0

7. 在三台 MySQL数据库节点启动agent进程

[root@master tools]# /etc/init.d/mysql-mmm-agent start
Starting MMM Agent Daemon:                                 [  OK  ]

  在monitor服务器开启monitor进程

[root@monitor tools]# /etc/init.d/mysql-mmm-monitor start
Starting MMM Monitor Daemon:                               [  OK  ]

#至此,配置已经完成。








遇到的问题:启动Minotor进程失败...未启动,目前正在解决中。

[root@monitor monitor]# /etc/init.d/mysql-mmm-monitor start

Starting MMM Monitor Daemon: 2014/06/04 06:29:37  INFO STARTING...

2014/06/04 06:29:37 DEBUG Created pid file '/var/run/mysql-mmm/mmm_mond.pid' with pid 41144

2014/06/04 06:29:37  INFO Waiting for network connection...

2014/06/04 06:29:37  INFO Spawning checker 'ping_ip'...