mysql-mmm-2.2.1安裝手冊

MMM Installation Guide

Mysql Master-Master Replication Manager
(mysql-mmm installation Guide)
Version:mysql-mmm-2.2.1
作者:andy.feng
網名:FH.CN
Email:[email protected]
BLOG:http://linuxguest.blog.51cto.com

PDF版下載>>>>> 

目錄
一、安裝環境介紹 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,下次重啓時候,就會生效。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章