基於MMM實現MariaDB的高可用

一、MMM

1、簡介

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

2、MMM的功能

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

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

3、MMM的優缺點及應用場景

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

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

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

二、MMM架構原理圖

wKioL1Ngw7KzkqVEAAA4RTFe4wQ449.png

三、資源配置

1、服務器列表

服務器IP主機名server id
monitoring host172.16.7.100monitor-
master 1172.16.7.200db11
master 2172.16.7.201db22
master 3172.16.7.202db33

2、虛擬IP列表

IProledescription
172.16.7.1write對就用程序連接的VIP進行寫操作
172.16.7.2read


對就用程序連接的VIP進行讀操作

172.16.7.3read

四、MMM的實現

1、配置master 1

(1)修改/etc/my.cnf配置文件

server-id       = 1
datadir = /mydata/data
log-bin = /mydata/binglogs/master-bin
relay_log = /mydata/relaylogs/relay
binlog_format=mixed
thread_concurrency = 4
log-slave-updates
sync_binlog=1
auto_increment_increment=2
auto_increment-offset=1

(2)爲master2 和 slave 授權複製用戶

MariaDB [(none)]> grant replication slave,replication client on *.* to 'repluser'@'172.16.7.201' identified by 'repluser';
MariaDB [(none)]> grant replication slave,replication client on *.* to 'repluser'@'172.16.7.202' identified by 'repluser';

(3)查看狀態信息,從服務器連接主服務器時使用

MariaDB [(none)]> show master status;
+-------------------+----------+--------------+------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000001 |      755 |              |                  |
+-------------------+----------+--------------+------------------+

2、配置master 2

(1)修改/etc/my.cnf配置文件

server-id       = 2
datadir = /mydata/data
log-bin = /mydata/binglogs/master-bin
relay_log = /mydata/relaylogs/relay
binlog_format=mixed
thread_concurrency = 4
log-slave-updates
sync_binlog=1
auto_increment_increment=2
auto_increment-offset=2

(2)爲master1 和 slave 授權複製用戶

MariaDB [(none)]> grant replication slave,replication client on *.* to 'repluser'@'172.16.7.200' identified by 'repluser';
MariaDB [(none)]> grant replication slave,replication client on *.* to 'repluser'@'172.16.7.202' identified by 'repluser';

(3)查看狀態信息,從服務器連接主服務器時使用

MariaDB [(none)]> show master status;
+-------------------+----------+--------------+------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000001 |      755 |              |                  |
+-------------------+----------+--------------+------------------+

(4)master 2 連接 master 1

MariaDB [(none)]> change master to master_host='172.16.7.200',master_user='repluser',master_password='repluser',master_log_file='master-bin.000001',master_log_pos=755;
MariaDB [(none)]>
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.16.7.200
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000001
          Read_Master_Log_Pos: 755
               Relay_Log_File: relay.000003
                Relay_Log_Pos: 536
        Relay_Master_Log_File: master-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: 755
              Relay_Log_Space: 823
              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:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 1
               Master_SSL_Crl:
           Master_SSL_Crlpath:
                   Using_Gtid: No
                  Gtid_IO_Pos:

(5)master 1 連接 master 2

MariaDB [(none)]> change master to master_host='172.16.7.201',master_user='repluser',master_password='repluser',master_log_file='master-bin.000001',master_log_pos=755;
Query OK, 0 rows affected (0.02 sec)
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.02 sec)
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.16.7.201
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000002
          Read_Master_Log_Pos: 327
               Relay_Log_File: db1-relay-bin.000003
                Relay_Log_Pos: 615
        Relay_Master_Log_File: master-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: 327
              Relay_Log_Space: 954
              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:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 2
               Master_SSL_Crl:
           Master_SSL_Crlpath:
                   Using_Gtid: No
                  Gtid_IO_Pos:

(6)測試master 1 與master 2 是否可以正常主從同步

=========== master 1 ================
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)
MariaDB [(none)]> create database hlbr;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| hlbr               |
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)
=========== master 2 ================
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| hlbr               |    #同步成功
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.02 sec)

(7)測試master 2 與master 1 是否可以正常主從同步

   我在master 2 上創建了一個bynr庫,可以同步到master 1 上,爲了減小篇幅,這裏就不貼圖了。

   經過(6),(7)的測試,說明雙主已然成功

3、配置slave

(1)修改/etc/my.cnf配置文件

server-id       = 3
datadir = /mydata/data
log-bin = /mydata/binglogs/master-bin
relay_log = /mydata/relaylogs/relay
binlog_format=mixed
thread_concurrency = 4
log-slave-updates
sync_binlog=1

(2)slave連接master 1

MariaDB [(none)]> change master to master_host='172.16.7.200',master_user='repluser',master_password='repluser',master_log_file='master-bin.000001',master_log_pos=755;
Query OK, 0 rows affected (0.05 sec)
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.16.7.200
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000001
          Read_Master_Log_Pos: 1261
               Relay_Log_File: relay.000002
                Relay_Log_Pos: 1042
        Relay_Master_Log_File: master-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: 1261
              Relay_Log_Space: 1329
              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:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 1
               Master_SSL_Crl:
           Master_SSL_Crlpath:
                   Using_Gtid: No
                  Gtid_IO_Pos:

(3)再次測試,在master 1 上創建數據庫,去slave上查看

====================== master 1 =====================
MariaDB [(none)]> create database hhht;
Query OK, 1 row affected (0.02 sec)
======================== slave ======================
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| bynr               |
| hhht               |
| hlbr               |    #所有的全部同步成功
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+

4、安裝配置mysql-mmm

   因爲monitor主機負責所有的監控工作,決定節點的移除等等,所以它必須要得到所有master和slave的授權;且每臺數據庫服務器上都需要安裝mysql-mmm-agent,通過簡單遠程服務集提供給監控節點

(1)在3臺mysql服務器上安裝mysql-mmm-agent

因爲有好多依賴關係,所以選擇yum安裝

[root@db1 ~]# yum -y install mysql-mmm-agent

(2)在3臺mysql服務器上爲monitor授權

用戶descriptionprivileges
monitor user用於mmm檢查mysql服務器健康狀況的用戶replication client
agent user用於mmm代理爲只讀模式和複製等的用戶super,replication client,process
replication user複製的用戶replication slave

接下來要做的就是爲上表中的用戶授權給監控主機,由於我們的主主、主從已經做好了,權限也已經授過了,所以上表中的第三個就不需重複操作了,而且我們只需在主服務器上授權剩下的兩個就好了,它會自動同步到另外兩臺mysql服務器上的

MariaDB [(none)]> grant replication client on *.* to 'monitor'@'172.16.7.100' identified by 'monitor';
MariaDB [(none)]> grant super,replication client,process on *.* to 'agent'@'172.16.7.100' identified by 'agent';
MariaDB [(none)]> flush privileges;

(3)在monitor節點上安裝mmm

[root@monitor ~]# yum -y install mysql-mmm*

 ①、配置/etc/mysql-mmm/mmm_common.conf

[root@monitor ~]# vim /etc/mysql-mmm/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        repluser    #授權複製用戶
    replication_password    repluser    #密碼
    agent_user              agent       #代理用戶
    agent_password          agent       #代理用戶密碼
</host>
<host db1>
    ip      172.16.7.200
    mode    master            #主的
    peer    db1
</host>
<host db2>
    ip      172.16.7.201
    mode    master
    peer    db2
</host>
<host db3>
    ip      172.16.7.202
    mode    slave              #從的
</host>
<role writer>
    hosts   db1, db2
    ips     172.16.7.1
    mode    exclusive           #exclusive表示排它
</role>
<role reader>
    hosts   db2, db3
    ips     172.16.7.2, 172.16.7.3
    mode    balanced            #balanced表示均衡
</role>

  ②、拷貝此文件到3臺mysql服務器

   這個不是數據庫中的數據,不能同步了,只能手動來了

[root@monitor ~]# scp /etc/mysql-mmm/mmm_common.conf [email protected]:/etc/mysql-mmm/   #3個節點都需這麼做

  ③、在monitor上修改mmm_mon.conf文件

[root@monitor ~]# vim /etc/mysql-mmm/mmm_mon.conf
include mmm_common.conf
<monitor>
    ip                  172.16.7.100    #監控主機的ip
    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            172.16.7.200, 172.16.7.201, 172.16.7.202 #各數據庫服務器的ip
    auto_set_online     60
    # 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        monitor    #監控用戶的用戶名
    monitor_password    monitor    #密碼
</host>
debug 0     #如果程序無序正常監控,可使用debug 1進行排查

(4)在每個DB上修改mmm_agent.conf

[root@db1 ~]# vim /etc/mysql-mmm/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 db1    #這一行是標記此主機的角色(引用mmm_common.conf中的host段),當前主機是db幾,這裏就要改爲this db幾

5、啓動MMM並測試

(1)啓動mysql-mmm-agent

[root@db1 ~]# service mysql-mmm-agent start
Starting MMM Agent Daemon:                                 [  OK  ]
#每個DB上的mysql-mmm-agent都要啓動

(2)啓動monitor監控程序

[root@monitor ~]# chkconfig mysql-mmm-monitor on
[root@monitor ~]# service mysql-mmm-monitor start
Starting MMM Monitor Daemon:                               [  OK  ]

(3)在監控主機查看監控狀態

[root@monitor ~]# service mysql-mmm-monitor status
mmm_mond (pid  1586) is running...
[root@monitor ~]# mmm_control show
  db1(172.16.7.200) master/ONLINE. Roles: writer(172.16.7.1)
  db2(172.16.7.201) master/ONLINE. Roles: reader(172.16.7.3)
  db3(172.16.7.202) slave/ONLINE. Roles: reader(172.16.7.2)

(4)設置db1離線,再查看狀態

[root@monitor ~]# mmm_control set_offline db1    #設置db1離線
OK: State of 'db1' changed to ADMIN_OFFLINE. Now you can wait some time and check all roles!
[root@monitor ~]# mmm_control show
  db1(172.16.7.200) master/ADMIN_OFFLINE. Roles:     #db1已經離線,VIP轉移到了db2上
  db2(172.16.7.201) master/ONLINE. Roles: reader(172.16.7.3), writer(172.16.7.1)
  db3(172.16.7.202) slave/ONLINE. Roles: reader(172.16.7.2)

在開始的時候,我們設置了一個寫IP:172.16.7.1和兩個讀IP:172.16.7.2、172.16.7.3,由(3)和(4)可以看出,在三個節點都正常的情況下,寫IP是在master 1上,當master 1掛了的話,寫IP就飄到master 2上了,這就解決了mysql數據庫的單點故障,通過MMM實現了mysql數據庫的高可用




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