Juszeil Conception

Juszeil Conception

  • D
  • ï
  • s T
  • a

«2014 - 6»
@GT|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
TODAY

Hide Banner | LOGIN
2023-3-24 |

BLOG

Total found 1 articles on 2014-6-20.

電 腦 技 術 2014-6-20 23:16:49

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


MMM簡介:

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

架構原理圖:


 

一、環境基礎配置

1. 設置hosts解析

三台服務器配置如下:

cat >>/etc/hosts<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://go.rritw.com/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-M1和MySQL-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-M1和MySQL-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 statusG

*************************** 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 statusG;

*************************** 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

    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   #前面創建的代理账號密碼

    ip      192.168.8.32   #MySQL-M1服務器IP

    mode    master

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

    ip      192.168.8.33

    mode    master

    peer    MySQL-M1

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

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

    mode    exclusive            #排它模式

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

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

    mode    balanced             #平衡模式

說明: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配置文件 

修改後的內容如下:

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

 

    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_user        mmm_monitor    #前面創建的監控账號

    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-mmm-agent defaults

ENABLED=1

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

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

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

[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: 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: 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、當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,提供只讀代理功能。

2、當Roles为reader的MySQL-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 [|all [|all]] - show checks status   #顯示檢查狀態,包括(ping、mysql、rep_threads、rep_backlog)

    set_online                  - set host online   #設置某host为online狀態

    set_offline                 - set host 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] - move exclusive role to host    #更改host的模式,比如更改處於slave的mysql數據庫角色为writer

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

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


Source URL: http://www.verydemo.com/demo_c152_i42088.html
Relate Post : MySQL Order By Rand()效率 MySQL max_connections MySQL block host connection MYSQL master master replication error (Error_code: 1236) MYSQL JOIN ON Fix MySQL connection error – Host ” is blocked because of many connection errors Exporting and importing data between MySQL and Microsoft excel How To Measure MySQL Query Performance with mysqlslap Website with HA and Clustering 初學MySQL必須知道的事
Comments :
No Comments

Post your comment:


Post your comment by Guest :
Verify Code :


Back To Top

Find Me

Powered By 2013-2015 ©. Juszeil Conception version 2.0
Queries Executed : 0.0257 seconds