【MySQL運維】MySQL雙主高可用架構之MMM實戰

MMM簡介:


MMMMaster-Master Replication Manager for MySQLmysql主主複製管理器),是關於mysql主主複製配置的監控、故障轉移和管理的一套可伸縮的腳本套件(在任何時候只有一個節點可以被寫入),這個套件也能基於標準的主從配置的任意數量的從服務器進行讀負載均衡,所以你可以用它來在一組居於複製的服務器啓動虛擬ip,除此之外,它還有實現數據備份、節點之間重新同步功能的腳本。
MySQL
本身沒有提供replication failover的解決方案,通過MMM方案能實現服務器的故障轉移,從而實現mysql的高可用。
MMM
項目來自 Google
http://code.google.com/p/mysql-master-master


官方網站爲:http://mysql-mmm.org


MMM主要功能由下面三個腳本提供
mmm_mond    :負責所有的監控工作的監控守護進程,決定節點的移除等等
mmm_agentd  :運行在mysql服務器上的代理守護進程,通過簡單遠程服務集提供給監控節點


mmm_control :通過命令行管理mmm_mond進程

關於此架構的優缺點:
優點:安全性、穩定性高,可擴展性好,當主服務器掛掉以後,另一個主立即接管,其他的從服務器能自動切換,不用人工干預。

缺點:至少三個節點,對主機的數量有要求,需要實現讀寫分離,可以在程序擴展上比較難實現。同時對主從(雙主)同步延遲要求比較高!因此不適合數據安全非常嚴格的場合。

實用場所:高訪問量,業務增長快,並且要求實現讀寫分離的場景。

環境:
MMM_Monitor: 192.168.8.31-----(MySQL-MON)
MySQL_Master1: 192.168.8.32-----(MySQL-M1)


MySQL_Master2: 192.168.8.33-----(MySQL-M2)


VIP_Write: 192.168.8.30-----(VIP0)


VIP_Read1: 192.168.8.34-----(VIP1)


VIP_Read2: 192.168.8.35-----(VIP2)

架構原理圖:


205947512.jpg


一、環境基礎配置



1. 設置hosts解析


三臺服務器配置如下:


cat >>/etc/hosts<<EOF
192.168.8.31    MySQL-MON
192.168.8.32    MySQL-M1


192.168.8.33    MySQL-M2


EOF

二、安裝配置mysql

具體安裝過過程略,如果不會安裝 mysql的可以不用看本教程了!


這裏說下my.cnf配置文件的細節:


MySQL-M1的配置:


server-id = 12


#log-slave-updates


#sync_binlog = 1


log-bin = /data/mysql/binlog/mysql-bin


auto-increment-increment = 2


auto-increment-offset = 2


relay-log=mysql-relay            


relay-log-index=mysql-relay.index


MySQL-M2的配置:


server-id = 13


#log-slave-updates


#sync_binlog = 1


log-bin = /data/mysql/binlog/mysql-bin


auto-increment-increment = 2


auto-increment-offset = 2


relay-log=mysql-relay            


relay-log-index=mysql-relay.index


三、安裝mysql-mmm

在三臺服務器安裝


wget http://download.fedoraproject.org/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm


rpm -ivh epel-release-5-4.noarch.rpm


yum -y install mysql-mmm*


[root@MySQL-M1 mysql-mmm]# rpm -qa |grep mysql-mmm


mysql-mmm-2.2.1-1.el5


mysql-mmm-agent-2.2.1-1.el5


mysql-mmm-tools-2.2.1-1.el5


mysql-mmm-monitor-2.2.1-1.el5


說明:也可以下載源碼包安裝:
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 xf mysql-mmm-2.2.1.tar.gz
cd mysql-mmm-2.2.1
make install

四、配置MySQL-M1MySQL-M2主主模式


1.首先創建三個賬號

mysql> grant file, replication salve on *.* to 'repl'@'192.168.8.%' identified by "repl";


mysql> grant process, super, replication client on *.* to 'mmm_agent'@'192.168.8.%' identified by 'mmm_agent';


mysql> grant replication client on *.* to "mmm_monitor"@"192.168.8.%" identified by "mmm_monitor";

說明:
第一個賬號repl(複製賬號),是用於主主複製
第二個賬號mmm_agent(代理賬號),是mmm agent用來變成只讀模式和同步master
第三個賬號mmm_monitor(監聽賬號),是mmm monitor服務器用來對mysql服務器做健康檢查的


要注意一點是:由於MySQL-M1MySQL-M2之間有複製,所以只要在一臺服務器上執行就可以了,不過要在MySQL-MON上執行後面兩條!

2.配置主主模式

2.1 MySQL-M1服務器作爲MySQL-M2服務器主

MySQL-M1服務器操作:


mysql> show master status;


+------------------+----------+--------------+------------------+


| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |


+------------------+----------+--------------+------------------+


| mysql-bin.000003 |      107 |              | mysql            |


+------------------+----------+--------------+------------------+


1 row in set (0.00 sec)


然後在MySQL-M2服務器操作:


mysql> change master to master_host='192.168.8.32',master_user='repl',master_password='repl',master_log_file='mysql-bin.000003',master_log_pos=107;


Query OK, 0 rows affected (0.07 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: 192.168.8.32


                 Master_User: repl


                 Master_Port: 3306


               Connect_Retry: 60


             Master_Log_File: mysql-bin.000003


         Read_Master_Log_Pos: 107


              Relay_Log_File: mysql-relay.000002


               Relay_Log_Pos: 253


       Relay_Master_Log_File: mysql-bin.000003


           Slave_IO_Running: Yes


           Slave_SQL_Running: Yes
       .....................................................
後面的信息省略..
       .....................................................

2.2 MySQL-M2服務器作爲MySQL-M1服務器主


MySQL-M2服務器上操作:


mysql> show master status;


+------------------+----------+--------------+------------------+


| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |


+------------------+----------+--------------+------------------+


| mysql-bin.000003 |      605 |              |                  |


+------------------+----------+--------------+------------------+


1 row in set (0.00 sec)


MySQL-M1服務器上操作:


mysql> change master to master_host='192.168.8.33',master_user='repl',master_password='repl',master_log_file='mysql-bin.000003',master_log_pos=605;


Query OK, 0 rows affected (0.06 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: 192.168.8.33


                 Master_User: repl


                 Master_Port: 3306


               Connect_Retry: 60


             Master_Log_File: mysql-bin.000003


         Read_Master_Log_Pos: 605


              Relay_Log_File: mysql-relay.000002


               Relay_Log_Pos: 253


       Relay_Master_Log_File: mysql-bin.000003


            Slave_IO_Running: Yes


           Slave_SQL_Running: Yes
       .....................................................
後面的信息省略..
       .....................................................

OK...主主模式配置成功!主主複製同步測試這裏不再說明,接着下面的步驟。


五、配置MMM監控、代理服務

1. 在三臺服務器修改mmm_common.conf配置文件(三臺服務器此配置文件內容相同)

修改後的內容如下:


active_master_role      writer


<host default>


   cluster_interface       eth0


   pid_path                /var/run/mysql-mmm/mmm_agentd.pid


   bin_path                /usr/libexec/mysql-mmm/


   replication_user        repl     #前面創建的複製賬號


   replication_password    repl     #前面創建的複製賬號密碼


   agent_user              mmm_agent   #前面創建的代理賬號


   agent_password          mmm_agent   #前面創建的代理賬號密碼


</host>


<host MySQL-M1>


   ip      192.168.8.32   #MySQL-M1服務器IP


   mode    master


   peer    MySQL-M2       #MySQL-M2服務器主機名


</host>


<host MySQL-M2>


   ip      192.168.8.33


   mode    master


   peer    MySQL-M1


</host>


<role writer>


   hosts   MySQL-M1, MySQL-M2   #能夠作爲Write的服務器


   ips     192.168.8.30         #Write節點虛擬IP,應用的寫請求將直接連接到這個IP


mode    exclusive            #排它模式


</role>


<role reader>


   hosts   MySQL-M1, MySQL-M2   #作爲Reader的服務器


   ips     192.168.8.34, 192.168.8.35  #Reader節點虛擬IP,應用的讀請求將直接連接到這些IP


mode    balanced             #平衡模式


</role>


說明:mode  exclusive


這個地方有兩種模式:


exclusive:在這種模式下任何時候只能一個主機擁有該角色


balanced : 該模式下可以多個主機同時擁有此角色。


通常情況下writerexclusivereaderbalanced


2. MySQL-M1服務器上修改mmm_agent.conf配置文件


修改後的內容如下:


include mmm_common.conf

# The 'this' variable refers to this server.  Proper operation requires


# that 'this' server (db1 by default), as well as all other servers, have the


# proper IP addresses set in mmm_common.conf.


this MySQL-M1


3. MySQL-M2服務器上修改mmm_agent.conf配置文件

修改後的內容如下:


include mmm_common.conf

# The 'this' variable refers to this server.  Proper operation requires


# that 'this' server (db1 by default), as well as all other servers, have the


# proper IP addresses set in mmm_common.conf.


this MySQL-M2


4. MySQL-MON服務器上配置mmm_mon.conf配置文件

修改後的內容如下:


include mmm_common.conf


<monitor>


   ip                  127.0.0.1


   pid_path            /var/run/mysql-mmm/mmm_mond.pid


   bin_path            /usr/libexec/mysql-mmm


   status_path         /var/lib/mysql-mmm/mmm_mond.status


ping_ips            192.168.8.32,192.168.8.33    #可以ping的真實agent服務器的IP


auto_set_online     10    #發現節點丟失則過10秒進行切換


# The kill_host_bin does not exist by default, though the monitor will


   # throw a warning about it missing.  See the section 5.10 "Kill Host


   # Functionality" in the PDF documentation.


   #


   # kill_host_bin     /usr/libexec/mysql-mmm/monitor/kill_host


   #


</monitor>



<host default>


   monitor_user        mmm_monitor    #前面創建的監控賬號


   monitor_password    mmm_monitor    #前面創建的監控賬號密碼


</host>



debug 0


5.啓動代理(默認是啓用,這裏只是說明下)

[root@MySQL-M1 mysql-mmm]# cat /etc/default/mysql-mmm-agent


# mysql-mmm-agent defaults


ENABLED=1


[root@MySQL-M2 mysql-mmm]# cat /etc/default/mysql-mmm-agent


# mysql-mmm-agent defaults


ENABLED=1

六、啓動各服務器的相關服務

MySQL-M1MySQL-M2服務器上啓動


/etc/init.d/mysql-mmm-agent start


MySQL-MON服務器上啓動


/etc/init.d/mysql-mmm-monitor start


MySQL-MON監控機上查看MMM狀態信息:


[root@MySQL-MON mysql-mmm]# mmm_control show


 MySQL-M1(192.168.8.32) master/ONLINE. Roles: reader(192.168.8.35), writer(192.168.8.30)


 MySQL-M2(192.168.8.33) master/ONLINE. Roles: reader(192.168.8.34)



[root@MySQL-MON mysql-mmm]# mmm_control checks all


MySQL-M2  ping         [last change: 2012/10/15 05:07:35]  OK


MySQL-M2  mysql        [last change: 2012/10/15 05:07:35]  OK


MySQL-M2  rep_threads  [last change: 2012/10/15 05:07:35]  OK


MySQL-M2  rep_backlog  [last change: 2012/10/15 05:07:35]  OK: Backlog is null


MySQL-M1  ping         [last change: 2012/10/15 05:07:35]  OK


MySQL-M1  mysql        [last change: 2012/10/15 05:07:35]  OK


MySQL-M1  rep_threads  [last change: 2012/10/15 05:07:35]  OK


MySQL-M1  rep_backlog  [last change: 2012/10/15 05:07:35]  OK: Backlog is null


[root@MySQL-MON mysql-mmm]# mmm_control mode


ACTIVE

下面分別查看各服務器的日誌信息:


[root@MySQL-M1 mysql-mmm]# cat /var/log/mysql-mmm/mmm_agentd.log


2012/10/15 05:06:06  INFO We have some new roles added or old rules deleted!


2012/10/15 05:06:06  INFO Added:   reader(192.168.8.35), writer(192.168.8.30)


[root@MySQL-M2 mysql-mmm]# cat /var/log/mysql-mmm/mmm_agentd.log


2012/10/16 14:53:51  INFO We have some new roles added or old rules deleted!


2012/10/16 14:53:51  INFO Added:   reader(192.168.8.34)


[root@MySQL-MON ~]# cat /var/log/mysql-mmm/mmm_mond.log


2012/10/15 05:07:36 FATAL Couldn't open status file '/var/lib/mysql-mmm/mmm_mond.status': Starting up without status information.


2012/10/15 05:08:38 FATAL State of host 'MySQL-M2' changed from AWAITING_RECOVERY to ONLINE because of auto_set_online(10 seconds). It was in state AWAITING_RECOVERY for 10 seconds


2012/10/15 05:08:38 FATAL State of host 'MySQL-M1' changed from AWAITING_RECOVERY to ONLINE because of auto_set_online(10 seconds). It was in state AWAITING_RECOVERY for 10 seconds


下面再看下各服務器的服務進程信息:


[root@MySQL-MON ~]# ps aux |grep mmm


root     19176  0.0  2.8 115784 14320 ?        S    05:07   0:00 mmm_mond


root     19178  0.3 14.4 331756 71556 ?        Sl   05:07   0:18 mmm_mond


root     19185  0.1  1.8 105824  9376 ?        S    05:07   0:06 perl /usr/libexec/mysql-mmm/monitor/checker ping_ip


root     19188  0.0  2.2 137644 10940 ?        S    05:07   0:05 perl /usr/libexec/mysql-mmm/monitor/checker mysql


root     19190  0.0  1.8 105824  9384 ?        S    05:07   0:02 perl /usr/libexec/mysql-mmm/monitor/checker ping


root     19192  0.1  2.2 137644 10984 ?        S    05:07   0:07 perl /usr/libexec/mysql-mmm/monitor/checker rep_backlog


root     19194  0.1  2.2 137644 10984 ?        S    05:07   0:07 perl /usr/libexec/mysql-mmm/monitor/checker rep_threads


root     19308  0.0  0.1  61228   720 pts/1    R+   06:42   0:00 grep mmm


[root@MySQL-M1 mysql-mmm]# ps aux |grep mmm


root      1371  0.0  0.1  61228   724 pts/1    R+   06:40   0:00 grep mmm


root     24228  0.0  2.2 106096 11068 ?        S    04:58   0:00 mmm_agentd


root     24230  0.2  2.6 140148 13204 ?        S    04:58   0:16 mmm_agentd


下面再看下VIP綁定信息:

七、模擬宕機切換測試

1.現在把MySQL-M1mysqld服務停掉


[root@MySQL-M1 mysql-mmm]# service mysqld stop


Shutting down MySQL.                                       [  OK  ]


[root@MySQL-M1 mysql-mmm]# ps aux |grep mysqld |grep -v grep



[root@MySQL-MON ~]# mmm_control show


 MySQL-M1(192.168.8.32) master/HARD_OFFLINE. Roles:


 MySQL-M2(192.168.8.33) master/ONLINE. Roles: reader(192.168.8.34), reader(192.168.8.35), writer(192.168.8.30)


2. 現在再恢復MySQL-M1


[root@MySQL-M1 mysql-mmm]# service mysqld start


Starting MySQL.....                                        [  OK  ]


[root@MySQL-M1 mysql-mmm]# ps aux |grep mysqld |grep -v grep


root     19328  1.2  0.2  66116  1340 pts/1    S    09:51   0:00 /bin/sh /opt/webserver/mysql/bin/mysqld_safe --datadir=/data/mysql/data/ --pid-file=/data/mysql/data//MySQL-M1.pid


mysql    20139  7.0 10.3 420060 51348 pts/1    Sl   09:51   0:00 /opt/webserver/mysql/bin/mysqld --basedir=/opt/webserver/mysql --datadir=/data/mysql/data/ --plugin-dir=/opt/webserver/mysql/lib/plugin --user=mysql --log-error=/data/mysql/logs/mysql.err --open-files-limit=10240 --pid-file=/data/mysql/data//MySQL-M1.pid --socket=/tmp/mysql.sock --port=3306


然後再到MySQL-MON上查看MMM信息:


[root@MySQL-MON ~]# mmm_control show


 MySQL-M1(192.168.8.32) master/ONLINE. Roles: reader(192.168.8.34)


 MySQL-M2(192.168.8.33) master/ONLINE. Roles: reader(192.168.8.35), writer(192.168.8.30)


可以看到MySQL-M1恢復後又OK了。。。。但是MySQL-M1此時不再提供寫代理了,只提供讀代理了!


接下來看下VIP綁定信息:

[root@MySQL-M1 mysql-mmm]# ip a |grep eth0

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000

   inet 192.168.1.32/24 brd 192.168.1.255 scope global eth0

   inet 192.168.8.34/32 scope global eth0

[root@MySQL-M2 mysql-mmm]# ip a |grep eth0

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000

   inet 192.168.8.33/24 brd 192.168.8.255 scope global eth0

   inet 192.168.8.35/32 scope global eth0

   inet 192.168.8.30/32 scope global eth0

總結:mmm_mond監控各mysql-server的運行狀態


1、當RolesreaderwriteMySQL-M1發生故障的時候,就將readerwriter角色從MySQL-M1上移除,並標記爲HARD_OFFLINE狀態,由RolesreaderMySQL-M2取代該服務器,並分配writer角色和浮動IP,此時MySQL-M2爲主服務器,承擔服務器的讀寫代理。當MySQL-M1恢復後,mysql-mmm會自動分配Rolesreader,標記爲ONLINE狀態,並和MySQL-M2一起承擔讀壓力,此時MySQL-M1slave,提供只讀代理功能。


2、當RolesreaderMySQL-M1發生故障,就將reader的角色從MySQL-MMM上移除,並標記爲HARD_OFFLINE狀態。當該MySQL-M1恢復後又會自動分配reader角色給該服務器,標記爲ONLINE狀態,並和MySQL-M2一起承擔讀壓力。



這裏只測試下MySQL-MMM故障轉移,有興趣的朋友還要以測試下當主主複製出現問題時,會導致MMM出現什麼問題!


附錄:mmm_control命令相關參數說明

[root@MySQL-MON ~]# /usr/sbin/mmm_control help


Valid commands are:


   help                              - show this message   #查看幫助信息


   ping                              - ping monitor     #ping監控,用於監控檢測agent服務器


   show                              - show status     #查看狀態信息


   checks [<host>|all [<check>|all]] - show checks status   #顯示檢查狀態,包括(pingmysqlrep_threadsrep_backlog)


   set_online <host>                 - set host <host> online   #設置某hostonline狀態


   set_offline <host>                - set host <host> offline  #設置某hostoffline狀態


   mode                              - print current mode.      #打印當前的模式,是ACTIVEMANUALPASSIVE(默認是ACTIVE模式)


   set_active                        - switch into active mode.  #更改爲active模式


   set_manual                        - switch into manual mode.  #更改爲manual模式


   set_passive                       - switch into passive mode. #更改爲passive模式


   move_role [--force] <role> <host> - move exclusive role <role> to host <host>   #更改host的模式,比如更改處於slavemysql數據庫角色爲writer


                                       (Only use --force if you know what you are doing!)


   set_ip <ip> <host>                - set role with ip <ip> to host <host>    help    #host設置ip,只有passive模式的時候才允許更改!


最後再總結MMM的兩點不足:

1、MMM不允許兩臺Master同時掛掉

2、MMM對主主複製之間的延遲控制比較嚴格

後期博文發寫一篇關於MHA高可用架構的切換實驗。。。。






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章