使用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 by 'RepAgent';
GRANT ALL PRIVILEGES on *.* to 'rep_monitor'@'%' identified by 'RepMonitor';


3,在server3上配置MMM
修改配置 /usr/local/mmm/etc/mmm_mon.conf,如下:

#
# Master-Master Manager config (monitor)
#

# Debug mode
debug no

# Paths
pid_path /usr/local/mmm/var/mmmd.pid
status_path /usr/local/mmm/var/mmmd.status
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
email root@localhost


# MMMD command socket tcp-port
bind_port 9988
agent_port 9989
monitor_ip 127.0.0.1

# Cluster interface
cluster_interface eth0

# Cluster hosts addresses and access params
host db1
ip 192.168.1.225
port 3306
user rep_monitor
password RepMonitor
mode master
peer db2

host db2
ip 192.168.1.226
port 3306
user rep_monitor
password RepMonitor
mode master
peer db1



#
# Define roles
#

active_master_role writer

# Mysql Reader role
role reader
mode balanced
servers db1, db2
ip 192.168.1.229, 192.168.1.230

# Mysql Writer role
role writer
mode exclusive
servers db1, db2
ip 192.168.1.231

#
# Checks parameters
#

# Ping checker
check ping
check_period 1
trap_period 5
timeout 2

# Mysql checker
# (restarts after 10000 checks to prevent memory leaks)
check mysql
check_period 1
trap_period 2
timeout 2
restart_after 10000

# Mysql replication backlog checker
# (restarts after 10000 checks to prevent memory leaks)
check rep_backlog
check_period 5
trap_period 10
max_backlog 60
timeout 2
restart_after 10000

# Mysql replication threads checker
# (restarts after 10000 checks to prevent memory leaks)
check rep_threads
check_period 1
trap_period 5
timeout 2
restart_after 10000


启动
server1 与 server2上分别启动mmmd_agent
[root@localhost ~]# mmmd_agent
[root@localhost ~]# netstat -tlnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:9989 0.0.0.0:* LISTEN 26010/perl

出现9989端口说明启动成功。


server3上启动mmmd_mon
[root@localhost etc]# mmmd_mon
Reading config file: 'mmm_mon.conf'
$VAR1 = {
'db2' => {
'roles' => [
'reader(192.168.1.229;)'
],
'version' => '0',
'state' => 'ONLINE'
},
'db1' => {
'roles' => [
'reader(192.168.1.230;)',
'writer(192.168.1.231;)'
],
'version' => '0',
'state' => 'ONLINE'
}
};
Role: 'reader(192.168.1.229;)'
Adding role: 'reader' with ip '192.168.1.229'
Role: 'reader(192.168.1.230;)'
Adding role: 'reader' with ip '192.168.1.230'
Role: 'writer(192.168.1.231;)'
Adding role: 'writer' with ip '192.168.1.231'

在管理节点上(server3)启动db节点:
mmm_control set_online db1
mmm_control set_online db2

查看下节点状态:
[root@localhost etc]# mmm_control show
Config file: mmm_mon.conf
Daemon is running!
Servers status:
db1(192.168.1.225): master/ONLINE. Roles: reader(192.168.1.230;), writer(192.168.1.231;)
db2(192.168.1.226): master/ONLINE. Roles: reader(192.168.1.229;)



OK,能看到这个效果说明你已经配置成功了,下面就可以随心所欲的折腾mysql的异常状态,来进行测试。

你可能感兴趣的:(mysql,socket,db2,配置管理,perl)