天天看點

MySQL雙主高可用架構之MMM實戰

MMM簡介:

MySQL本身沒有提供replication failover的解決方案,通過MMM方案能實作伺服器的故障轉移,進而實作mysql的高可用。

官方網站為: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)

架構原理圖:

<a href="http://img1.51cto.com/attachment/201211/205947512.jpg" target="_blank"></a>

一、環境基礎配置

1. 設定hosts解析

三台伺服器配置如下:

cat &gt;&gt;/etc/hosts&lt;&lt;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

三、安裝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

說明:也可以下載下傳源碼包安裝:

tar xf mysql-mmm-2.2.1.tar.gz

cd mysql-mmm-2.2.1

make install

四、配置MySQL-M1和MySQL-M2主主模式

1.首先建立三個賬号

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

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

mysql&gt; 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-M1和MySQL-M2之間有複制,是以隻要在一台伺服器上執行就可以了,不過要在MySQL-MON上執行後面兩條!

2.配置主主模式

2.1 把MySQL-M1伺服器作為MySQL-M2伺服器主

在MySQL-M1伺服器操作:

mysql&gt; 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&gt; 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&gt;  start slave;

Query OK, 0 rows affected (0.00 sec)

mysql&gt; 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-bin.000003 |      605 |              |                  |

在MySQL-M1伺服器上操作:

mysql&gt; 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&gt; start slave;

mysql&gt; show slave status\G;

                 Master_Host: 192.168.8.33

         Read_Master_Log_Pos: 605

            Slave_IO_Running: Yes

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

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

1. 在三台伺服器修改mmm_common.conf配置檔案(三台伺服器此配置檔案内容相同)

修改後的内容如下:

active_master_role      writer

&lt;host default&gt;

   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   #前面建立的代理賬号密碼

&lt;/host&gt;

&lt;host MySQL-M1&gt;

   ip      192.168.8.32   #MySQL-M1伺服器IP

   mode    master

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

&lt;host MySQL-M2&gt;

   ip      192.168.8.33

   peer    MySQL-M1

&lt;role writer&gt;

   hosts   MySQL-M1, MySQL-M2   #能夠作為Write的伺服器

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

mode    exclusive            #排它模式

&lt;/role&gt;

&lt;role reader&gt;

   hosts   MySQL-M1, MySQL-M2   #作為Reader的伺服器

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

說明:mode  exclusive

這個地方有兩種模式:

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

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

通常情況下writer是exclusive,reader是balanced

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配置檔案

this MySQL-M2

4. 在MySQL-MON伺服器上配置mmm_mon.conf配置檔案

&lt;monitor&gt;

   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

# 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

&lt;/monitor&gt;

   monitor_password    mmm_monitor    #前面建立的監控賬号密碼

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-M1和MySQL-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-M1的mysqld服務停掉

[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     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資訊:

 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: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; 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

   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、當Roles為reader和write的MySQL-M1發生故障的時候,就将reader和writer角色從MySQL-M1上移除,并标記為HARD_OFFLINE狀态,由Roles為reader的MySQL-M2取代該伺服器,并配置設定writer角色和浮動IP,此時MySQL-M2為主伺服器,承擔伺服器的讀寫代理。當MySQL-M1恢複後,mysql-mmm會自動配置設定Roles為reader,标記為ONLINE狀态,并和MySQL-M2一起承擔讀壓力,此時MySQL-M1為slave,提供隻讀代理功能。

這裡隻測試下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 [&lt;host&gt;|all [&lt;check&gt;|all]] - show checks status   #顯示檢查狀态,包括(ping、mysql、rep_threads、rep_backlog)

   set_online &lt;host&gt;                 - set host &lt;host&gt; online   #設定某host為online狀态

   set_offline &lt;host&gt;                - set host &lt;host&gt; offline  #設定某host為offline狀态

   mode                              - print current mode.      #列印目前的模式,是ACTIVE、MANUAL、PASSIVE(預設是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] &lt;role&gt; &lt;host&gt; - move exclusive role &lt;role&gt; to host &lt;host&gt;   #更改host的模式,比如更改處于slave的mysql資料庫角色為writer

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

   set_ip &lt;ip&gt; &lt;host&gt;                - set role with ip &lt;ip&gt; to host &lt;host&gt;    help    #為host設定ip,隻有passive模式的時候才允許更改!

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

1、MMM不允許兩台Master同時挂掉

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

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

本文轉自 yntmdr 51CTO部落格,原文連結:http://blog.51cto.com/yntmdr/1595815,如需轉載請自行聯系原作者

繼續閱讀