MySQL-MMM 高可用

MMM 簡介mysql

         MMM (Master-Master relication manager fro MySQL ,MySQK 主主複製管理器)是一套支持雙主故障切換和雙主平常管理的腳步程序。主要是用來監控和管理MySQL Master-Master (雙主)複製,雖然叫作雙主複製,到時業務上同一時刻值容許對一個主進行寫入,另外一臺備選主提供部分讀服務,以加速在主主切換時備選主的預熱。一方面實現了故障切換的功能,也可實現多個 Slave 的read 負載均衡。sql

關於MMM 高可用架構的說明以下:數據庫

        mmm_mon :監控進程,負載全部的監控工做,絕對和處理全部節點角色活動。此腳本須要在監管機上運行。vim

        mmm_agent:運行在每一個MySQL 服務器上的代理進程,完成監控的探針工做和執行簡單的遠程服務設置。此腳本須要在被監管機上運行。centos

        mmm_control:提供管理 mmm_mond 進程命令。緩存

        mysql-mmm 的監管端會提供多個虛擬IP(VIP),包括一個可寫VIP,多個可讀VIP,這些IP會綁定在可用MySQL 上,當某一臺MySQL 宕機時,監管會將VIP遷移到其餘 MySQL。服務器

案例環境網絡

本案例環境使用五臺服務器模擬搭建:架構

         主機                         操做系統                      IP 地址                           主要軟件負載均衡

mysql-master1         centos7x86_64             192.168.213.174              mysql-mmm*

mysql-master2         centos7x86_64              192.168.213.177              mysql-mmm*

mysql-slave 1            centos7x86_64              192.168.213.179              mysql-mmm*

mysql-slave 2            centos7x86_64              192.168.213.173              mysql-mmm*

mysql-monitor         centos7x86_64              192.168.213.178               mysql-mmm*

一. 搭建MySQL 多主多從模式

(1)在線下載安裝epel 源,五臺服務器都要安裝。

[root@master1 ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

#將aliyun 的 yum源下載到本地網絡yum 源

[root@master1 ~]# yum -y install epel-release                             #下載epel 源

[root@master1 ~]# yum clean all && yum makecache            #清空全部,從新設置原數據緩存

(2)下載 mariadb (mysql 的分支),關閉防火牆功能   (mariadb 只在主從4個服務器上安裝)

[root@master1 ~]# yum -y install mariadb-server mariadb

[root@master1 ~]# systemctl stop firewalld.service                 #關閉防火牆
[root@master1 ~]# setenforce 0

(3)修改 mariadb 的配置文件

[root@master1 ~]# vim /etc/my.cnf

[mysqld]
log_error=/var/lib/mysql/mysql.err                                          #錯誤日誌路徑

log=/var/lib/mysql/mysql_log.log                                              #通用查詢日誌
log_slow_queries=/var/lib/mysql_slow_queris.log                #慢查詢日誌路徑

binlog-ignore-db=mysql,information_schema                          #不須要同步的數據庫名稱
character_set_server=utf8                                                             #默認字符集爲 utf8
log_bin=mysql_bin                                                                           #啓用二進制日誌
server_id=1                                                                                        #服務器id , 注意:每臺服務器的id 都不同,確保惟一性
log_slave_updates=ture                                                                  #容許從服務器進行日誌更新

sync_binlog=1                                                                                    #容許從服務器同步二進制日誌
auto_increment_increment=2                                                        #字段一次遞增多少
auto_increment_offset=1                                                                #自增段的起始值

(4)啓動mysql 服務,並查看默認端口是否開啓

[root@master1 ~]# systemctl restart mariadb
[root@master1 ~]# netstat -ntap | grep 3306
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      41241/mysqld
     

(5)設置數據庫 root 用戶登陸的 密碼

[root@master1 ~]# mysqladmin -u root password '123'
[root@master1 ~]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.

          其餘三臺數據庫服務器安裝與配置與 master1 相同,配置文件的其餘參數都同樣,僅 server-id 不一樣。所以能夠在 master 1 服務器上傳配置文件,注意修改 server-id.

[root@master01 ~]# scp -r /etc/my.cnf root@192.168.213.177:/etc/my.cnf
[root@master01 ~]# scp -r /etc/my.cnf root@192.168.213.179:/etc/my.cnf
[root@master01 ~]# scp -r /etc/my.cnf root@192.168.213.173:/etc/my.cnf

二.配置 master 1 he master 2 主主模式

(1)先查看 log bin 日誌和pos 值

master 1

MariaDB [(none)]> show master status;
+------------------+----------+--------------+--------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB         |
+------------------+----------+--------------+--------------------------+
| mysql_bin.000003 |      554 |              | mysql,information_schema |
+------------------+----------+--------------+--------------------------+
1 row in set (0.00 sec)

master 2

MariaDB [(none)]> show master status;
+------------------+----------+--------------+--------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB         |
+------------------+----------+--------------+--------------------------+
| mysql_bin.000003 |      554 |              | mysql,information_schema |
+------------------+----------+--------------+--------------------------+
1 row in set (0.00 sec)

(2)master 1  和 master 2 互相提高訪問權限

在m1上爲m2授予從的權限

MariaDB [(none)]> grant replication slave on *.* to 'replication'@'192.168.213.%' identified by '123456';
Query OK, 0 rows affected (0.35 sec)

MariaDB [(none)]> change master to master_host='192.168.213.177',master_user='replication',master_password='123456',master_log_file='mysql_bin.000003',master_log_pos= 554;                                                                                                                                         #master 2 log bin 日誌 和pos值
Query OK, 0 rows affected (0.02 sec)

在m2上也要爲m1 受權從的權限

MariaDB [(none)]> grant replication slave on *.* to 'replication'@'192.168.213.%' identified by '123456';
Query OK, 0 rows affected (0.35 sec)

MariaDB [(none)]> change master to master_host='192.168.213.174',master_user='replication',master_password='123456',master_log_file='mysql_bin.000003',master_log_pos= 554;
Query OK, 0 rows affected (0.02 sec)

(3)開啓兩臺主主服務器同步功能,並查詢主從狀態

tart slave;   //開啓同步功能
show slave status\G;
     Slave_I0_Running: Yes                //這兩個選項參數必須爲yes
     Slave_SQL_Running: Yes

主主同步配置完成,查看狀態 Slave_IO  和 Slave_SQI 爲 yes ,說明主主同步成功

三.配置從服務器  Slave1 和Slave2,做爲 Master 1 的從庫

查看 master1  的狀態值

MariaDB [(none)]> show master status;
+------------------+----------+--------------+--------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB         |
+------------------+----------+--------------+--------------------------+
| mysql_bin.000003 |      554 |              | mysql,information_schema |
+------------------+----------+--------------+--------------------------+
1 row in set (0.00 sec)

在 slave1 和slave2 分別作

MariaDB [(none)]> change master to master_host='192.168.213.174',master_user='replication',master_password='123456',master_log_file='mysql_bin.000003',master_log_pos= 554;                    #master1 的ip
Query OK, 0 rows affected (0.02 sec)

開啓同步功能,並查看同步狀態

start slave;   //開啓同步功能
show slave status\G;
     Slave_I0_Running: Yes                //這兩個選項參數必須爲yes
     Slave_SQL_Running: Yes

四 .安裝配置 MySQL-MMM

(1)安裝MMM 。Centos 默認沒有 mysql-mmm 軟件包,使用epel 源,五臺服務器都要安裝 epel 和 MMM

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

(2)修改 MMM 的配置文件,系統中全部主機的該配置文件內容都是同樣的,包括監控主機 mysql-monitor

[root@master1 ~]# cd /etc/mysql-mmm/                                  #配置文件路徑
[root@master1 mysql-mmm]# vim mmm_common.conf

<host default>
     cluster_interface      ens33                                           #網卡改成 ens33
     pid_path                /run/mysql-mmm-agent.pid
     bin_path                /usr/libexec/mysql-mmm/
     replication_user        replicant                                        #Mariadb 給與權限的用戶
     replication_password    123456                                     #給與權限的用戶密碼
     agent_user              mmm_agent                                    #agent 客戶端代理用戶
     agent_password          123456                                        #代理用戶密碼
</host>

<host db1>                                    #主服務器 master1
     ip      192.168.213.174
     mode    master                      #狀態爲:主
     peer    db2                               #與主服務器master 2互相切換
</host>

<host db2>                                 #主服務器 master2
     ip      192.168.213.177          
     mode    master                      #狀態爲:主
     peer    db1
</host>

<host db3>                                   #從服務器 slave1
     ip      192.168.213.179
     mode    slave                            #狀態爲:slave
</host>

<host db4>                                       #從服務器 slave2
     ip      192.168.213.173
     mode    slave                               #狀態爲:slave
</host>

<role writer>                                      #寫操做
     hosts   db1, db2                             #主服務器master1 和master2 具備寫的操做

     ips     192.168.213.100                   #指定虛擬IP
     mode    exclusive                   #指定模式,db1 和db2 只能存在一個IP地址,只能有一臺主機進行寫入操做
</role>

<role reader>                                       #讀操做
     hosts  db3, db4
     ips     192.168.213.110, 192.168.213.120          #虛擬IP
     mode    balanced                                   #負載均衡,讀取操做會從 db3 和db4 主機進行
</role>

(3)遠程複製,將 mmm_common.conf 配置文件傳送到其餘4臺主機上

[root@master1 mysql-mmm]# scp mmm_common.conf root@192.168.213.177:/etc/mysql-mmm/
The authenticity of host '192.168.213.177 (192.168.213.177)' can't be established.
ECDSA key fingerprint is SHA256:KM7QwLupjrfzZ2YQdMOoGKJtIUgtz2agvwTzZOPHu2k.
ECDSA key fingerprint is MD5:f1:32:f7:7f:b7:eb:4e:9e:2e:fa:7e:8a:56:88:fe:c1.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.213.177' (ECDSA) to the list of known hosts.
root@192.168.213.177's password:    #對方root 用戶登陸密碼
mmm_common.conf                                           100%  842   329.6KB/s   00:00   
[root@master1 mysql-mmm]# scp mmm_common.conf root@192.168.213.179:/etc/mysql-mmm/
The authenticity of host '192.168.213.179 (192.168.213.179)' can't be established.
ECDSA key fingerprint is SHA256:HtLFtvYxQF5ER0eA1uKE8VgRx038LWpDYBbp1S1CrJ8.
ECDSA key fingerprint is MD5:23:41:18:56:8e:ed:f3:65:b1:5f:96:11:e9:11:cb:29.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.213.179' (ECDSA) to the list of known hosts.
root@192.168.213.179's password:
mmm_common.conf                                           100%  842   376.0KB/s   00:00   
[root@master1 mysql-mmm]# scp mmm_common.conf root@192.168.213.173:/etc/mysql-mmm/
The authenticity of host '192.168.213.173 (192.168.213.173)' can't be established.
ECDSA key fingerprint is SHA256:w910JWPfehgM09d+OlOiC6q61NjELLHDh5LWojkuYL0.
ECDSA key fingerprint is MD5:94:a5:a1:e0:4d:14:cc:3c:ff:8c:24:e5:3e:e1:2b:cd.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.213.173' (ECDSA) to the list of known hosts.
root@192.168.213.173's password:
mmm_common.conf                                           100%  842   565.0KB/s   00:00  

[root@master1 mysql-mmm]# scp mmm_common.conf root@192.168.213.178:/etc/mysql-mmm/
The authenticity of host '192.168.213.178 (192.168.213.178)' can't be established.
ECDSA key fingerprint is SHA256:ABSTPGOHvqKvUsfwD/uf5ESPpdT1RjvucRpzMqcUuzI.
ECDSA key fingerprint is MD5:f5:3a:8c:8b:1e:d5:a3:33:24:32:03:2d:4d:3e:e8:68.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.213.178' (ECDSA) to the list of known hosts.
root@192.168.213.178's password:
mmm_common.conf                                           100%  842   277.8KB/s   00:00 

(4)在4臺數據庫上爲監控 monitor 受權訪問

MariaDB [(none)]> grant super,replication client,process on *.* to 'mmm_agent'@'192.168.213.%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> grant replication client on *.* to 'mmm_monitor'@'192.168.213.%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;                #刷新權限
Query OK, 0 rows affected (0.00 sec)

(5)在數據庫主機上須要編輯   mmm_agent.conf 配置文件,根據不一樣主機修改成不一樣的值

[root@master1 mysql-mmm]# vim mmm_agent.conf

this db1               #分別修改成 db1 、db2 、db3 、db4

五 .監控服務器 monitor 配置

[root@localhost ~]# cd /etc/mysql-mmm/
[root@localhost mysql-mmm]# vim mmm_mon.conf

<monitor>

ping_ips            192.168.213.174,192.168.213.177,192.168.213.179,192.168.213.173      #監控全部數據庫的 IP 地址
auto_set_online     10                                         #設置10秒上線

<host default>
     monitor_user        mmm_monitor                  #用戶名
     monitor_password    123456                          #密碼

六 .啓動監控和代理

(1)在全部數據庫服務器啓動 mysql-mmm-agent

[root@master1 mysql-mmm]# systemctl start mysql-mmm-agent.service
[root@master1 mysql-mmm]# systemctl enable mysql-mmm-agent.service

(2)啓動監控服務 mysql-mmm-monitor

[root@localhost mysql-mmm]# systemctl start mysql-mmm-monitor.service

(3)測試羣集

[root@localhost mysql-mmm]# mmm_control show
  db1(192.168.213.174) master/ONLINE. Roles: writer(192.168.213.100)                  #虛擬IP
   db2(192.168.213.177) master/ONLINE. Roles:
   db3(192.168.213.179) slave/ONLINE. Roles: reader(192.168.213.110)
   db4(192.168.213.173) slave/ONLINE. Roles: reader(192.168.213.120)

    #對應的真是的IP  

檢查狀態都須要所有 OK

[root@localhost mysql-mmm]# mmm_control checks all
db4  ping         [last change: 2018/09/10 10:50:52]  OK
db4  mysql        [last change: 2018/09/10 10:50:52]  OK
db4  rep_threads  [last change: 2018/09/10 10:50:52]  OK
db4  rep_backlog  [last change: 2018/09/10 10:50:52]  OK: Backlog is null
db2  ping         [last change: 2018/09/10 10:50:52]  OK
db2  mysql        [last change: 2018/09/10 10:50:52]  OK
db2  rep_threads  [last change: 2018/09/10 10:50:52]  OK
db2  rep_backlog  [last change: 2018/09/10 10:50:52]  OK: Backlog is null
db3  ping         [last change: 2018/09/10 10:50:52]  OK
db3  mysql        [last change: 2018/09/10 10:50:52]  OK
db3  rep_threads  [last change: 2018/09/10 10:50:52]  OK
db3  rep_backlog  [last change: 2018/09/10 10:50:52]  OK: Backlog is null
db1  ping         [last change: 2018/09/10 10:50:52]  OK
db1  mysql        [last change: 2018/09/10 10:50:52]  OK
db1  rep_threads  [last change: 2018/09/10 10:50:52]  OK
db1  rep_backlog  [last change: 2018/09/10 10:50:52]  OK: Backlog is null

七 .故障測試

(1)停掉主 db1 的數據庫,等待幾秒,能夠看到數據庫 db1 處於 HARD_OFFLINE. Roles (離線狀態),檢測不到數據庫的存在

[root@master1 mysql-mmm]# systemctl stop mariadb.service

[root@localhost mysql-mmm]# mmm_control show

db1(192.168.213.174) master/HARD_OFFLINE. Roles:
   db2(192.168.213.177) master/ONLINE. Roles: writer(192.168.213.100)
   db3(192.168.213.179) slave/ONLINE. Roles: reader(192.168.213.110)
   db4(192.168.213.173) slave/ONLINE. Roles: reader(192.168.213.120)

宕掉MySQL  數據庫,虛擬IP 會所有在另外一臺正常數據庫上

再啓動主db1 的數據庫

[root@master1 mysql-mmm]# systemctl start mariadb.service        #啓動mysql  數據庫

[root@localhost mysql-mmm]# mmm_control show
   db1(192.168.213.174) master/AWAITING_RECOVERY. Roles:                  #等待回覆
   db2(192.168.213.177) master/ONLINE. Roles: writer(192.168.213.100)
   db3(192.168.213.179) slave/ONLINE. Roles: reader(192.168.213.110)
   db4(192.168.213.173) slave/ONLINE. Roles:reader(192.168.213.120)

[root@localhost mysql-mmm]# mmm_control show
  db1(192.168.213.174) master/ONLINE. Roles:                                         #正常狀態(在線)
   db2(192.168.213.177) master/ONLINE. Roles: writer(192.168.213.100)
   db3(192.168.213.179) slave/ONLINE. Roles: reader(192.168.213.110)
   db4(192.168.213.173) slave/ONLINE. Roles: reader(192.168.213.120)

雖然主服務器 db1 重新上線,可是虛擬IP 並不會復位,

(3)停掉從服務器 db3 的數據庫

[root@localhost mysql-mmm]#  systemctl stop mariadb.service

[root@localhost mysql-mmm]# mmm_control show
# Warning: agent on host db3 is not reachable
   db1(192.168.213.174) master/ONLINE. Roles:
   db2(192.168.213.177) master/ONLINE. Roles: writer(192.168.213.100)
   db3(192.168.213.179) slave/HARD_OFFLINE. Roles:    #從服務器db3 關閉,虛擬IP 會所有飄移到靈位一臺從服務器db4
   db4(192.168.213.173) slave/ONLINE. Roles: reader(192.168.213.110), reader(192.168.213.120)

啓動從 db3 的數據庫

[root@localhost mysql-mmm]#  systemctl start mariadb.service

[root@localhost mysql-mmm]# mmm_control show
# Warning: agent on host db3 is not reachable
   db1(192.168.213.174) master/ONLINE. Roles:
   db2(192.168.213.177) master/ONLINE. Roles: writer(192.168.213.100)
   db3(192.168.213.179) slave/AWAITING_RECOVERY. Roles:
   db4(192.168.213.173) slave/ONLINE. Roles: reader(192.168.213.110), reader(192.168.213.120)

[root@localhost mysql-mmm]# mmm_control show
  db1(192.168.213.174) master/ONLINE. Roles:
   db2(192.168.213.177) master/ONLINE. Roles: writer(192.168.213.100)
   db3(192.168.213.179) slave/ONLINE. Roles: reader(192.168.213.120)            #從服務器上線後,虛擬IP又恢復
   db4(192.168.213.173) slave/ONLINE. Roles: reader(192.168.213.110)

八 .測試數據同步情況

以監控服務器做爲客戶端進行遠程登陸 mysql 服務進行測試,在監控服務器上安裝 mariadb 數據庫

[root@localhost mysql-mmm]# yum install mariadb-server mariadb –y

在主數據庫 db1 上爲監控服務器受權登陸MariaDB 數據庫

MariaDB [(none)]> grant all on *.* to 'testdba'@'192.168.213.178' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

在監控服務器上使用虛擬IP 登陸MariaDB 數據庫,進行數據插入,建立一個數據庫

[root@localhost mysql-mmm]# mysql -utestdba -p -h 192.168.213.100
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.

MariaDB [school]> create database mogo;
Query OK, 1 row affected (0.01 sec)

登陸其餘四臺數據庫,均可以查看到剛建立的數據庫 mogo,證實羣集同步成功


MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mogo               || mysql              || performance_schema || test               |+--------------------+

相關文章
相關標籤/搜索