一、MMM 概述
1. 什么是 MMM
??MMM(Master-Master replication manager for MySQL,MySQL 主主復(fù)制管理器)是一套支持雙主故障切換和雙主日常管理的腳本程序。MMM 使用 Perl 語言開發(fā),主要從來監(jiān)控和管理 MySQL Master-Master(雙主)復(fù)制,雖然叫做雙主復(fù)制,但是業(yè)務(wù)上同一時刻只允許一個主進行寫入,另一臺備選主上提供部分讀服務(wù),以加速在主主切換時備選主的預(yù)熱,可以說 MMM 這套腳本程序一方面實現(xiàn)了故障切換的功能,另一方面其內(nèi)部附加的工具也可以實現(xiàn)多個 Slave 的 read 負載均衡。
2. 應(yīng)用場景
?MMM 提供了自動和手動兩種方式移除一組服務(wù)器中復(fù)制延遲較高的服務(wù)器的虛擬 ip,同時它還可以備份數(shù)據(jù),實現(xiàn)兩節(jié)點之間的數(shù)據(jù)同步等。由于 MMM 無法完全保證數(shù)據(jù)的一致性,所以 MMM 適用于對數(shù)據(jù)的一致性要求不是很高的,但是又想最大程度地保證業(yè)務(wù)可用性的場景。對于那些對數(shù)據(jù)的一致性要求很高的業(yè)務(wù),非常不建議采用 MMM 這種高可用架構(gòu)。
?
3. MMM 特點
- MMM 是一套靈活的腳本程序
- 基于 perl 語言實現(xiàn)
- 用來對 mysql replication 進行監(jiān)控和故障遷移
- 管理 MySQL Master-Master 復(fù)制的配置
3.1 mmm故障切換流程? ??
Monitor 檢測到 Master1 連接失敗
Monitor 發(fā)送 set_offline 指令到 Master1 的 Agent
Master1 Agent 如果存活,下線寫 VIP,嘗試把 Master1 設(shè)置為 read_only=1
Moniotr 發(fā)送 set_online 指令到 Master2
Master2 Agent 接收到指令,執(zhí)行 select master_pos_wait() 等待同步完畢
Master2 Agent 上線寫 VIP,把 Master2 節(jié)點設(shè)為 read_only=0
Monitor 發(fā)送更改同步對象的指令到各個 Slave 節(jié)點的 Agent
各個 Slave 節(jié)點向新 Master 同步數(shù)據(jù)
?
4. 關(guān)于 MMM 高可用架構(gòu)的說明
mmm_mon:監(jiān)控進程,負責(zé)所有的監(jiān)控工作,決定和處理所有節(jié)點角色活動。此腳本需要在監(jiān)管機上運行。
mmm_agent:運行在每個 MySQL 服務(wù)器上的代理進程,完成監(jiān)控的探針工作和執(zhí)行簡單的遠端服務(wù)設(shè)置。此腳本需要在被監(jiān)管機上運行。
mmm_control:一個簡單的腳本,提供管理 mmm_mond 進程的命令。
mysql-mmm 的監(jiān)管端會提供多個虛擬 IP(VIP),包括一個可寫 VIP,多個可讀 VIP,通過監(jiān)管的管理,這些 IP 會綁定在可用 MySQL 之上,當(dāng)某一臺 MySQL 宕機時,監(jiān)管會將 VIP 遷移至其他 MySQL。
?
5. 用戶及授權(quán)
??在整個監(jiān)管過程中,需要在 MySQL 中添加相關(guān)授權(quán) yoghurt,以便讓 MySQL 可以支持監(jiān)理機的維護。授權(quán)的用戶包括一個 mmm_monitor 用戶和一個 mmm_agent 用戶,如果想使用 MMM 的備份工具則還需要添加一個 mmm_tools 用戶。
?
二、案例環(huán)境
1. 服務(wù)器配置
2. 服務(wù)器環(huán)境
systemctl stop firewalld && systemctl disable firewalld
setenforce 0
?
3 修改主機名
hostnamectl set-hostname master1
su
?
master1 master2 slave1 slave2 monitor
三、案例實施
1. 搭建 MySQL 多主多從架構(gòu)
(1) master1、master2、slave1、slave2 節(jié)點安裝 mysql
(2) 修改 master1 配置文件
[client]
port = 3306
default-character-set=utf8
socket=/usr/local/mysql/mysql.sock
[mysql]
port = 3306
default-character-set=utf8
socket=/usr/local/mysql/mysql.sock
auto-rehash
[mysqld]
user = mysql
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port = 3306
character-set-server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket=/usr/local/mysql/mysql.sock
server-id = 1
log-error=/usr/local/mysql/data/mysql_error.log
general_log=ON
general_log_file=/usr/local/mysql/data/mysql_general.log
slow_query_log=ON
slow_query_log_file=mysql_slow_query.log
long_query_time=5
binlog-ignore-db=mysql,information_schema
log_bin=mysql_bin
log_slave_updates=true
sync_binlog=1
innodb_flush_log_at_trx_commit=1
auto_increment_increment=2
auto_increment_offset=1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
重啟 mysqld
systemctl restart mysqld
?
參數(shù)說明
...... server-id = 1 #每臺 Mysql 主機的 server-id 不能相同 log-error=/usr/local/mysql/data/mysql_error.log #錯誤日志 general_log=ON #通用查詢?nèi)罩?general_log_file=/usr/local/mysql/data/mysql_general.log slow_query_log=ON #慢查詢?nèi)罩?slow_query_log_file=mysql_slow_query.log long_query_time=5 binlog-ignore-db=mysql,information_schema #不需要同步的庫名 log_bin=mysql_bin #開啟二進制日志用于主從數(shù)據(jù)復(fù)制 log_slave_updates=true #允許slave從master復(fù)制數(shù)據(jù)時可以寫入到自己的二進制日志 sync_binlog=1 #"雙1設(shè)置",MySQL 在每寫一次二進制日志時都會同步到磁盤中去 innodb_flush_log_at_trx_commit=1 #"雙1設(shè)置",每次事務(wù)提交時MySQL都會把緩存的數(shù)據(jù)寫入日志文件,并且刷到磁盤中去 auto_increment_increment=2 #自增字段一次遞增多少 auto_increment_offset=1 #自增字段的起始值
(3) 修改其他三臺 mysql 并重啟服務(wù)
把配置文件復(fù)制到其他 3 臺數(shù)據(jù)庫服務(wù)器并重啟 mysql 服務(wù)器
注意:配置文件中的 server-id 不可相同,需要修改
(4) 配置主主復(fù)制,兩臺主服務(wù)器相互復(fù)制
① 在兩臺主服務(wù)器上都執(zhí)行授予從的權(quán)限,從服務(wù)器上不需要執(zhí)行
master1 服務(wù)器(192.168.10.20
mysql> grant replication slave on *.* to 'replication'@'192.168.10.%' identified by '123456';
mysql> flush privileges;
mysql> show master status;
+------------------+----------+--------------+--------------------------+-----------------
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Se
+------------------+----------+--------------+--------------------------+-----------------
| mysql_bin.000001 | 1023 | | mysql,information_schema |
+------------------+----------+--------------+--------------------------+-----------------
1 row in set (0.00 sec)
master2 服務(wù)器(192.168.10.30)
mysql> grant replication slave on *.* to 'replication'@'192.168.10.%' identified by '123456';
mysql> flush privileges;
mysql> show master status;
+------------------+----------+--------------+--------------------------+-----------------
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Se
+------------------+----------+--------------+--------------------------+-----------------
| mysql_bin.000001 | 1023 | | mysql,information_schema |
+------------------+----------+--------------+--------------------------+-----------------
1 row in set (0.00 sec)
② 在 master1 上配置同步
192.168.10.20
change master to master_host='192.168.10.30',master_user='replication',master_password='123456',master_log_file='mysql_bin.000001',master_log_pos=1023; start slave; show slave status\G; #查看 IO 和 SQL 線程是不是 YES,位置偏移量對不對
③ 在 master2 上配置同步
192.168.10.30
change master to master_host='192.168.10.20',master_user='replication',master_password='123456',master_log_file='mysql_bin.000001',master_log_pos=1023;
start slave;
show slave status\G;
#查看 IO 和 SQL 線程是不是 YES,位置偏移量對不對
(5) 配置主從復(fù)制,在兩臺從服務(wù)器上做
① slave1 服務(wù)器
192.168.10.40
change master to master_host='192.168.10.20',master_user='replication',master_password='123456',master_log_file='mysql_bin.000001',master_log_pos=1023;
start slave;
show slave status\G;
?
② slave2 服務(wù)器
192.168.10.50
change master to master_host='192.168.10.20',master_user='replication',master_password='123456',master_log_file='mysql_bin.000001',master_log_pos=1023;
start slave;
show slave status\G;
?
(6) 測試主主、主從同步情況
2. 安裝配置 MySQL-MMM
(1) 在所有服務(wù)器上安裝 MySQL-MMM
注:若本地倉庫中無以上軟件,需先為各服務(wù)器配置在線源倉庫。
yum -y install epel-release && yum -y install mysql-mmm*
?
(2) 在 master1 上對 MySQL-MMM 進行配置
192.168.10.20
[root@master1 ~]# cd /etc/mysql-mmm/
[root@master1 mysql-mmm]# cp mmm_common.conf mmm_common.conf.bak
#修改配置文件前,先備份
[root@master1 mysql-mmm]# vim mmm_common.conf
active_master_role writer
<host default>
cluster_interface ens33
pid_path /run/mysql-mmm-agent.pid
bin_path /usr/libexec/mysql-mmm/
replication_user replication
##指定主主、主從復(fù)制用戶,要與前面一致
replication_password 123456
agent_user mmm_agent
##指定monitor代理進程的用戶名
agent_password 123456
</host>
<host db1>
ip 192.168.10.20
mode master
peer db2
##peer設(shè)置同級數(shù)據(jù)庫
</host>
<host db2>
ip 192.168.10.30
mode master
peer db1
</host>
<host db3>
ip 192.168.10.40
mode slave
</host>
<host db4>
ip 192.168.10.50
mode slave
</host>
<role writer>
hosts db1, db2
ips 192.168.10.200
##設(shè)定寫VIP
mode exclusive
#只有一個 host 可以進行寫操作模式
</role>
<role reader>
hosts db3, db4
ips 192.168.10.201, 192.168.10.202
##設(shè)定讀VIP
mode balanced
##多個 slave 主機可以進行讀操作模式
</role>
(3) 把配置文件復(fù)制到其他 4 臺主機
所有主機該配置文件內(nèi)容都是相同的
scp mmm_common.conf root@192.168.10.30:/etc/mysql-mmm/
scp mmm_common.conf root@192.168.10.40:/etc/mysql-mmm/
scp mmm_common.conf root@192.168.10.50:/etc/mysql-mmm/
scp mmm_common.conf root@192.168.10.90:/etc/mysql-mmm/
(4) 修改所有數(shù)據(jù)庫服務(wù)器的代理配置文件 mmm_agent.conf
master1
[root@master1 ~]# vim /etc/mysql-mmm/mmm_agent.conf
?
include mmm_common.conf
this db1
##根據(jù)不同的主機分別修改為db1/db2/db3/db4,默認配置為db1,因此master1無需修改
?
master2
[root@master2 ~]# vim /etc/mysql-mmm/mmm_agent.conf
include mmm_common.conf
this db2
slave1
[root@slave ~]# vim /etc/mysql-mmm/mmm_agent.conf include mmm_common.conf this db3
slave 2
[root@slave2 ~]# vim /etc/mysql-mmm/mmm_agent.conf
?
include mmm_common.conf
this db4
?
(5) 在 monitor 監(jiān)控服務(wù)器上修改監(jiān)控配置文件 mmm_mon.conf
monitor 服務(wù)器(192.168.10.90)
[root@monitor ~]# vim /etc/mysql-mmm/mmm_mon.conf
include mmm_common.conf
<monitor>
ip 127.0.0.1
pid_path /run/mysql-mmm-monitor.pid
bin_path /usr/libexec/mysql-mmm
status_path /var/lib/mysql-mmm/mmm_mond.status
ping_ips 192.168.10.20,192.168.10.30,192.168.10.40,192.168.10.50
##指定所有數(shù)據(jù)庫服務(wù)器的IP
auto_set_online 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>
<host default>
monitor_user mmm_monitor
##指定mmm_monitor的用戶名
monitor_password 123456
##指定mmm_monitor的密碼
</host>
debug 0
(6) 在所有數(shù)據(jù)庫上為 mmm_agent(代理進程)授權(quán)
所有數(shù)據(jù)庫執(zhí)行下列語句
grant super,replication client,process on *.* to 'mmm_agent'@'192.168.10.%' identified by '123456';
flush privileges;
?
?(7) 在所有數(shù)據(jù)庫上為 mmm_monitor(監(jiān)控進程)授權(quán)
grant replication client on *.* to 'mmm_monitor'@'192.168.10.%' identified by '123456';
flush privileges;
?
(8) 在所有數(shù)據(jù)庫服務(wù)器上啟動 mysql-mmm-agent
systemctl start mysql-mmm-agent.service && systemctl enable mysql-mmm-agent.service
?
(9) 在 monitor 服務(wù)器上啟動 mysql-mmm-monitor
systemctl start mysql-mmm-monitor.service && systemctl enable mysql-mmm-monitor.service
?
(10) 在 monitor 服務(wù)器上測試群集
① 查看各節(jié)點的情況
[root@monitor ~]#mmm_control show
? db1(192.168.10.20) master/ONLINE. Roles: writer(192.168.10.200)
? db2(192.168.10.30) master/ONLINE. Roles:?
? db3(192.168.10.40) slave/ONLINE. Roles: reader(192.168.10.202)
? db4(192.168.10.50) slave/ONLINE. Roles: reader(192.168.10.201)
?
② 檢測監(jiān)控功能是否完善
[root@monitor ~]#mmm_control checks all
db4 ?ping ? ? ? ? [last change: 2021/11/04 16:13:20] ?OK
db4 ?mysql ? ? ? ?[last change: 2021/11/04 16:13:20] ?OK
db4 ?rep_threads ?[last change: 2021/11/04 16:13:20] ?OK
db4 ?rep_backlog ?[last change: 2021/11/04 16:13:20] ?OK: Backlog is null
db2 ?ping ? ? ? ? [last change: 2021/11/04 16:13:20] ?OK
db2 ?mysql ? ? ? ?[last change: 2021/11/04 16:13:20] ?OK
db2 ?rep_threads ?[last change: 2021/11/04 16:13:20] ?OK
db2 ?rep_backlog ?[last change: 2021/11/04 16:13:20] ?OK: Backlog is null
db3 ?ping ? ? ? ? [last change: 2021/11/04 16:13:20] ?OK
db3 ?mysql ? ? ? ?[last change: 2021/11/04 16:13:20] ?OK
db3 ?rep_threads ?[last change: 2021/11/04 16:13:20] ?OK
db3 ?rep_backlog ?[last change: 2021/11/04 16:13:20] ?OK: Backlog is null
db1 ?ping ? ? ? ? [last change: 2021/11/04 16:13:20] ?OK
db1 ?mysql ? ? ? ?[last change: 2021/11/04 16:13:20] ?OK
db1 ?rep_threads ?[last change: 2021/11/04 16:13:20] ?OK
db1 ?rep_backlog ?[last change: 2021/11/04 16:13:20] ?OK: Backlog is null
?
③ 指定綁定 VIP 的主機
[root@monitor ~]#mmm_control move_role writer db2
OK: Role 'writer' has been moved from 'db1' to 'db2'. Now you can wait some time and check new roles info!
[root@monitor ~]#mmm_control show
? db1(192.168.10.20) master/ONLINE. Roles:?
? db2(192.168.10.30) master/ONLINE. Roles: writer(192.168.10.200)
? db3(192.168.10.40) slave/ONLINE. Roles: reader(192.168.10.202)
? db4(192.168.10.50) slave/ONLINE. Roles: reader(192.168.10.201)[root@monitor ~]#mmm_control move_role writer db1
OK: Role 'writer' has been moved from 'db2' to 'db1'. Now you can wait some time and check new roles info!
[root@monitor ~]#mmm_control show
? db1(192.168.10.20) master/ONLINE. Roles: writer(192.168.10.200)
? db2(192.168.10.30) master/ONLINE. Roles:?
? db3(192.168.10.40) slave/ONLINE. Roles: reader(192.168.10.202)
? db4(192.168.10.50) slave/ONLINE. Roles: reader(192.168.10.201)
?
3. 故障測試
(1) 模擬 master 宕機以及恢復(fù)
① 停止 master1 的 mysql 服務(wù)
② 查看 VIP 漂移情況
monitor
[root@monitor ~]#mmm_control show
? db1(192.168.10.20) master/HARD_OFFLINE. Roles:?
? db2(192.168.10.30) master/ONLINE. Roles: writer(192.168.10.200)
? db3(192.168.10.40) slave/ONLINE. Roles: reader(192.168.10.202)
? db4(192.168.10.50) slave/ONLINE. Roles: reader(192.168.10.201)
?
VIP 成功漂移至 master2,且 master1 顯示 HARD_OFFLINE
③ 重啟 master1 的 mysql 服務(wù)
master1
systemctl start mysqld
?
master1 恢復(fù)后 VIP 仍在 master2 上,并未轉(zhuǎn)移到 master1
(2) 模擬從服務(wù)器宕機以及恢復(fù)
① 停止 slave1 的 mysql 服務(wù)
slave1
systemctl stop mysqld
?
② 查看 VIP 漂移情況
monitor文章來源:http://www.zghlxwxcb.cn/news/detail-445089.html
[root@monitor ~]#mmm_control show
? db1(192.168.10.20) master/ONLINE. Roles:?
? db2(192.168.10.30) master/ONLINE. Roles: writer(192.168.10.200)
? db3(192.168.10.40) slave/HARD_OFFLINE. Roles:?
? db4(192.168.10.50) slave/ONLINE. Roles: reader(192.168.10.201), reader(192.168.10.202)
?
slave1 所對應(yīng)的的 VIP 被 slave2 接管
③ 重啟 slave1 的 mysql 服務(wù)
slave1
systemctl start mysqld
?
④ 查看 slave1 是否恢復(fù)
monitor
[root@monitor ~]#mmm_control show
? db1(192.168.10.20) master/ONLINE. Roles:?
? db2(192.168.10.30) master/ONLINE. Roles: writer(192.168.10.200)
? db3(192.168.10.40) slave/AWAITING_RECOVERY. Roles:?
? db4(192.168.10.50) slave/ONLINE. Roles: reader(192.168.10.201), reader(192.168.10.202)[root@monitor ~]#mmm_control show
? db1(192.168.10.20) master/ONLINE. Roles:?
? db2(192.168.10.30) master/ONLINE. Roles: writer(192.168.10.200)
? db3(192.168.10.40) slave/ONLINE. Roles:?
? db4(192.168.10.50) slave/ONLINE. Roles: reader(192.168.10.201), reader(192.168.10.202)[root@monitor ~]#mmm_control show
? db1(192.168.10.20) master/ONLINE. Roles:?
? db2(192.168.10.30) master/ONLINE. Roles: writer(192.168.10.200)
? db3(192.168.10.40) slave/ONLINE. Roles: reader(192.168.10.202)
? db4(192.168.10.50) slave/ONLINE. Roles: reader(192.168.10.201)
?
在一段時間的交接后,slave1 重新獲取到 VIP,繼續(xù)工作
(3) 客戶端測試
① 在 master1 服務(wù)器上為 monitor 服務(wù)器地址授權(quán)登錄
grant all on *.* to 'test'@'192.168.10.90' identified by '123456';
flush privileges;
?
② 在 monitor 服務(wù)器上使用 VIP 登錄
yum -y install mariadb-server mariadb
systemctl start mariadb.service && systemctl enable mariadb.service
mysql -utest -p123456 -h 192.168.10.200?? ??? ?#能登錄則成功
?
③ 客戶端創(chuàng)建數(shù)據(jù),測試同步情況
monitor
文章來源地址http://www.zghlxwxcb.cn/news/detail-445089.html
到了這里,關(guān)于MySQL 數(shù)據(jù)庫之 MMM 高可用架構(gòu)構(gòu)建的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!