日期:2014-05-16  浏览次数:20967 次

使用MMM搭建Mysql同步高可用性

先介绍下MMM?
MMM是 mysql-master-master的缩写。?
MMM项目来自 Google:http://code.google.com/p/mysql-master-master?

MySQL本身没有提供replication failover的解决方案,那么如何使Replication方案具有HA的功能呢,那就是MMM。?
大家都知道mysql的同步机制在解决实际问题上具有很高的可行性,特别对读写负载比较高的web2.0来说,那么在实际应用中必须保证mysql的稳定性及性能。?

Master-Master Replication?
1、使用两个MySQL数据库db1,db2,互为Master和Slave,即:?
一边db1作为db2的master,一旦有数据写向db0时,db2定时从db1更新?
另一边db2也作为db1的master,一旦有数据写向db2时,db1也定时从db2获得更新?

2、但从AppServer的角度来说,同时只有一个结点db1扮演Master,另外一个结点db2扮演Slave,不能同时两个结点扮演Master。即AppSever总是把write操作分配某个数据库(db1),除非db1 failed,被切换。?

3、如果扮演Slave的数据库结点db2 Failed了:?
a)此时appServer要能够把所有的read,write分配给db1,read操作不再指向db2?
b)一旦db2恢复过来后,继续充当Slave角色,并告诉AppServer可以将read分配给它了?

4、如果扮演Master的数据库结点db1 Failed了?
a)此时appServer要能够把所有的写操作从db1切换分配给db2,也就是切换Master由db2充当?
b)db1恢复过来后,充当Slave的角色,Master由db2继续扮演?


MMM利用了虚拟IP的技术:1个网卡可以同时使用多个IP。?
(所以使用MMM时,需要2*n+1个IP,n为mysql数据库结点个数,包括master,slave)?

MMM有3个重要的器件:?
1、mmmd_mon - 数据库控制节点运行。?
2、mmm_control - 数据库控制节点运行。?
3、mmmd_agent - 数据库节点(master/slave)运行。?


本文环境:?
server1 ip: 192.168.1.225 ; virtual read ip:192.168.1.229?
server2 ip: 192.168.1.226 ; virtual read ip:192.168.1.230?

server3 ip: 192.168.1.227?
virtual write ip: 192.168.1.231?


配置过程很简单,?
server1 server2 replication双向 master-master?
server1 server2 安装mmm并配置mmm_agent.conf?
server3 安装mmm并配置mmm_mon.conf?

1,首先我们要先配置好master-master双向同步,这一步就不多说了。?
2,安装部署MMM?
3台服务器都要安装MMM软件,安装过程如下:?

安装如下perl模块,为MMM安装做好准备,?
cpan Algorithm::Diff?
cpan Proc::Daemon?
cpan Time::HiRes?
cpan DBI?
cpan DBD::mysql?

wget http://mysql-master-master.googlecode.com/files/mmm-1.0-pre2.tar.bz2?
tar xzf mmm-1.0-pre2.tar.bz2?
cd mmm-1.0-pre2?
./install.pl?

3,在server1上配置MMM?
修改配置 /usr/local/mmm/etc/mmm_agent.conf,如下:?

#?
# Master-Master Manager config (agent)?
#?

# Debug mode?
debug no?

# Paths?
pid_path /usr/local/mmm/var/mmmd_agent.pid?
bin_path /usr/local/mmm/bin?

# Logging setup?
log mydebug?
file /usr/local/mmm/var/mmm-debug.log?
level debug?

log mytraps?
file /usr/local/mmm/var/mmm-traps.log?
level trap?

# MMMD command socket tcp-port and ip?
bind_port 9989?

# Cluster interface?
cluster_interface eth0?

# Define current server id?
this db1?
mode master?

# For masters?
peer db2?

# Cluster hosts addresses and access params?
host db1?
ip 192.168.1.225?
port 3306?
user rep_agent?
password RepAgent?

host db2?
ip 192.168.1.226?
port 3306?
user rep_agent?
password RepAgent?

GRANT ALL PRIVILEGES on *.* to 'rep_agent'@'%′ identified by 'RepAgent';?
GRANT ALL PRIVILEGES on *.* to 'rep_monitor'@'%' identified by 'RepMonitor';

?

?

3,在server2上配置MMM?
修改配置 /usr/local/mmm/etc/mmm_agent.conf,如下:?

#?
# Master-Master Manager config (agent)?
#?

# Debug mode?
debug no?

# Paths?
pid_path /usr/local/mmm/var/mmmd_agent.pid?
bin_path /usr/local/mmm/bin?

# Logging setup?
log mydebug?
file /usr/local/mmm/var/mmm-debug.log?
level debug?

log mytraps?
file /usr/local/mmm/var/mmm-traps.log?
level trap?

# MMMD command socket tcp-port and ip?
bind_port 9989?

# Cluster interface?
cluster_interface eth0?

# Define current server id?
this db2?
mode master?

# For masters?
peer db1?

# Cluster hosts addresses and access params?
host db1?
ip 192.168.1.225?
port 3306?
user rep_agent?
password RepAgent?

host db2?
ip 192.168.1.226?
port 3306?
user rep_agent?
password RepAgent?

GRANT ALL PRIVILEGES on *.* to 'rep_agent'@'%′ identified