下面要配置的MySQL-MMM环境包含四台数据库服务器和一台管理服务器,如下:
  OS:    CentOS-6.4-x86_64-minimal
  MYSQL: mysql-5.5.30-linux2.6-x86_64
 
 参考文档:http://mysql-mmm.org/mmm2:guide
 
------------------------------------------------------
function      IP          servername       server-id
-------------------------------------------------------
monitor   192.168.137.42   monitor                 
master    192.168.137.40    db1                    1
master      192.168.137.41    db2                   2
slave      192.168.137.43    db3                    3
slave      192.168.137.44    db4                    4
-------------------------------------------------------

虚拟IP设置如下:
writer(192.168.137.140)
reader(192.168.137.141)
reader(192.168.137.142)
reader(192.168.137.143)


一、配置MySQL Relication,
1.修改添加内容在/etc/my.cnf如下
[mysqld] 
 
default-storage-engine = innodb 
replicate-ignore-db = mysql 
binlog-ignore-db    = mysql 
 
server-id           = 1 
log-bin             = mysql-bin.log 
log_bin_index       = mysql-bin.log.index 
relay_log           = mysql-bin.relay 
relay_log_index     = mysql-bin.relay.index 
expire_logs_days    = 10 
max_binlog_size     = 100M 
log_slave_updates   = 1

注意:server-id在每台服务器上的值都是不一样 

2.重启mysql服务,重启成功后登陆mysql,执行show master show;命令
记住File 和Position的值

3.使用mysql-mmm时一共需要三个用户: replication、mmm_agent和mmm_monitor
(管理服务器上用来监控cluster状态的用户,所以可以限定只能从管理服务器登录)。
使用下面三条命令新建这三个用户并分配相应的权限:
mysql>GRANT REPLICATION CLIENT ON *.* TO 'mmm_monitor'@'192.168.137.%' IDENTIFIED BY 'monitor';
mysql>GRANT REPLICATION SLAVE  ON *.* TO 'replication'@'192.168.137.%' IDENTIFIED BY 'replication';
mysql>GRANT SUPER, REPLICATION CLIENT, PROCESS ON *.* TO 'mmm_agent'@'192.168.137.%'   IDENTIFIED BY 'agent';

4.启动从数据库SLAVE进程,Mysql版本从5.1.7以后开始就不支持“master-host”类似的参数,在从库上执行如下命令;
mysql>CHANGE MASTER TO master_host='192.168.137.40', master_port=3306, master_user='replication',master_password='replication', master_log_file='mysql-bin.000004', master_log_pos=107;
mysql>start slave;
mysql>show slave status\G

主要看
Slave_IO_Running: Yes 
Slave_SQL_Running: Yes
是否为Yes

将db2作为master,db1作为slave,重复1-4的内容 。
将db3 db4配置为db1的slave,重复1-4的内容 。


二、安装MMM
在管理服务器和数据库服务器上分别要运行mysql-mmm monitor和agent程序。
下面分别安装:

先安装mysql-mmm依赖文件可从如下地址下载
http://search.cpan.org/

或者通过”perl -MCPAN -e shell“ 来安装

yum -y install perl-CPAN
yum -y install perl-Time-HiRes*

tar -zxvf Algorithm-Diff-1.1902.tar.gz
cd Algorithm-Diff-1.1902
perl Makefile.PL
make
make test
make install

tar -zxvf DBI-1.625.tar.gz
cd DBI-1.625
perl Makefile.PL
make
make test
make install

tar -zxvf Log-Dispatch-2.37.tar.gz
cd Log-Dispatch-2.37
perl Makefile.PL
make
make test
make install

tar -zxvf Log-Log4perl-1.40.tar.gz
cd Log-Log4perl-1.40
perl Makefile.PL
make
make test
make install

tar -zxvf MailTools-2.12.tar.gz
cd MailTools-2.12
perl Makefile.PL
make
make test
make install

gzip -d Net-ARP-1.0.6.gz
tar xzvf Net-ARP-1.0.6
cd Net-ARP
perl Makefile.PL
make
make install

tar -zxvf Proc-Daemon-0.14.tar.gz
cd Proc-Daemon-0.14
perl Makefile.PL
make
make test
make install

tar -zxvf DBD-mysql-4.023.tar.gz
cd DBD-mysql-4.030
perl Makefile.PL --with-mysql=/usr/local/mysql/ --mysql_config=/usr/local/mysql/bin/mysql_config
make
make install


tar xzvf mysql-mmm-2.2.1.tar.gz
cd mysql-mmm-2.2.1
make install

 在monitor安装
tar xzvf Class-Singleton-1.4.tar.gz
cd Class-Singleton-1.4
make
make install
 

三、配置MMM
完成安装后,所有的配置文件都放到了/etc/mysql-mmm/下面。管理服务器和数据库服务器上都要包含一个共同的文件mmm_common.conf,内容如下:
active_master_role  writer



  cluster_interface    eth0

  pid_path        /var/run/mmm_agentd.pid
  bin_path        /usr/lib/mysql-mmm/

        replication_user                replication
        replication_password        replication

  agent_user        mmm_agent
  agent_password        agent



  ip                        192.168.137.40
  mode          master
  peer          db2



  ip                        192.168.137.41
  mode          master
  peer          db1



  ip          192.168.137.43
  mode          slave



                ip                192.168.137.44
                mode              slave




  hosts          db1, db2
  ips          192.168.137.140
  mode          exclusive



  hosts          db2, db3, db4
  ips          192.168.137.141, 192.168.137.142, 192.168.137.143
  mode          balanced



可以在db1上编辑该文件后,通过scp命令分别复制到monitor、db2、db3、db4上。

编辑mmm_agent.conf。在数据库服务器上,还有一个mmm_agent.conf需要修改,其内容是:
include mmm_common.conf
this db1

最后一行的db1,在不同的数据库服务器上要分别改为db2、db3和db4,否则代理就会无法启动。

在数据库服务器上启动代理程序
/etc/init.d/mysql-mmm-agent start

编辑mmm_mon.confg。在管理服务器上,修改mmm_mon.conf文件,修改后内容为:
include mmm_common.conf


  ip          192.168.137.42
  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.137.40, 192.168.137.41, 192.168.137.43, 192.168.137.44
#auto_set_online            60



  monitor_user      mmm_monitor
  monitor_password    monitor


debug 0


在管理服务器上启动监控程序
/etc/init.d/mysql-mmm-monitor start

启动后,稍等几秒,可以通过mmm_control程序查看状态:
[root@monitor ~]# mmm_control show
  db1(192.168.137.40) master/AWAITING_RECOVERY. Roles:
  db2(192.168.137.41) master/AWAITING_RECOVERY. Roles:
  db3(192.168.137.43) slave/AWAITING_RECOVERY. Roles:
  db4(192.168.137.44) slave/AWAITING_RECOVERY. Roles:
 

将所有DB服务器设为online状态:
shell>mmm_control set_online db1
shell>mmm_control set_online db2
shell>mmm_control set_online db3
shell>mmm_control set_online db4

再次查看状态:
[root@monitor ~]#mmm_control show
  db1(192.168.137.40) master/ONLINE. Roles: writer(192.168.137.140)
  db2(192.168.137.41) master/ONLINE. Roles: reader(192.168.137.142)
  db3(192.168.137.43) slave/ONLINE. Roles: reader(192.168.137.141)
  db4(192.168.137.44) slave/ONLINE. Roles: reader(192.168.137.143)
 
设置成功,下面查看节点状态:
[root@monitor ~]# mmm_control checks all
db4  ping         [last change: 2013/04/16 13:22:29]  OK
db4  mysql        [last change: 2013/04/16 13:22:29]  OK
db4  rep_threads  [last change: 2013/04/16 13:22:29]  OK
db4  rep_backlog  [last change: 2013/04/16 13:22:29]  OK: Backlog is null
db2  ping         [last change: 2013/04/16 09:49:54]  OK
db2  mysql        [last change: 2013/04/16 09:49:54]  OK
db2  rep_threads  [last change: 2013/04/16 09:49:54]  OK
db2  rep_backlog  [last change: 2013/04/16 09:49:54]  OK: Backlog is null
db3  ping         [last change: 2013/04/16 09:49:54]  OK
db3  mysql        [last change: 2013/04/16 09:49:54]  OK
db3  rep_threads  [last change: 2013/04/16 09:49:54]  OK
db3  rep_backlog  [last change: 2013/04/16 09:49:54]  OK: Backlog is null
db1  ping         [last change: 2013/04/16 09:49:54]  OK
db1  mysql        [last change: 2013/04/16 09:49:54]  OK
db1  rep_threads  [last change: 2013/04/16 09:49:54]  OK
db1  rep_backlog  [last change: 2013/04/16 09:49:54]  OK: Backlog is null

最后将除write机器的DB的read_only状态设为on: 
mysql>set global read_only=on;
或者在slave机器上添加只读账号

在db1,db2上添加一个帐号给外部登陆:
mysql>grant all  privileges on * . * to 'test'@'%' identified by '123456';
然后就可以通过 mysql -utest -p123456 -h 192.168.137.140 连接writer 服务器
同样的道理可以虚拟IP连接reader服务器。


特别注意:现在解释一下monitor机上/etc/mysql-mmm/mmm_mon.conf里我注释掉的那句auto_set_online参数:
不设置该参数:MySql服务器挂掉的时间超过60秒的话,服务器恢复后会一直处于AWAITING_RECOVERY的状态,需要我们手动set_online把他设置为ONLINE状态,如果低于60秒,且非Flapping状态,会自动设置为ONLINE状态。
设置了该参数:服务器恢复到AWAITING_RECOVERY状态后,会等待我们设置的秒数,然后自动设置为ONLINE状态(还要考虑Flapping状态)。
因此,设置与不设置auto_set_online参数根据你的需求了,我偏向于不设置。

特别注意,在安装过程中发现writer IP ping不通,通过ip add list命令发现也没发现虚拟IP,而reader IP 则没有这个问题。
在db1上的/etc/mysql-mmm/mmm_agent.conf文件加入一行
debug 1
重启/etc/init.d/mysql-mmm-agent restart 报错,如下图所示


原来是DBD-mysql 没有安装好,
cp -rf /usr/local/mysql/lib/* /usr/lib64/
cp -rf /usr/local/mysql/include/mysql/* /usr/include/
重新安装DBD-mysql 后问题解决.