MySQL(11)数据库实现高可用架构之MMM
文章目錄
- 一、MySQL MMM介紹
- 1.1 什么是 MMM?
- 1.2 MMM的組成
- 2.1 MySQL M搭建的步驟(理論)
- 2.1.1 搭建步驟
- 2.1.2故障測(cè)試
- 2.2 MySQL M搭建具體操作步驟(實(shí)操)
- 2.2.1 搭建步驟
- 1.Master01、Master02、Slave1、Slave2 節(jié)點(diǎn)上安裝 mysql5.7
- 2.Master01、Master02、Slave1、Slave2 節(jié)點(diǎn)上關(guān)閉防火墻
- 3.搭建 MySQL 多主多從模式
- 4.配置主主復(fù)制,兩臺(tái)主服務(wù)器相互復(fù)制,在兩臺(tái)主服務(wù)器上查看,記錄日志文件名稱和同步點(diǎn)
- 5.在 master01 上配置同步
- 6.在 master02 上配置同步
- 7.配置主從復(fù)制,在兩臺(tái)從服務(wù)器上做
- 8.測(cè)試主主、主從 同步情況
- 9.在所有服務(wù)器上安裝 MySQL-MMM
- 10.在 master01 上對(duì) MySQL-MMM 進(jìn)行配置
- 11.修改所有數(shù)據(jù)庫(kù)服務(wù)器的代理配置文件 mmm_agent.conf,
- 12.在 monitor 監(jiān)控服務(wù)器上修改監(jiān)控配置文件 mmm_mon.conf
- 13.在所有數(shù)據(jù)庫(kù)上為 mmm_agent(代理進(jìn)程)授權(quán)
- 14.在所有數(shù)據(jù)庫(kù)服務(wù)器上啟動(dòng) mysql-mmm-agent
- 15.在 monitor 服務(wù)器上啟動(dòng) mysql-mmm-monitor
- 16.在 monitor 服務(wù)器上測(cè)試群集
- 2.3故障測(cè)試
- 1.停止 master01 確認(rèn) VIP 是否移動(dòng)到 master02 上,注意:master01 主服務(wù)器恢復(fù)服務(wù)后,不會(huì)搶占
- 2.停止一臺(tái)從服務(wù)器,另一臺(tái)將接管兩個(gè)虛擬IP,以保證業(yè)務(wù)不停止
- 3.在 master01 服務(wù)器上為 monitor 服務(wù)器地址授權(quán)登錄
- 4.在 monitor 服務(wù)器上使用 VIP 登錄
- 5.創(chuàng)建數(shù)據(jù),測(cè)試同步情況
一、MySQL MMM介紹
MMM(Master-Master replication manager for MvSQL,MySQL主主復(fù)制管理器)
是一套支持雙主故障切換和雙主日常管理的腳本程序。MMM 使用 Perl 語(yǔ)言開(kāi)發(fā),主要用來(lái)監(jiān)控和管理 MySQL Master-Master (雙主)復(fù)制,雖然叫做雙主復(fù)制,但是業(yè)務(wù)上同一時(shí)刻只允許對(duì)一個(gè)主進(jìn)行寫(xiě)入,另一臺(tái)備選主上提供部分讀服務(wù),以加速在主主切換時(shí)備選主的預(yù)熱,可以說(shuō)MMM這套腳本程序一方面實(shí)現(xiàn)了故障切換的功能,另一方面其內(nèi)部附加的工具腳本也可以實(shí)現(xiàn)多個(gè) Slave 的 read 負(fù)載均衡。
MMM提供了自動(dòng)和手動(dòng)兩種方式移除一組服務(wù)器中復(fù)制延遲較高的服務(wù)器的虛擬ip,同時(shí)它還可以備份數(shù)據(jù),實(shí)現(xiàn)兩節(jié)點(diǎn)之間的數(shù)據(jù)同步等。由于MMM無(wú)法完全保證數(shù)據(jù)的一致性,所以MMM適用于對(duì)數(shù)據(jù)的一致性要求不是很高,但是又想最大程度地保證業(yè)務(wù)可用性的場(chǎng)景。
1.1 什么是 MMM?
MMM是一套靈活的腳本程序,基于perl實(shí)現(xiàn),用來(lái)對(duì) mysql replication 進(jìn)行監(jiān)控和故障遷移,并能管理 MySQL Master-Master 復(fù)制的配置。
1.2 MMM的組成
關(guān)于 MMM 高可用架構(gòu)的說(shuō)明如下:
●mmm_mon:監(jiān)控進(jìn)程,負(fù)責(zé)所有的監(jiān)控工作,決定和處理所有節(jié)點(diǎn)角色活動(dòng)。此腳本需要在監(jiān)控主機(jī)上運(yùn)行。
●mmm_agent:運(yùn)行在每個(gè)MySQL服務(wù)器上的代理進(jìn)程,完成監(jiān)控的探針工作和執(zhí)行簡(jiǎn)單的遠(yuǎn)端服務(wù)設(shè)置。此腳本需要在被監(jiān)管機(jī)上運(yùn)行。
●mmm_control:一個(gè)簡(jiǎn)單的腳本,提供管理 mmm_mon 進(jìn)程的命令。
●mysql-mmm 的監(jiān)管端會(huì)提供多個(gè)虛擬 IP(VIP),包括一個(gè)可寫(xiě) VIP,多個(gè)可讀 VIP,通過(guò)監(jiān)管的管理,這些 IP 會(huì)綁定在可用 MySQL 之上,當(dāng)某一臺(tái) MySQL 宕機(jī)時(shí),監(jiān)管會(huì)將 VIP 遷移至其他 MySQL。
在整個(gè)監(jiān)管過(guò)程中,需要在 MySQL 中添加相關(guān)授權(quán)用戶,以便讓 MySQL 可以支持監(jiān)控主機(jī)的維護(hù)。 授權(quán)的用戶包括一個(gè) mmm_monitor 用戶和一個(gè) mmm_agent 用戶。
二、MySQL 搭建
環(huán)境準(zhǔn)備
----------------------搭建 MySQL MMM--------------------------------
master01(db1) 192.168.80.20 mysql5.7、mysql-mmm
master02(db2) 192.168.80.30 mysql5.7、mysql-mmm
slave01(db3) 192.168.80.13 mysql5.7、mysql-mmm
slave02(db4) 192.168.80.14 mysql5.7、mysql-mmm
monitor 192.168.80.15 mysql-mmm
2.1 MySQL M搭建的步驟(理論)
2.1.1 搭建步驟
systemctl stop firewalld
setenforce 0
---------------------- 搭建 MySQL 多主多從模式 ----------------------
//修改 master01 配置文件
vim /etc/my.cnf
…
[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 #每臺(tái) Mysql 主機(jī)的 server-id 不能相同
log-error=/usr/local/mysql/data/mysql_error.log #錯(cuò)誤日志
general_log=ON #通用查詢?nèi)罩?br /> general_log_file=/usr/local/mysql/data/mysql_general.log
slow_query_log=ON #慢查詢?nèi)罩?br /> slow_query_log_file=mysql_slow_query.log
long_query_time=5
binlog-ignore-db=mysql,information_schema #不需要同步的庫(kù)名
log_bin=mysql_bin #開(kāi)啟二進(jìn)制日志用于主從數(shù)據(jù)復(fù)制
log_slave_updates=true #允許slave從master復(fù)制數(shù)據(jù)時(shí)可以寫(xiě)入到自己的二進(jìn)制日志
sync_binlog=1 #“雙1設(shè)置”,MySQL 在每寫(xiě)一次二進(jìn)制日志時(shí)都會(huì)同步到磁盤(pán)中去
innodb_flush_log_at_trx_commit=1 #“雙1設(shè)置”,每次事務(wù)提交時(shí)MySQL都會(huì)把緩存的數(shù)據(jù)寫(xiě)入日志文件,并且刷到磁盤(pán)中去
auto_increment_increment=2 #自增字段一次遞增多少
auto_increment_offset=1 #自增字段的起始值
//把配置文件復(fù)制到其它 3 臺(tái)數(shù)據(jù)庫(kù)服務(wù)器上并啟動(dòng)服務(wù)器,注意:配置文件中的 server_id 要修改
scp /etc/my.cnf root@192.168.80.30:/etc/
scp /etc/my.cnf root@192.168.80.13:/etc/
scp /etc/my.cnf root@192.168.80.14:/etc/
systemctl restart mysqld
//配置主主復(fù)制,兩臺(tái)主服務(wù)器相互復(fù)制
#在兩臺(tái)主服務(wù)器上都執(zhí)行授予從的權(quán)限,從服務(wù)器上不需要執(zhí)行
grant replication slave on . to ‘replication’@‘192.168.80.%’ identified by ‘123456’;
#在兩臺(tái)主服務(wù)器上查看,記錄日志文件名稱和同步點(diǎn)
show master status;
±------------------±---------±-------------±-----------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
±------------------±---------±-------------±-----------------+
| master-bin.000002 | 154 | | |
±------------------±---------±-------------±-----------------+
#在 master01 上配置同步
change master to master_host=‘192.168.80.30’,master_user=‘replication’,master_password=‘123456’,master_log_file=‘mysql_bin.000002’,master_log_pos=154;
start slave;
show slave status\G
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
#在 master02 上配置同步
change master to master_host=‘192.168.80.20’,master_user=‘replication’,master_password=‘123456’,master_log_file=‘mysql_bin.000002’,master_log_pos=154;
start slave;
show slave status\G
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
//配置主從復(fù)制,在兩臺(tái)從服務(wù)器上做
change master to master_host=‘192.168.80.20’,master_user=‘replication’,master_password=‘123456’,master_log_file=‘mysql_bin.000002’,master_log_pos=154;
start slave;
show slave status\G
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
//測(cè)試主主、主從 同步情況
create database db_test;
---------------------- 安裝配置 MySQL-MMM ----------------------
//在所有服務(wù)器上安裝 MySQL-MMM
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum -y install epel-release
yum -y install mysql-mmm*
//在 master01 上對(duì) MySQL-MMM 進(jìn)行配置
cd /etc/mysql-mmm/
vim mmm_common.conf
……
cluster_interface ens33
……
replication_user replication
replication_password 123456
agent_user mmm_agent
agent_password 123456
//把配置文件復(fù)制到其它 4 臺(tái)主機(jī),所有主機(jī)該配置文件內(nèi)容都是一樣的
scp mmm_common.conf root@192.168.80.30:/etc/mysql-mmm/
scp mmm_common.conf root@192.168.80.13:/etc/mysql-mmm/
scp mmm_common.conf root@192.168.80.14:/etc/mysql-mmm/
scp mmm_common.conf root@192.168.80.15:/etc/mysql-mmm/
//修改所有數(shù)據(jù)庫(kù)服務(wù)器的代理配置文件 mmm_agent.conf
vim /etc/mysql-mmm/mmm_agent.conf
include mmm_common.conf
this db1 #根據(jù)不同的主機(jī)分別修改為 db1,db2,db3,db4
//在 monitor 監(jiān)控服務(wù)器上修改監(jiān)控配置文件 mmm_mon.conf
vim /etc/mysql-mmm/mmm_mon.conf
include mmm_common.conf
…
ping_ips 192.168.80.20,192.168.80.30,192.168.80.13,192.168.80.14 #指定所有數(shù)據(jù)庫(kù)服務(wù)器的 IP
auto_set_online 10 #指定自動(dòng)上線時(shí)間
//在所有數(shù)據(jù)庫(kù)上為 mmm_agent(代理進(jìn)程)授權(quán)
grant super, replication client, process on . to ‘mmm_agent’@‘192.168.80.%’ identified by ‘123456’;
//在所有數(shù)據(jù)庫(kù)上為 mmm_moniter(監(jiān)控進(jìn)程)授權(quán)
grant replication client on . to ‘mmm_monitor’@‘192.168.80.%’ identified by ‘123456’;
flush privileges;
//在所有數(shù)據(jù)庫(kù)服務(wù)器上啟動(dòng) mysql-mmm-agent
systemctl start mysql-mmm-agent.service
systemctl enable mysql-mmm-agent.service
//在 monitor 服務(wù)器上啟動(dòng) mysql-mmm-monitor
systemctl start mysql-mmm-monitor.service
//在 monitor 服務(wù)器上測(cè)試群集
#查看各節(jié)點(diǎn)的情況
mmm_control show
db1(192.168.80.20) master/ONLINE. Roles: writer(192.168.80.188)
db2(192.168.80.30) master/ONLINE. Roles:
db3(192.168.80.13) slave/ONLINE. Roles: reader(192.168.80.198)
db4(192.168.80.14) slave/ONLINE. Roles: reader(192.168.80.199)
#檢測(cè)監(jiān)控功能是否都完善,需要各種OK
mmm_control checks all
#指定綁定 VIP 的主機(jī)
mmm_control move_role writer db2
2.1.2故障測(cè)試
mmm_control move_role writer db1
#停止 master01 確認(rèn) VIP 是否移動(dòng)到 master02 上。注意:master01 主服務(wù)器恢復(fù)服務(wù)后,不會(huì)搶占
mmm_control show
db1(192.168.80.20) master/HARD_OFFLINE. Roles:
db2(192.168.80.30) master/ONLINE. Roles: writer(192.168.80.188)
#停止一臺(tái)從服務(wù)器,另一臺(tái)將接管兩個(gè)虛擬IP,以保證業(yè)務(wù)不停止
mmm_control show
//客戶端測(cè)試
#在 master01 服務(wù)器上為 monitor 服務(wù)器地址授權(quán)登錄
grant all on . to ‘testdba’@‘192.168.80.15’ identified by ‘123456’;
flush privileges;
#在 monitor 服務(wù)器上使用 VIP 登錄
yum install -y mariadb-server mariadb
systemctl start mariadb.service
mysql -utestdba -p -h 192.168.235.188
#創(chuàng)建數(shù)據(jù),測(cè)試同步情況
create database testdba;
2.2 MySQL M搭建具體操作步驟(實(shí)操)
2.2.1 搭建步驟
1.Master01、Master02、Slave1、Slave2 節(jié)點(diǎn)上安裝 mysql5.7
2.Master01、Master02、Slave1、Slave2 節(jié)點(diǎn)上關(guān)閉防火墻
3.搭建 MySQL 多主多從模式
把配置文件復(fù)制到其它 3 臺(tái)數(shù)據(jù)庫(kù)服務(wù)器上并啟動(dòng)服務(wù)器,注意:配置文件中的 server_id 要修改,重啟服務(wù)
4.配置主主復(fù)制,兩臺(tái)主服務(wù)器相互復(fù)制,在兩臺(tái)主服務(wù)器上查看,記錄日志文件名稱和同步點(diǎn)
5.在 master01 上配置同步
6.在 master02 上配置同步
7.配置主從復(fù)制,在兩臺(tái)從服務(wù)器上做
8.測(cè)試主主、主從 同步情況
9.在所有服務(wù)器上安裝 MySQL-MMM
10.在 master01 上對(duì) MySQL-MMM 進(jìn)行配置
把配置文件復(fù)制到其它 4 臺(tái)主機(jī),所有主機(jī)該配置文件內(nèi)容都是一樣的
11.修改所有數(shù)據(jù)庫(kù)服務(wù)器的代理配置文件 mmm_agent.conf,
12.在 monitor 監(jiān)控服務(wù)器上修改監(jiān)控配置文件 mmm_mon.conf
13.在所有數(shù)據(jù)庫(kù)上為 mmm_agent(代理進(jìn)程)授權(quán)
在所有數(shù)據(jù)庫(kù)上為 mmm_moniter(監(jiān)控進(jìn)程)授權(quán)
14.在所有數(shù)據(jù)庫(kù)服務(wù)器上啟動(dòng) mysql-mmm-agent
15.在 monitor 服務(wù)器上啟動(dòng) mysql-mmm-monitor
16.在 monitor 服務(wù)器上測(cè)試群集
查看各節(jié)點(diǎn)的情況
檢測(cè)監(jiān)控功能是否都完善,需要各種Ok
指定綁定 VIP 的主機(jī)
2.3故障測(cè)試
1.停止 master01 確認(rèn) VIP 是否移動(dòng)到 master02 上,注意:master01 主服務(wù)器恢復(fù)服務(wù)后,不會(huì)搶占
2.停止一臺(tái)從服務(wù)器,另一臺(tái)將接管兩個(gè)虛擬IP,以保證業(yè)務(wù)不停止
3.在 master01 服務(wù)器上為 monitor 服務(wù)器地址授權(quán)登錄
4.在 monitor 服務(wù)器上使用 VIP 登錄
5.創(chuàng)建數(shù)據(jù),測(cè)試同步情況
總結(jié)
以上是生活随笔為你收集整理的MySQL(11)数据库实现高可用架构之MMM的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: MySQL(10)数据库实现高可用架构之
- 下一篇: Squid 代理服务器应用大全