如何搭建MySQL高可用MMM架構(gòu)?相信大部分人都還沒學(xué)會這個技能,為了讓大家學(xué)會,給大家總結(jié)了以下內(nèi)容,話不多說,一起往下看吧。
專注于為中小企業(yè)提供網(wǎng)站設(shè)計、做網(wǎng)站服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)沙河口免費做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了成百上千企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
MMM(Master-Master replication manager for MySQL)是一套支持雙主故障切換和雙主日常管理的腳本程序。MMM使用Perl語言開發(fā),主要用來監(jiān)控和管理MySQL Master-Master(雙主)復(fù)制,可以說是mysql主主復(fù)制管理器。雖然叫做雙主復(fù)制,但是業(yè)務(wù)上同一時刻只允許對一個主進(jìn)行寫入,另一臺備選主上提供部分讀服務(wù),以加速在主主切換時刻備選主的預(yù)熱,可以說MMM這套腳本程序一方面實現(xiàn)了故障切換的功能,另一方面其內(nèi)部附加的工具腳本也可以實現(xiàn)多個slave的read負(fù)載均衡。關(guān)于mysql主主復(fù)制配置的監(jiān)控、故障轉(zhuǎn)移和管理的一套可伸縮的腳本套件(在任何時候只有一個節(jié)點可以被寫入),這個套件也能對居于標(biāo)準(zhǔn)的主從配置的任意數(shù)量的從服務(wù)器進(jìn)行讀負(fù)載均衡,所以你可以用它來在一組居于復(fù)制的服務(wù)器啟動虛擬ip,除此之外,它還有實現(xiàn)數(shù)據(jù)備份、節(jié)點之間重新同步功能的腳本。
MMM提供了自動和手動兩種方式移除一組服務(wù)器中復(fù)制延遲較高的服務(wù)器的虛擬ip,同時它還可以備份數(shù)據(jù),實現(xiàn)兩節(jié)點之間的數(shù)據(jù)同步等。由于MMM無法完全的保證數(shù)據(jù)一致性,所以MMM適用于對數(shù)據(jù)的一致性要求不是很高,但是又想最大程度的保證業(yè)務(wù)可用性的場景。MySQL本身沒有提供replication failover的解決方案,通過MMM方案能實現(xiàn)服務(wù)器的故障轉(zhuǎn)移,從而實現(xiàn)mysql的高可用。對于那些對數(shù)據(jù)的一致性要求很高的業(yè)務(wù),非常不建議采用MMM這種高可用架構(gòu)。
MMM這種模式,master,slaver容易被控死,兩個就兩個
MHA模式,有高可擴(kuò)展性,一主雙備,一個區(qū)域。再擴(kuò)展一主雙備,一個區(qū)域
都要安裝node
主服務(wù)器1 192.168.136.191 db1
主服務(wù)器2 192.168.136.168 db2
從服務(wù)器1 192.168.136.185 db3
從服務(wù)器2 192.168.136.184 db4
監(jiān)控服務(wù)器 192.168.136.135
wget -O /etc/yum.repos.d/CentOS-Base.repo httP://mirrors.aliyun.com/repo/Centos-7.repo
yum -y install epel-release
yum clean all && yum makecache
yum install mariadb-server mariadb -y`
vim /etc/my.cnf
9dd
[mysqld]
log_error=/var/lib/mysql/mysql.err
log=/var/lib/mysql/mysql_log.log
log_slow_queries=/var/lib/mysql_slow_queries.log
binlog-ignore-db=mysql,information_schema
character_set_server=utf8
log_bin=mysql_bin
server_id=1 #注意每臺id都要是不一樣的
log_slave_updates=true
sync_binlog=1
auto_increment_increment=2
auto_increment_offset=1
systemctl stop firewalld.service
setenforce 0
systemctl start mariadb.service
[root@localhost ~]# mysql #進(jìn)入M1數(shù)據(jù)庫
進(jìn)入M1服務(wù)器查看日志文件的名稱和位置值
MariaDB [(none)]> show master status; #查看日志文件的名稱和位置值
+------------------+----------+--------------+--------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+--------------------------+
| mysql_bin.000003 | 245 | | mysql,information_schema |
+------------------+----------+--------------+--------------------------+
MariaDB [(none)]> show master status;
+------------------+----------+--------------+--------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+--------------------------+
| mysql_bin.000003 | 245 | | mysql,information_schema |
+------------------+----------+--------------+--------------------------+
grant replication slave on *.* to 'replication'@'192.168.136.%' identified by '123456';`
change master to master_host='192.168.136.168',master_user='relication',master_password='123456',master_log_file='mysql_bin.000003',master_log_pos=245;
change master to master_host='192.168.136.167',master_user='relication',master_password='123456',master_log_file='mysql_bin.000003',master_log_pos=245;
#兩臺服務(wù)器都執(zhí)行開啟同步數(shù)據(jù)
MariaDB [(none)]> slave start;
#兩臺主服務(wù)器都執(zhí)行,查看同步數(shù)據(jù)的狀態(tài)
MariaDB [(none)]> show slave status\G;
#看到下面的IO線程和狀態(tài)都是YES就是正確了
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
#我們測試一下兩個主服務(wù)器是否能同步數(shù)據(jù)
M1創(chuàng)建數(shù)據(jù)一個數(shù)據(jù)庫
MariaDB [(none)]> create database myschool;
Query OK, 1 row affected (0.00 sec)
M2
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| myschool |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
#s1和s2從服務(wù)器
[root@localhost ~]# mysql
#都指向M1主服務(wù)器的地址,日志文件和參數(shù)
MariaDB [(none)]> change master to master_host='192.168.136.191',master_user='replication',master_password='123456',master_log_file='mysql_bin.000003',master_log_pos=245;
#在M1創(chuàng)建一個數(shù)據(jù)庫
MariaDB [(none)]> create database school;
Query OK, 1 row affected (0.00 sec)
#其他三臺服務(wù)器都有這個數(shù)據(jù)庫
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| school |
| test |
+--------------------+
#所有服務(wù)器都要裝mmm源碼包
yum -y install mysql-mmm*
#到第一臺主服務(wù)器開始配置
[root@localhost ~]# cd /etc/mysql-mmm/
[root@localhost mysql-mmm]# ls
mmm_agent.conf mmm_common.conf mmm_mon.conf mmm_mon_log.conf mmm_tools.conf
[root@localhost mysql-mmm]# vim mmm_common.conf
cluster_interface ens33 #網(wǎng)卡是ens33
pid_path /run/mysql-mmm-agent.pid
bin_path /usr/libexec/mysql-mmm/
replication_user replication #用戶
replication_password 123456 #我們設(shè)置的密碼
agent_user mmm_agent
agent_password 123456 #代理密碼
ip 192.168.136.191 #主服務(wù)器1地址
mode master
peer db2
ip 192.168.136.168 #主服務(wù)器2地址
mode master
peer db1
ip 192.168.136.185 #從服務(wù)器1地址
mode slave
ip 192.168.136.184 #從服務(wù)器2地址
mode slave
hosts db1, db2
ips 192.168.136.200 #主服務(wù)的虛擬地址,可以漂移
mode exclusive
hosts db3, db4
ips 192.168.136.210, 192.168.136.220 #從服務(wù)器1和2的虛擬地址
mode balanced
scp mmm_common.conf root@192.168.136.168:/etc/mysql-mmm/
scp mmm_common.conf root@192.168.136.185:/etc/mysql-mmm/
scp mmm_common.conf root@192.168.136.184:/etc/mysql-mmm/
scp mmm_common.conf root@192.168.136.135:/etc/mysql-mmm/
[root@localhost mysql-mmm]# vim mmm_mon.conf
ping_ips 192.168.136.191,192.168.136.168,192.168.136.185,192.168.136.184 輸入你所有的地址
auto_set_online 10 #自帶上線時間為10s
monitor_password 123456 #修改密碼為123456
在所有數(shù)據(jù)庫給mmm_agent授權(quán)
grant super, replication client, process on *.* to 'mmm_agent'@'192.168.136.%' identified by '123456';
grant replication client on *.* to 'mmm_monitor'@'192.168.136.%' identified by '123456';
[root@localhost mysql-mmm]# vim mmm_agent.conf
this db2
this db3
this db4
systemctl start mysql-mmm-agent.service
systemctl enable mysql-mmm-agent.service
#回到監(jiān)控服務(wù)器
systemctl start mysql-mmm-monitor.service
[root@localhost mysql-mmm]# mmm_control show
db1(192.168.136.191) master/ONLINE. Roles: writer(192.168.136.200)
db2(192.168.136.168) master/ONLINE. Roles:
db3(192.168.136.185) slave/ONLINE. Roles: reader(192.168.136.220)
db4(192.168.136.184) slave/ONLINE. Roles: reader(192.168.136.210)
#更改綁定的虛擬地址
[root@localhost mysql-mmm]# mmm_control move_role writer db2
#測試監(jiān)控服務(wù)器功能是否完善
[root@localhost mysql-mmm]# mmm_control checks all
db4 ping [last change: 2019/11/25 16:38:25] OK
db4 mysql [last change: 2019/11/25 16:38:25] OK
db4 rep_threads [last change: 2019/11/25 16:38:25] OK
db4 rep_backlog [last change: 2019/11/25 16:38:25] OK: Backlog is null
db2 ping [last change: 2019/11/25 16:38:25] OK
db2 mysql [last change: 2019/11/25 16:38:25] OK
db2 rep_threads [last change: 2019/11/25 16:38:25] OK
db2 rep_backlog [last change: 2019/11/25 16:38:25] OK: Backlog is null
db3 ping [last change: 2019/11/25 16:38:25] OK
db3 mysql [last change: 2019/11/25 16:38:25] OK
db3 rep_threads [last change: 2019/11/25 16:38:25] OK
db3 rep_backlog [last change: 2019/11/25 16:38:25] OK: Backlog is null
db1 ping [last change: 2019/11/25 16:38:25] OK
db1 mysql [last change: 2019/11/25 16:38:25] OK
db1 rep_threads [last change: 2019/11/25 16:38:25] OK
db1 rep_backlog [last change: 2019/11/25 16:38:25] OK: Backlog is null
[root@localhost mysql-mmm]# 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@localhost mysql-mmm]# mmm_control show
db1(192.168.136.191) master/ONLINE. Roles: writer(192.168.136.200)
db2(192.168.136.168) master/ONLINE. Roles:
db3(192.168.136.185) slave/ONLINE. Roles: reader(192.168.136.220)
db4(192.168.136.184) slave/ONLINE. Roles: reader(192.168.136.210)
#第一臺主服務(wù)器關(guān)閉數(shù)據(jù)庫模擬故障
[root@localhost mysql-mmm]# systemctl stop mariadb.service
#回到監(jiān)控服務(wù)器測試,虛擬網(wǎng)址就變更了
[root@localhost mysql-mmm]# mmm_control show
db1(192.168.136.191) master/HARD_OFFLINE. Roles:
db2(192.168.136.168) master/ONLINE. Roles: writer(192.168.136.200)
db3(192.168.136.185) slave/ONLINE. Roles: reader(192.168.136.220)
db4(192.168.136.184) slave/ONLINE. Roles: reader(192.168.136.210)
#再把第一臺主服務(wù)器開啟數(shù)據(jù)庫
[root@localhost mysql-mmm]# systemctl start mariadb.service
#在回到監(jiān)控服務(wù)器查看主服務(wù)器狀態(tài)
[root@localhost mysql-mmm]# mmm_control show
db1(192.168.136.191) master/ONLINE. Roles:
db2(192.168.136.168) master/ONLINE. Roles: writer(192.168.136.200)
db3(192.168.136.185) slave/ONLINE. Roles: reader(192.168.136.220)
db4(192.168.136.184) slave/ONLINE. Roles: reader(192.168.136.210)
#監(jiān)控服務(wù)器
[root@localhost mysql-mmm]# yum install mariadb-server mariadb -y
#再M1服務(wù)器為監(jiān)控器地址授權(quán)登錄
MariaDB [(none)]> grant all on *.* to 'testba'@'192.168.136.135' identified by '123456';
MariaDB [(none)]> flush privileges;
mysql -utestdba -p -h 192.168.136.200
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2562
Server version: 5.5.64-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
MariaDB [(none)]> create database chen;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| chen |
| mysql |
| performance_schema |
| school |
| test |
+--------------------+
6 rows in set (0.01 sec)
到此為止,MySQL高可用MMM架構(gòu)就搭建成功了,如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。