mysql-mmm-2.2.1安装手册中文版

MMM Installation Guide

Mysql Master-Master Replication Manager
(mysql-mmm installation Guide)
Version:mysql-mmm-2.2.1

目录
一、安装环境介绍 4
一、确定MMM架构(参见手册第二章,典型应用) 4
二、确定操作系统和mysql版本及相关信息 4
二、安装步骤分析 5
三、正式安装 5
一、安装系统 5
二、安装mysql 5
1、检查系统是否安装过mysql 5
2、下载mysql软件,编译安装,在四台机器上都需要做 5
三、配置mysql master-master复制 6
1、修改my.cnf配置 6
2、创建复制使用的mysql用户 7
3、同步数据 7
4、配置复制关系 8
四、安装mysql-mmm 11
1、安装agent和monitor需要的perl库 12
2、下载安装mysql-mmm 13
3、mysql-mmm使用的目录和存放文件简介 13
4、配置MYSQL-MMM-agentd 13
5、启动MYSQL-mmm_agent 15
6、配置MYSQL-MMM-monitor 15
7、启动MYSQL-MMM-monitor 16
8、管理MYSQL-MMM-monitor 16
9、从2个master架构扩展到2个master多个slave架构(已修正) 17
四、测试 17

一、测试写入数据是否同步 19
二、测试writer故障切换 20
三、测试db3的主是否会自动切换 22
四、测试角色优先配置的影响 24

一、安装环境介绍
一、确定MMM架构(参见手册第二章,典型应用)
我们使用两个master,一个slave的架构。

二、确定操作系统和MYSQL版本及相关信息
1、三台安装mysql的服务器
主机名 IP地址  角色   mysql_server_id
db1  10.1.1.15  master1   1
db2  10.1.1.14  master2   2
db3  10.1.1.13  slave1   3
mmm 10.1.1.12  mmm_mon  –

虚拟IP规划
IP   角色   描述
10.1.1.20  writer  应用程序连接此服务器写入数据
10.1.1.21  reader  应用程序连接此服务器读取数据
10.1.1.22  reader  应用程序连接此服务器读取数据
10.1.1.23  reader  应用程序连接此服务器读取数据
2、软件版本
LINUX所有服务器使用RHEL5.4
MYSQL使用mysql-5.1.40.tar.gz
MMM使用mysql-mmm-2.2.1.tar.gz
3、其他
Mysql使用utf8字符集
存储引擎使用myisam
Binlog格式使用ROW
Mysql端口使用9188
3、获得软件
RHEL5.4:http://rhel.ieesee.net/uingei/
MYSQL:http://downloads.mysql.com/archives.php?p=mysql-5.1&o=other
MMM:http://mysql-mmm.org/_media/:mmm2:mysql-mmm-2.2.1.tar.gz
二、安装步骤分析
一、编译安装mysql
二、搭建mysql的master-master复制架构
三、安装mysql-mmm-agent
四、安装mysql-mmm,搭建成完成两个master的架构(参见手册第二章,典型应用)
五、在刚才的基础上增加一个slave,最终完成两个master,多个slave的架构
三、正式安装
一、安装系统
略过
二、安装MYSQL
1、检查系统是否安装过MYSQL
# rpm -qa | grep mysql  #如果有,使用以下命令先卸载
#yum remove mysql*
2、下载MYSQL软件,编译安装,在四台机器上都需要做
#useradd mysql
#mkdir /soft && cd /soft
#wget http://downloads.mysql.com/archives/mysql-5.1/mysql-5.1.40.tar.gz
#tar -zxvf mysql-5.1.40.tar.gz  && cd mysql-5.1.40
#mkdir   /usr/local/mysql
#./configure     \
–prefix=/usr/local/mysql       \
–without-debug \
–enable-thread-safe-client \
–enable-assembler \
–enable-profiling \
–with-mysqld-ldflags=-all-static \
–with-client-ldflags=-all-static \
–with-charset=utf8 \
–with-extra-charsets=all  \
–with-big-tables       \
–enable-largefile      \
–without-ndb-debug     \
–with-plugins=partition
#make && make install
#cp support-files/my-medium.cnf /etc/my.cnf
#cd /usr/local/mysql/bin
#./mysql_install_db –user=mysql
#./mysqld_safe   –user=mysql   –skip_name_resolve  &
#/usr/local/mysql/bin/mysql -uroot mysql -e “delete from user where user = ”;”
# echo “/usr/local/mysql/bin/mysqld_safe –user=mysql –skip_name_resolve &” >> /etc/rc.local  \\修改系统启动默认启动mysql
#修改.bash_profile   PATH=$PATH:$HOME/bin:/usr/local/mysql/bin/

三、配置MYSQL MASTER-MASTER复制
1、修改MY.CNF配置
db1 修改my.cnf,确定有以下内容
port            = 9188
server-id       = 1
log-bin=mysql-bin
binlog_format=ROW
log-slave-updates
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=1

example:
[client]
port            = 9188
socket          = /tmp/mysql.sock
[mysqld]
port            = 9188
socket          = /tmp/mysql.sock
skip-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
log-bin=mysql-bin
binlog_format=ROW
log-slave-updates
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=1
server-id       = 1
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout

db2 修改my.cnf,确定有以下内容
port            = 9188
server-id       = 2
log-bin=mysql-bin
binlog_format=ROW
log-slave-updates
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=2
db3修改my.cnf,确定有以下内容
port            = 9188
server-id       = 3
log-bin=mysql-bin
log-slave-updates
db4修改my.cnf,确定有以下内容
port            = 9188
server-id       = 4
log-bin=mysql-bin
log-slave-updates

现在正式开始配置db1和db2的master-master复制
修改过刚才的配置文件以后,重新启动mysql

2、创建复制使用的MYSQL用户
db1:
GRANT REPLICATION SLAVE ON *.* TO ‘slave’@’10.1.1.14’ IDENTIFIED BY ‘slave’;
GRANT REPLICATION SLAVE ON *.* TO ‘slave’@’10.1.1.13’ IDENTIFIED BY ‘slave’;
flush privileges;
db2:
GRANT REPLICATION SLAVE ON *.* TO ‘slave’@’10.1.1.15’ IDENTIFIED BY ‘slave’;
GRANT REPLICATION SLAVE ON *.* TO ‘slave’@’10.1.1.13’ IDENTIFIED BY ‘slave’;
flush privileges;

3、同步数据
由于我们都是新装的库,数据是同步的,但是我们也简单操作以下步骤,开始配置同步数据到复制结尾,都不要让任何的mysql服务器写入数据,避免导致数据不同步。
db1:
mysql> flush tables with read lock;
mysql> show master status\G
*************************** 1. row ***************************
File: mysql-bin.000001
Position: 409
Binlog_Do_DB:
Binlog_Ignore_DB:
1 row in set (0.00 sec)
通过任何方法拷贝db1的数据到db2去
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
4、配置复制关系
先配置db2复制db1的数据
db2:
mysql> CHANGE MASTER TO
-> MASTER_HOST=’10.1.1.15′,
-> MASTER_USER=’slave’,
-> MASTER_PASSWORD=’slave’,
-> MASTER_PORT=9188,
-> MASTER_LOG_FILE=’mysql-bin.000001′,
-> MASTER_LOG_POS=409,
-> MASTER_CONNECT_RETRY=10;
Query OK, 0 rows affected (0.01 sec)

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.1.1.15
Master_User: slave
Master_Port: 9188
Connect_Retry: 10
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 409
Relay_Log_File: zj13-relay-bin.000002
Relay_Log_Pos: 251
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 409
Relay_Log_Space: 405
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
1 row in set (0.00 sec)

Slave_IO_Running: Yes
Slave_SQL_Running: Yes
这两个值是YES,证明db2复制db1的数据成功了
mysql> show master status\G
*************************** 1. row ***************************
File: mysql-bin.000001
Position: 325
Binlog_Do_DB:
Binlog_Ignore_DB:
1 row in set (0.00 sec)

接着配置db1复制db2的数据:
db1:
mysql> CHANGE MASTER TO
-> MASTER_HOST=’10.1.1.14′,
-> MASTER_USER=’slave’,
-> MASTER_PASSWORD=’slave’,
-> MASTER_PORT=9188,
-> MASTER_LOG_FILE=’mysql-bin.000001′,
-> MASTER_LOG_POS=325,
-> MASTER_CONNECT_RETRY=10;
Query OK, 0 rows affected (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.1.1.14
Master_User: slave
Master_Port: 9188
Connect_Retry: 10
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 325
Relay_Log_File: zj14-relay-bin.000002
Relay_Log_Pos: 251
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 325
Relay_Log_Space: 405
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
1 row in set (0.00 sec)

5、简单的master-master测试
在db1的test目录建立一张表
db1:
mysql> use test
Database changed
mysql> create table t (id int(11) not null auto_increment,name varchar(30),primary key (id));
Query OK, 0 rows affected (0.00 sec)
mysql> insert into t(name) values (‘andy.feng’);
Query OK, 1 row affected (0.00 sec)

mysql> select * from t;
+—-+———–+
| id | name      |
+—-+———–+
|  1 | andy.feng |
+—-+———–+
1 row in set (0.00 sec)

在db2上查询,并且也插入一条数据,到db1再查看,是否两边同步,并且注意id的值
db2:
mysql> use test
Database changed
mysql> select * from t;
+—-+———–+
| id | name      |
+—-+———–+
|  1 | andy.feng |
+—-+———–+
1 row in set (0.00 sec)

mysql> insert into t(name) values (‘fh.cn’);
Query OK, 1 row affected (0.00 sec)

mysql> insert into t(name) values (‘mysql-mmm’);
Query OK, 1 row affected (0.00 sec)

mysql> select * from t;
+—-+———–+
| id | name      |
+—-+———–+
|  1 | andy.feng |
|  2 | fh.cn     |
|  4 | mysql-mmm |
+—-+———–+
3 rows in set (0.00 sec)

db1:
mysql> select * from t;
+—-+———–+
| id | name      |
+—-+———–+
|  1 | andy.feng |
|  2 | fh.cn     |
|  4 | mysql-mmm |
+—-+———–+
3 rows in set (0.00 sec)
这样看来,master-master复制是搭建成功了。
四、安装MYSQL-MMM
1、安装AGENT和MONITOR需要的PERL库
注意: 如果你想要使用非root用户运行mmm_mond进程请在db4上安装fping。
请在agent机器上(db1,db2,db3)上安装iproute包,一般系统默认是安装过的。
安装使用perl MCPAN,具体方法自己google
在 db1,db2,db3上都安装如下perl库(mysql-agent需求的perl库)
有些perl库,特别是红色的三个库系统默认应该是有的,可以通过以下方法查看是否,安装,如果安装,就不必要安装,因为安装这三个库比较麻烦,特别DBD::mysql,需要下载软件手工编译
确认是否安装一下模块:
#find /usr/lib/perl5/  -name mysql.pm
#find /usr/lib/perl5/  -name stat.pm
#find /usr/lib/perl5/  -name Basename.pm

# perl -MCPAN -e shell
cpan> install Algorithm::Diff
cpan> install DBI
cpan>install Log::Dispatch
cpan> install Log::Log4perl
cpan> install Mail::Send
cpan> install Net::ARP
cpan> install Proc::Daemon
cpan> install Time::HiRes
cpan>install DBD::mysql
cpan>install File::stat
cpan>install File:basename

手工编译安装DBD::mysql的几点注意:
# PERL -MCPAN -E SHELL
CPAN> GET DBD::MYSQL
存放的地址一般在这里/ROOT/.CPAN/SOURCES/AUTHORS/ID/C/CA/CAPTTOFU/DBD-MYSQL-4.019.TAR.GZ
注意:
? 如果手工编译的MYSQL,一定要把加载MYSQL的LIB
# ECHO “/USR/LOCAL/MYSQL/LIB/MYSQL/” >>  /ETC/LD.SO.CONF
#LDCONF
?  在PERL MAKEFILE.PL 的时候,一定要指定你定制的一些MYSQL的信息
#PERL MAKEFILE.PL  –MYSQL_CONFIG=/USR/LOCAL/MYSQL/BIN/MYSQL_CONFIG –WITH-MYSQL=/USR/LOCAL/MYSQL/
? 一定要MAKE TEST,通过了再MAKE INSTALL

在db4上安装如下perl库(mysql-monitor需求的perl库)
先使用类似上面的find语句查找是否已经安装对应模块,如果已经安装,就不必要再安装一次
# perl -MCPAN -e shell
cpan> install Algorithm::Diff
cpan> install Class::Singleton
cpan> install Log::Dispatch
cpan> install Log::Log4perl
cpan> install Mail:Send
cpan> install Proc::Daemon
cpan> install Thread::Queue
cpan> install Time::HiRes

红色的应该是系统自带的:
–DBI and DBD::mysql
–File::Basename
–File::stat
–File::Temp
–Net::Ping

2、下载安装MYSQL-MMM
db1,db2,db3,db4都要安装:
/soft]# wget http://mysql-mmm.org/_media/:mmm2:mysql-mmm-2.2.1.tar.gz
# mv :mmm2:mysql-mmm-2.2.1.tar.gz mysql-mmm-2.2.1.tar.gz
# tar -zxvf mysql-mmm-2.2.1.tar.gz
# cd mysql-mmm-2.2.1
# make install

3、MYSQL-MMM使用的目录和存放文件简介
文件目录          描述
/usr/lib/perl5/vendor_perl/5.8.8/MMM    MMM使用的perl模块
/usr/lib/mysql-mmm        MMM的脚本插件
/usr/sbin   MMM的命令保存路径
/var/log/mysql-mmm   MMM的日志保存路径
/etc   MMM配置文件保存的路径
/etc/mysql-mmm   MMM配置文件保存的路径,优先级最高
/etc/init.d/  agentd和monitor的启动关闭脚本

4、配置MYSQL-MMM-AGENTD
添加agentd使用的mysql用户,db1,db2,db3都要添加
db1,db2,db3:
mysql>grant super,replication client,process on *.* to ‘mmm_agent’@’10.1.1.15’ identified by ‘mmm_agent’;
mysql>grant super,replication client,process on *.* to ‘mmm_agent’@’10.1.1.14’ identified by ‘mmm_agent’;
mysql>grant super,replication client,process on *.* to ‘mmm_agent’@’10.1.1.13’ identified by ‘mmm_agent’;
mysql>grant super,replication client,process on *.* to ‘mmm_agent’@’10.1.1.12’ identified by ‘mmm_agent’;
(由于db1和db2有复制,所以只需要在其中一台执行就可以了。db3需要单独执行一遍)

修改mysql-mmm配置文件
db1:
# cat /etc/mysql-mmm/mmm_agent.conf
include mmm_common.conf
this db1
# cat /etc/mysql-mmm/mmm_common.conf
active_master_role      writer
<host default>
cluster_interface                       eth1  #由于我eth0配置的外网IP,eth1配置的10网段IP,所以我这里写的是eth1

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

replication_user                        slave
replication_password                    slave

agent_user                              mmm_agent
agent_password                          mmm_agent

mysql_port                              9188
</host>

<host db1>
ip                                      10.1.1.15
mode                                    master
peer                                    db2
</host>

<host db2>
ip                                      10.1.1.14
mode                                    master
peer                                    db1
</host>

<host db3>
ip                                      10.1.1.13
mode                                    slave
</host>
<role writer>
hosts                                   db1, db2
ips                                     10.1.1.20
mode                                    exclusive
</role>

<role reader>
hosts                                   db1, db2,db3
ips                                     10.1.1.23,10.1.1.22,10.1.1.21
mode                                    balanced
</role>

将db1的/etc/mysql-mmm/mmm_common.conf文件拷贝到db2,db3,db4相同的位置,拷贝方法自定义

db2:
# cat /etc/mysql-mmm/mmm_agent.conf
include mmm_common.conf
this db2
db3:
# cat /etc/mysql-mmm/mmm_agent.conf
include mmm_common.conf
this db3

5、启动MYSQL-MMM_AGENT
db1,db2,db3:
# /etc/init.d/mysql-mmm-agent start
Daemon bin: ‘/usr/sbin/mmm_agentd’
Daemon pid: ‘/var/run/mmm_agentd.pid’
Starting MMM Agent daemon… Ok

# /etc/init.d/mysql-mmm-agent status
Daemon bin: ‘/usr/sbin/mmm_agentd’
Daemon pid: ‘/var/run/mmm_agentd.pid’
Checking MMM Agent process: running.

[root@(155/15)~]# ps aux | grep mmm
root     26758  0.0  0.1  11524  6912 ?        S    14:52   0:00 mmm_agentd
root     26759  0.0  0.1  11568  7088 ?        S    14:52   0:00 mmm_agentd
root     26798  0.0  0.0   3920   672 pts/1    S+   14:55   0:00 grep mmm
[root@(155/15)~]# netstat -tulnp | grep mmm
tcp        0      0 10.1.1.15:9989              0.0.0.0:*                   LISTEN      26759/mmm_agentd

[root@(155/15)~]# /etc/init.d/mysql-mmm-agent –help
Daemon bin: ‘/usr/sbin/mmm_agentd’
Daemon pid: ‘/var/run/mmm_agentd.pid’
Usage: /etc/init.d/mysql-mmm-agent {start|stop|restart|status}

Note:如果不能启动,可以查看/var/log/mysql-mmm/mmm_agentd.log 文件的提示,或者启动时候的报错,最大的可能是perl对应模块没有安装成功,成功安装对应的模块就可以解决问题了。
6、配置MYSQL-MMM-MONITOR
创建mmm-monitor使用的mysql用户,在db1,db2,db3上
db1,db2,db3:
mysql>grant replication client on *.* to ‘mmm_monitor’@’10.1.1.12’ identified by ‘mmm_monitor’;

在实际使用中可以简化用户,让复制,agent,monitor使用同一个用户,注意权限就可以行了。下面配置monitor
db4:
# cat /etc/mysql-mmm/mmm_mon.conf
include mmm_common.conf

<monitor>
ip                                      10.1.1.12
pid_path                                /var/run/mmm_mond.pid
bin_path                                /usr/lib/mysql-mmm/
status_path                             /var/lib/misc/mmm_mond.status
ping_ips                                10.1.1.13, 10.1.1.14, 10.1.1.15
</monitor>

<host default>
monitor_user                    mmm_monitor
monitor_password                mmm_monitor
</host>

debug 0

7、启动MYSQL-MMM-MONITOR
# /etc/init.d/mysql-mmm-monitor start
Daemon bin: ‘/usr/sbin/mmm_mond’
Daemon pid: ‘/var/run/mmm_mond.pid’
Starting MMM Monitor daemon: Ok

[root@(140/12)~]# /etc/init.d/mysql-mmm-monitor –help
Daemon bin: ‘/usr/sbin/mmm_mond’
Daemon pid: ‘/var/run/mmm_mond.pid’
Usage: /etc/init.d/mysql-mmm-monitor {start|stop|restart|status}

# ps aux | grep mmm
root      2737  0.0  0.2  13764  8892 ?        S    15:10   0:00 mmm_mond
root      2738  1.3  0.9 108480 39992 ?        Sl   15:10   0:00 mmm_mond
root      2745  0.4  0.1  10676  6668 ?        S    15:10   0:00 perl /usr/lib/mysql-mmm//monitor/checker ping_ip
root      2748  0.5  0.1  12860  7776 ?        S    15:10   0:00 perl /usr/lib/mysql-mmm//monitor/checker mysql
root      2750  0.4  0.1  10676  6668 ?        S    15:10   0:00 perl /usr/lib/mysql-mmm//monitor/checker ping
root      2752  0.5  0.1  12860  7824 ?        S    15:10   0:00 perl /usr/lib/mysql-mmm//monitor/checker rep_backlog
root      2754  0.5  0.1  12860  7820 ?        S    15:10   0:00 perl /usr/lib/mysql-mmm//monitor/checker rep_threads

8、管理MYSQL-MMM-MONITOR

查看mmm集群节点的状态:
db4:

# mmm_control mode
ACTIVE

# mmm_control show
db1(10.1.1.15) master/AWAITING_RECOVERY. Roles:
db2(10.1.1.14) master/AWAITING_RECOVERY. Roles:
db3(10.1.1.13) slave/AWAITING_RECOVERY. Roles:
现在都处于AWATING_RECOVERY状态
我们将db1和db2两个master置于ONLINE状态
# mmm_control set_online db1
OK: State of ‘db1’ changed to ONLINE. Now you can wait some time and check its new roles!
# mmm_control set_online db2
OK: State of ‘db2’ changed to ONLINE. Now you can wait some time and check its new roles!
# mmm_control show
db1(10.1.1.15) master/ONLINE. Roles: reader(10.1.1.22), writer(10.1.1.20)
db2(10.1.1.14) master/ONLINE. Roles: reader(10.1.1.21), reader(10.1.1.23)
db3(10.1.1.13) slave/AWAITING_RECOVERY. Roles:。
现在mysql-mmm两个master的模式已经搭建完成,我们对所有节点做一次检查:
# mmm_control checks all
db2  ping         [last change: 2011/05/18 15:10:15]  OK
db2  mysql        [last change: 2011/05/18 15:10:15]  OK
db2  rep_threads  [last change: 2011/05/18 15:10:15]  OK
db2  rep_backlog  [last change: 2011/05/18 15:10:15]  OK: Backlog is null
db3  ping         [last change: 2011/05/18 15:10:15]  OK
db3  mysql        [last change: 2011/05/18 15:10:15]  OK
db3  rep_threads  [last change: 2011/05/18 15:10:15]  ERROR: Replication is not set up
db3  rep_backlog  [last change: 2011/05/18 15:10:15]  ERROR: Replication is not set up
db1  ping         [last change: 2011/05/18 15:10:15]  OK
db1  mysql        [last change: 2011/05/18 15:10:15]  OK
db1  rep_threads  [last change: 2011/05/18 15:10:15]  OK
db1  rep_backlog  [last change: 2011/05/18 15:10:15]  OK: Backlog is null

db1和db2已经正常了,只有db3复制是失败的,因为我们还没有配置,现在我们开始配置db3的复制,完成两个master多个slave的架构。

9、从2个MASTER架构扩展到2个MASTER多个SLAVE架构
现在的mysql-mmm集群还属于未使用状态,数据还是不能更新的,我们先从db1备份数据然后恢复到db3上,然后再db3开始配置复制:

Note:
这里存在一个问题,就是关于选择哪个db作为db3的主。由于网上关于两主多slave的架构的方案文档很少,我曾经在这里也犯过一个错误,所以现在给大家强调一下,如果大家觉得我所说的内容有问题,请反馈给我,谢谢。
那么是选择10.1.1.20作为主?因为10.1.1.20是永远在线并且唯一的ip?
错!选择10.1.1.20作为主是最严重的错误,虽然10.1.1.20会切换到其他机器,但是db3上,Master_Log_File:和Read_Master_Log_Pos却会出现问题,具体细节问题大家自己测试下吧。
选择其他三个虚拟IP也是不可以取的,所以我们最终选择db2或者db1的真实IP。
我以前就纠结是选择db1还是选择db2的真实ip作为db3的主呢?其实这个问 题不用纠结,mysql-mmm比我们想象中智能,他会在db3的master offline的时候,自动切换主到另外一个master服务器,也就说说db3的master是跟着writer角色迁移的,前提是你配置好了 mysql-mmm
我们就把db2配置成db3的主吧:

db2:
mysql> show master status\G
*************************** 1. row ***************************
File: mysql-bin.000002
Position: 582
Binlog_Do_DB:
Binlog_Ignore_DB:
1 row in set (0.00 sec)
db3:
mysql>CHANGE MASTER TO
MASTER_HOST=’10.1.1.14′,
MASTER_USER=’slave’,
MASTER_PASSWORD=’slave’,
MASTER_PORT=9188,
MASTER_LOG_FILE=’ mysql-bin.000002′,
MASTER_LOG_POS=582,
MASTER_CONNECT_RETRY=10;
mysql> start slave;

mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.1.1.14
Master_User: slave
Master_Port: 9188
Connect_Retry: 10
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 582
Relay_Log_File: zj14-relay-bin.000025
Relay_Log_Pos: 251
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 582
Relay_Log_Space: 550
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
1 row in set (0.00 sec)
重要:在mmm-monitor上查看各个节点的状态,将writer角色以外的主机的mysql都设置成read-only=1,不要设置错误哦。
# mmm_control show
db1(10.1.1.15) master/ONLINE. Roles: reader(10.1.1.22), writer(10.1.1.20)
db2(10.1.1.14) master/ONLINE. Roles: reader(10.1.1.21), reader(10.1.1.23)
db3(10.1.1.13) slave/AWAITING_RECOVERY. Roles:
# Role writer is assigned to it’s preferred host db1.

db2:
mysql> show global variables like ‘read_only’\G
*************************** 1. row ***************************
Variable_name: read_only
Value: OFF
1 row in set (0.00 sec)
mysql> set global read_only=on;
Query OK, 0 rows affected (0.00 sec)
db3:
mysql> set global read_only=on;
Query OK, 0 rows affected (0.00 sec)

然后再mmm-monitor上将db3的agent置于online状态
# mmm_control set_online db3
OK: State of ‘db3’ changed to ONLINE. Now you can wait some time and check its new roles!

# mmm_control set_online db3
OK: State of ‘db3’ changed to ONLINE. Now you can wait some time and check its new roles!
[root@(140/12)~]# mmm_control show
db1(10.1.1.15) master/ONLINE. Roles: reader(10.1.1.22), writer(10.1.1.20)
db2(10.1.1.14) master/ONLINE. Roles: reader(10.1.1.23)
db3(10.1.1.13) slave/ONLINE. Roles: reader(10.1.1.21)

# mmm_control checks all
db2  ping         [last change: 2011/05/18 15:10:15]  OK
db2  mysql        [last change: 2011/05/18 15:10:15]  OK
db2  rep_threads  [last change: 2011/05/18 15:10:15]  OK
db2  rep_backlog  [last change: 2011/05/18 15:10:15]  OK: Backlog is null
db3  ping         [last change: 2011/05/18 15:10:15]  OK
db3  mysql        [last change: 2011/05/18 21:56:41]  OK
db3  rep_threads  [last change: 2011/05/18 21:54:11]  OK
db3  rep_backlog  [last change: 2011/05/18 21:54:08]  OK: Backlog is null
db1  ping         [last change: 2011/05/18 15:10:15]  OK
db1  mysql        [last change: 2011/05/18 15:10:15]  OK
db1  rep_threads  [last change: 2011/05/18 15:10:15]  OK
db1  rep_backlog  [last change: 2011/05/18 15:10:15]  OK: Backlog is null
现在一切OK,我们准备测试吧。

四、测试
一、测试写入数据是否同步
在mmm-monitor上连接10.1.1.20,尝试写入数据,然后查看三个服务器是否同步数据。
1、 从writer角色的机器登陆自己的mysql,创建一个大家都可以登陆用户。
db3:
#mysql
mysql> grant all privileges on *.* to ‘andy’@’10.1.1.12’ identified by ‘andy’;
mysql>flush privileges;

2、 登陆db2和db3,查看是否存在此用户
db2:
#mysql
mysql> select user,host from mysql.user;
+————-+————-+
| user        | host        |
+————-+————-+
| andy        | 10.1.1.12 |
| mmm_agent   | 10.1.1.12   |
| mmm_monitor | 10.1.1.12   |
| mmm_agent   | 10.1.1.13   |
| slave       | 10.1.1.13   |
| mmm_agent   | 10.1.1.14   |
| mmm_agent   | 10.1.1.15   |
| slave       | 10.1.1.15   |
| web         | 10.1.1.8    |
| root        | 127.0.0.1   |
| root        | localhost   |
+————-+————-+
11 rows in set (0.00 sec)
db3:
mysql> select user,host from mysql.user;
+————-+——————+
| user        | host             |
+————-+——————+
| andy        | 10.1.1.12      |
| mmm_agent   | 10.1.1.12        |
| mmm_monitor | 10.1.1.12        |
| mmm_agent   | 10.1.1.13        |
| mmm_agent   | 10.1.1.14        |
| mmm_agent   | 10.1.1.15        |
| root        | 127.0.0.1        |
| root        | localhost        |
| root        | zj12.dq-game.com |
+————-+——————+
9 rows in set (0.00 sec)
3、 从db4登陆10.1.1.20往test.t表里添加数据
# mysql -uandy -p -h 10.1.1.20 -P9188
mysql> use test
Database changed
mysql> select * from t;
+—-+———–+
| id | name      |
+—-+———–+
|  1 | andy.feng |
|  2 | fh.cn     |
|  4 | mysql-mmm |
+—-+———–+
3 rows in set (0.00 sec)

mysql> insert into t(name) values (‘from-monitor’);
Query OK, 1 row affected (0.01 sec)

db2和db3:
mysql> select * from t;
+—-+————–+
| id | name         |
+—-+————–+
|  1 | andy.feng    |
|  2 | fh.cn        |
|  4 | mysql-mmm    |
|  5 | from-monitor |
+—-+————–+
4 rows in set (0.00 sec

二、测试WRITER故障切换
1、关闭现在的writer角色的mysql服务器,也就是现在的db1.
# killall mysqld
2、在monitor上检查节点的状态
# mmm_control show
db1(10.1.1.15) master/HARD_OFFLINE. Roles:
db2(10.1.1.14) master/ONLINE. Roles: reader(10.1.1.22), writer(10.1.1.20)
db3(10.1.1.13) slave/ONLINE. Roles: reader(10.1.1.21), reader(10.1.1.23)
已经检测到故障,自动切换了

# mmm_control checks all
db2  ping         [last change: 2011/05/18 15:10:15]  OK
db2  mysql        [last change: 2011/05/18 15:10:15]  OK
db2  rep_threads  [last change: 2011/05/18 22:27:41]  ERROR: Replication is broken
db2  rep_backlog  [last change: 2011/05/18 15:10:15]  OK: Backlog is null
db3  ping         [last change: 2011/05/18 15:10:15]  OK
db3  mysql        [last change: 2011/05/18 21:56:41]  OK
db3  rep_threads  [last change: 2011/05/18 22:27:50]  OK
db3  rep_backlog  [last change: 2011/05/18 21:54:08]  OK: Backlog is null
db1  ping         [last change: 2011/05/18 15:10:15]  OK
db1  mysql        [last change: 2011/05/18 22:27:38]  ERROR: Connect error (host = 10.1.1.15:9188, user = mmm_monitor)! Lost connection to MySQL server at ‘reading initial communication packet’, system error: 111
db1  rep_threads  [last change: 2011/05/18 15:10:15]  OK
db1  rep_backlog  [last change: 2011/05/18 15:10:15]  OK: Backlog is null

现在db3复制是正常的,db2的slave角色复制线程失败,db1的mysql连接失败

4、 在monitor上连接10.1.1.20写入数据
# mysql -uandy -p -h10.1.1.20 -P9188
mysql> use test
mysql> insert into t(name) values (‘from-monitor2’);
mysql> insert into t(name) values (‘from-monitor3’);

在db3上查看数据是否同步:
mysql> select * from t;
+—-+—————+
| id | name          |
+—-+—————+
|  1 | andy.feng     |
|  2 | fh.cn         |
|  4 | mysql-mmm     |
|  5 | from-monitor  |
|  7 | from-monitor2 |
|  8 | from-monitor3 |
+—-+—————+
6 rows in set (0.00 sec)
数据同步成功!!!
5、恢复db1,查看db1数据是否同步:
启动db1数据库
# mmm_control show
db1(10.1.1.15) master/AWAITING_RECOVERY. Roles:
db2(10.1.1.14) master/ONLINE. Roles: reader(10.1.1.22), writer(10.1.1.20)
db3(10.1.1.13) slave/ONLINE. Roles: reader(10.1.1.21), reader(10.1.1.23)
由于db1停机大于了60s,所以需要手动设置online
# mmm_control set_online db1
OK: State of ‘db1’ changed to ONLINE. Now you can wait some time and check its new roles!
连接上db1,查看数据是否完整:
db1:
mysql> use test;
Database changed
mysql> select * from t;
+—-+—————+
| id | name          |
+—-+—————+
|  1 | andy.feng     |
|  2 | fh.cn         |
|  4 | mysql-mmm     |
|  5 | from-monitor  |
|  7 | from-monitor2 |
|  8 | from-monitor3 |
+—-+—————+
6 rows in set (0.00 sec)
顺便看一下read_only状态
mysql> show global variables like ‘read_only’;
+—————+——-+
| Variable_name | Value |
+—————+——-+
| read_only     | OFF   |
+—————+——-+
1 row in set (0.00 sec)
 

三、测试db3的主是否会自动切换
db3:
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.1.1.14
                  Master_User: slave
                  Master_Port: 9188
                Connect_Retry: 10
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 65724106
               Relay_Log_File: zj12-relay-bin.000002
                Relay_Log_Pos: 10545
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
 Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 65724106
              Relay_Log_Space: 10699
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
1 row in set (0.00 sec)
现在db3的主是10.1.1.14(db2),是正常的情况,那么如果我们停掉db2会出现什么效果呢?
# mmm_control set_offline db2
OK: State of ‘db2’ changed to ADMIN_OFFLINE. Now you can wait some time and check all roles!
# mmm_control show
 db1(10.1.1.15) master/ONLINE. Roles: reader(10.1.1.23), writer(10.1.1.20)
 db2(10.1.1.14) master/ADMIN_OFFLINE. Roles:
 db3(10.1.1.13) slave/ONLINE. Roles: reader(10.1.1.21), reader(10.1.1.22)
我们来看看db3的情况
db3
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.1.1.15
                  Master_User: slave
                  Master_Port: 9188
                Connect_Retry: 10
              Master_Log_File: mysql-bin.000006
          Read_Master_Log_Pos: 65726122
               Relay_Log_File: zj12-relay-bin.000002
                Relay_Log_Pos: 2820
        Relay_Master_Log_File: mysql-bin.000006
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           Replicate_Do_Table:
       Replicate_Ignore_Table:
      Replicate_Wild_Do_Table:
 Replicate_Wild_Ignore_Table:
                   Last_Errno: 0
                   Last_Error:
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 65726122
              Relay_Log_Space: 2974
              Until_Condition: None
               Until_Log_File:
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File:
           Master_SSL_CA_Path:
              Master_SSL_Cert:
            Master_SSL_Cipher:
               Master_SSL_Key:
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error:
               Last_SQL_Errno: 0
               Last_SQL_Error:
1 row in set (0.00 sec)
db3已经自动把主切换到了db1,通过mmm_monitor观察,db3的复制是正确的。这个大家可以自己测试。
      四、测试角色优先配置的影响
先配置角色优先
先修改mmm-monitor的配置,让db1具有优先获取writer角色。
#vim /etc/mysql-mmm/mmm_common.conf
<role writer>
        hosts                                   db1, db2
        ips                                     10.1.1.20
        mode                                    exclusive
        prefer                                  db1
</role>
# /etc/init.d/mysql-mmm-monitor restart
# mmm_control show
 db1(10.1.1.15) master/ONLINE. Roles: reader(10.1.1.22), writer(10.1.1.20)
 db2(10.1.1.14) master/ONLINE. Roles: reader(10.1.1.21), reader(10.1.1.23)
 db3(10.1.1.13) slave/AWAITING_RECOVERY. Roles:
# Role writer is assigned to it’s preferred host db1.
我直接说结果吧,大家自己测试一下。
1、 当db1在线的时候,db1优先成为writer角色
由于db3是优先使用writer角色作为自己的master,所以db3的master也会自动迁移。
最后
很好,数据还是同步的,刚才我们临时修改了read_only值,但是没有修改my.cnf,所以现在就把read_only设置到所有的mysql服务器的my.cnf里[mysqld]里添加 read_only=1,下次重启时候,就会生效。

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据