MySQL-MMM安装指南(Multi-Master Replication Manager for MySQL)

最基本的MMM安装必须至少需要2个数据库服务器和一个监控服务器下面要配置的MySQL Cluster环境包含四台数据库服务器和一台监控服务器,如下:

function ip hostname server id
monitoring host mon
master 1 db1 1
master 2 db2 2
slave 1 db3 3
slave 2 db4 4


 配置完成后,使用下面的虚拟IP访问MySQL Cluster,他们通过MMM分配到不同的服务器。

ip role description writer 应用程序应该连接到这个ip进行写操作 reader 应用程序应该链接到这些ip中的一个进行读操作 reader reader reader



2. Basic configuration of master 1

First we install MySQL on all hosts:

aptitude install mysql-server

Then we edit the configuration file /etc/mysql/my.cnf and add the following lines – be sure to use different server ids for all hosts:

server_id           = 1 log_bin             = /var/log/mysql/mysql-bin.log  log_bin_index       = /var/log/mysql/mysql-bin.log.index  relay_log           = /var/log/mysql/mysql-relay-bin  relay_log_index     = /var/log/mysql/mysql-relay-bin.index  expire_logs_days    = 10  max_binlog_size     = 100M  log_slave_updates   = 1

Then remove the following entry:

bind-address =

Set to number of masters:

auto_increment_increment = 2

Set to a unique, incremented number, less than auto_increment_increment, on each server

auto_increment_offset = 1

Do not bind of any specific IP, use instead:

bind-address =

Afterwards we need to restart MySQL for our changes to take effect:

/etc/init.d/mysql restart

Now we can create the required users. We'll need 3 different users:

function description privileges
monitor user used by the mmm monitor to check the health of the MySQL servers REPLICATION CLIENT
agent user used by the mmm agent to change read-only mode, replication master, etc. SUPER, REPLICATION CLIENT, PROCESS
relication user used for replication REPLICATION SLAVE

GRANT REPLICATION CLIENT                 ON *.* TO 'mmm_monitor'@'192.168.0.%' IDENTIFIED BY 'monitor_password'; GRANT SUPER, REPLICATION CLIENT, PROCESS ON *.* TO 'mmm_agent'@'192.168.0.%'   IDENTIFIED BY 'agent_password'; GRANT REPLICATION SLAVE                  ON *.* TO 'replication'@'192.168.0.%' IDENTIFIED BY 'replication_password';

Note: We could be more restrictive here regarding the hosts from which the users are allowed to connect: mmm_monitor is used from mmm_agent and replication are used from –

Note: Don't use a replication_password longer than 32 characters

4. Synchronisation of data between both databases

I'll assume that db1 contains the correct data. If you have an empty database, you still have to syncronize the accounts we have just created.

First make sure that no one is altering the data while we create a backup.


Then get the current position in the binary-log. We will need this values when we setup the replication on db2, db3 and db4.

(db1) mysql> SHOW MASTER STATUS; +------------------+----------+--------------+------------------+ | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+  | mysql-bin.000002 |      374 |              |                  |  +------------------+----------+--------------+------------------+  1 row in set (0.00 sec)

DON'T CLOSE this mysql-shell. If you close it, the database lock will be removed. Open a second console and type:

db1$ mysqldump -u root -p --all-databases > /tmp/database-backup.sql

Now we can remove the database-lock. Go to the first shell:

(db1) mysql> UNLOCK TABLES;

Copy the database backup to db2, db3 and db4.

db1$ scp /tmp/database-backup.sql <user>@ db1$ scp /tmp/database-backup.sql <user>@ db1$ scp /tmp/database-backup.sql <user>@

Then import this into db2, db3 and db4:

db2$ mysql -u root -p < /tmp/database-backup.sql db3$ mysql -u root -p < /tmp/database-backup.sql db4$ mysql -u root -p < /tmp/database-backup.sql

Then flush the privileges on db2, db3 and db4. We have altered the user-table and mysql has to reread this table.

(db2) mysql> FLUSH PRIVILEGES; (db3) mysql> FLUSH PRIVILEGES; (db4) mysql> FLUSH PRIVILEGES;

On debian and ubuntu, copy the passwords in /etc/mysql/debian.cnf from db1 to db2, db3 and db4. This password is used for starting and stopping mysql.

Both databases now contain the same data. We now can setup replication to keep it that way.

Note: Import just only add records from dump file. You should drop all databases before import dump file.

5. Setup replication

Configure replication on db2, db3 and db4 with the following commands:

(db2) mysql> CHANGE MASTER TO master_host='', master_port=3306, master_user='replication',                master_password='replication_password', master_log_file='<file>', master_log_pos=<position>; (db3) mysql> CHANGE MASTER TO master_host='', master_port=3306, master_user='replication',                master_password='replication_password', master_log_file='<file>', master_log_pos=<position>; (db4) mysql> CHANGE MASTER TO master_host='', master_port=3306, master_user='replication',                master_password='replication_password', master_log_file='<file>', master_log_pos=<position>;

Please insert the values return by “show master status” on db1 at the <file> and <position> tags.

Start the slave-process on all 3 hosts:

(db2) mysql> START SLAVE; (db3) mysql> START SLAVE; (db4) mysql> START SLAVE;

Now check if the replication is running correctly on all hosts:

(db2) mysql> SHOW SLAVE STATUS\G *************************** 1. row ***************************               Slave_IO_State: Waiting for master to send event                  Master_Host:                 Master_User: replication                 Master_Port: 3306                Connect_Retry: 60  … (db3) mysql> SHOW SLAVE STATUS\G *************************** 1. row ***************************               Slave_IO_State: Waiting for master to send event                  Master_Host:                 Master_User: replication                 Master_Port: 3306                Connect_Retry: 60  … (db4) mysql> SHOW SLAVE STATUS\G *************************** 1. row ***************************               Slave_IO_State: Waiting for master to send event                  Master_Host:                 Master_User: replication                 Master_Port: 3306                Connect_Retry: 60  …

Now we have to make db1 replicate from db2. First we have to determine the values for master_log_file and master_log_pos:

(db2) mysql> SHOW MASTER STATUS; +------------------+----------+--------------+------------------+  | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |  +------------------+----------+--------------+------------------+  | mysql-bin.000001 |       98 |              |                  | +------------------+----------+--------------+------------------+  1 row in set (0.00 sec)

Now we configure replication on db1 with the following command:

(db1) mysql> CHANGE MASTER TO master_host = '', master_port=3306, master_user='replication',               master_password='replication_password', master_log_file='<file>', master_log_pos=<position>;

Now insert the values return by “show master status” on db2 at the <file> and <position> tags.

Start the slave-process:

(db1) mysql> START SLAVE;

Now check if the replication is running correctly on db1:

(db1) mysql> SHOW SLAVE STATUS\G *************************** 1. row ***************************               Slave_IO_State: Waiting for master to send event                  Master_Host:                 Master_User: <replication>                 Master_Port: 3306                Connect_Retry: 60  …

Replication between the nodes should now be complete. Try it by inserting some data into both db1 and db2 and check that the data will appear on all other nodes.

6. Install MMM

Create user

Optional: Create user that will be the owner of the MMM scripts and configuration files. This will provide an easier method to securely manage the monitor scripts.

useradd --comment "MMM Script owner" --shell /sbin/nologin mmmd

Monitoring host

First install dependencies:

aptitude install liblog-log4perl-perl libmailtools-perl liblog-dispatch-perl libclass-singleton-perl libproc-daemon-perl libalgorithm-diff-perl libdbi-perl libdbd-mysql-perl

Then fetch the latest mysql-mmm-common*.deb and mysql-mmm-monitor*.deb and install it:

dpkg -i mysql-mmm-common_*.deb mysql-mmm-monitor*.deb

Database hosts

On Ubuntu First install dependencies:

aptitude install liblog-log4perl-perl libmailtools-perl liblog-dispatch-perl iproute libnet-arp-perl libproc-daemon-perl libalgorithm-diff-perl libdbi-perl libdbd-mysql-perl

Then fetch the latest mysql-mmm-common*.deb and mysql-mmm-agent*.deb and install it:

dpkg -i mysql-mmm-common_*.deb mysql-mmm-agent_*.deb

On RedHat

yum install -y mysql-mmm-agent

This will take care of all the dependencies, which may include:


mysql-mmm-agent.noarch 0:2.2.1-1.el5

Dependency Installed:

libart_lgpl.x86_64 0:2.3.17-4                                                 
mysql-mmm.noarch 0:2.2.1-1.el5                                                
perl-Algorithm-Diff.noarch 0:1.1902-2.el5                                     
perl-DBD-mysql.x86_64 0:4.008-1.rf                                            
perl-DateManip.noarch 0:5.44-1.2.1                                            
perl-IPC-Shareable.noarch 0:0.60-3.el5                                        
perl-Log-Dispatch.noarch 0:2.20-1.el5                                         
perl-Log-Dispatch-FileRotate.noarch 0:1.16-1.el5                              
perl-Log-Log4perl.noarch 0:1.13-2.el5                                         
perl-MIME-Lite.noarch 0:3.01-5.el5                                            
perl-Mail-Sender.noarch 0:0.8.13-2.el5.1                                      
perl-Mail-Sendmail.noarch 0:0.79-9.el5.1                                      
perl-MailTools.noarch 0:1.77-1.el5                                            
perl-Net-ARP.x86_64 0:1.0.6-2.1.el5                                           
perl-Params-Validate.x86_64 0:0.88-3.el5                                      
perl-Proc-Daemon.noarch 0:0.03-1.el5                                          
perl-TimeDate.noarch 1:1.16-5.el5                                             
perl-XML-DOM.noarch 0:1.44-2.el5                                              
perl-XML-Parser.x86_64 0:2.34-                                       
perl-XML-RegExp.noarch 0:0.03-2.el5                                           
rrdtool.x86_64 0:1.2.27-3.el5                                                 
rrdtool-perl.x86_64 0:1.2.27-3.el5

Configure MMM

All generic configuration-options are grouped in a separate file called /etc/mysql-mmm/mmm_common.conf. This file will be the same on all hosts in the system:

active_master_role          writer
<host default>
    cluster_interface       eth0
    pid_path                /var/run/
    bin_path                /usr/lib/mysql-mmm/
    replication_user        replication
    replication_password    replication_password
    agent_user              mmm_agent
    agent_password          agent_password
<host db1>
    mode                    master
    peer                    db2
<host db2>
    mode                    master
    peer                    db1
<host db3>
    mode                    slave
<host db4>
    mode                    slave
<role writer>
    hosts                   db1, db2
    mode                    exclusive
<role reader>
    hosts                   db1, db2, db3, db4
    ips           ,,,
    mode                    balanced

Don't forget to copy this file to all other hosts (including the monitoring host).

On the database hosts we need to edit /etc/mysql-mmm/mmm_agent.conf. Change “db1” accordingly on the other hosts:

include mmm_common.conf
this db1

On the monitor host we need to edit /etc/mysql-mmm/mmm_mon.conf:

include mmm_common.conf
    pid_path                /var/run/
    bin_path                /usr/lib/mysql-mmm/
    status_path             /var/lib/misc/mmmd_mon.status
    ping_ips      ,,,,
<host default>
    monitor_user            mmm_monitor
    monitor_password        monitor_password
debug 0

ping_ips are some ips that are pinged to determine whether the network connection of the monitor is ok. I used my switch ( and the four database server.

7. Start MMM

Start the agents

(On the database hosts)


Edit /etc/default/mysql-mmm-agent to enable the agent:


Red Hat

RHEL/Fedora does not enable packages to start at boot time per default policy, so you might have to turn it on manually so the agents will start automatically when server is rebooted:

chkconfig mysql-mmm-agent on

Then start it:

/etc/init.d/mysql-mmm-agent start

Start the monitor

(On the monitoring host) Edit /etc/default/mysql-mmm-monitor to enable the monitor:


Then start it:

/etc/init.d/mysql-mmm-monitor start

Wait some seconds for mmmd_mon to start up. After a few seconds you can use mmm_control to check the status of the cluster:

mon$ mmm_control show
  db1( master/AWAITING_RECOVERY. Roles: 
  db2( master/AWAITING_RECOVERY. Roles: 
  db3( slave/AWAITING_RECOVERY. Roles: 
  db4( slave/AWAITING_RECOVERY. Roles:

Because its the first startup the monitor does not know our hosts, so it sets all hosts to state AWAITING_RECOVERY and logs a warning message:

mon$ tail /var/log/mysql-mmm/mmm_mon.warn
2009/10/28 23:15:28  WARN Detected new host 'db1': Setting its initial state to 'AWAITING_RECOVERY'. Use 'mmm_control set_online db1' to switch it online.
2009/10/28 23:15:28  WARN Detected new host 'db2': Setting its initial state to 'AWAITING_RECOVERY'. Use 'mmm_control set_online db2' to switch it online.
2009/10/28 23:15:28  WARN Detected new host 'db3': Setting its initial state to 'AWAITING_RECOVERY'. Use 'mmm_control set_online db3' to switch it online.
2009/10/28 23:15:28  WARN Detected new host 'db4': Setting its initial state to 'AWAITING_RECOVERY'. Use 'mmm_control set_online db4' to switch it online.

Now we set or hosts online (db1 first, because the slaves replicate from this host):

mon$ mmm_control set_online db1
OK: State of 'db1' changed to ONLINE. Now you can wait some time and check its new roles!
mon$ mmm_control set_online db2
OK: State of 'db2' changed to ONLINE. Now you can wait some time and check its new roles!
mon$ mmm_control set_online db3
OK: State of 'db3' changed to ONLINE. Now you can wait some time and check its new roles!
mon$ mmm_control set_online db4
OK: State of 'db4' changed to ONLINE. Now you can wait some time and check its new roles!




上一篇 2015-05-01 22:16
下一篇 2015-05-02 19:19


  • 逻辑卷实例

    1、创建一个至少有两个PV组成的大小为20G的名为testvg的VG;要求PE大小为16MB, 而后在卷组中创建大小为5G的逻辑卷testlv;挂载至/users目录 2、新建用户archlinux,要求其家目录为/users/archlinux,而后su切换至archlinux用户,复制/etc/pam.d目录至自己的家目录 3、扩展testlv至7G,要…

    Linux干货 2016-09-01
  • DNS基础及使用BIND搭建域名服务器

        本文主线是DNS服务相关概念和服务器搭建,在此之前了解一下域名这个东东很有其必要性,因为DNS服务就是应域名而生的。那么域名是什么哪?     域名     域名是用一串用点分开的字符串来表示的internet上的某一台或者一组计算机的名称,用于在数据传输时标识计算机的电子方位。举…

    Linux干货 2015-06-11
  • Linux 第五天: (08月01日) Linux用户组管理

    Linux 第五天: (08月01日) Linux用户组管理         管理员 root,0普通用户 1-65535系统用户 1-499(centos6), 1-999(centos7)登录用户 500(centos6)+, 1000(centos7)+   /etc/passwd 用户及属性/etc/…

    Linux干货 2016-08-08
  • Function函数实例

    函数:function     把一段独立功能的代码当做一个整体,而后为之取一个名字,命令的代码段,即为函数。 注意:     定义函数的代码段不会自动执行,在调用时执行;所谓调用函数,在代码中给定函数名即可     函数名出现的任何位置,…

    Linux干货 2016-08-21
  • if、case 语法

    1. 条件选择 if 语句         选择执行:              单分支      &nbs…

    Linux干货 2016-08-22
  • 磁盘管理及文件系统

    磁盘管理 本文将从以下几方面介绍 第一部分:磁盘管理 1、浅识Linux磁盘 2、常见命令 第二部分:文件系统管理 1、文件系统的创建 2、文件系统挂载 3、自动挂载配置文件 第一部分:磁盘管理 浅识Linux磁盘 对于Linux来说我们知道一切皆文件,同样在Linux中硬件设备在系统上会映射成相对应的文件,我们就像是在Windows看文件一样只要打开对应的…

    Linux干货 2017-08-15