您好,登錄后才能下訂單哦!
MMM 簡介
MMM (Master-Master relication manager fro MySQL ,MySQK 主主復制管理器)是一套支持雙主故障切換和雙主日常管理的腳步程序。主要是用來監控和管理MySQL Master-Master (雙主)復制,雖然叫做雙主復制,到時業務上同一時刻值允許對一個主進行寫入,另一臺備選主提供部分讀服務,以加速在主主切換時備選主的預熱。一方面實現了故障切換的功能,也可實現多個 Slave 的read 負載均衡。
關于MMM 高可用架構的說明如下:
mmm_mon :監控進程,負載所有的監控工作,絕對和處理所有節點角色活動。此腳本需要在監管機上運行。
mmm_agent:運行在每個MySQL 服務器上的代理進程,完成監控的探針工作和執行簡單的遠程服務設置。此腳本需要在被監管機上運行。
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+OlOiC6q61NjELLHDh6LWojkuYL0.
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 |
+--------------------+
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。