溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

MySQL高可用MMM架構(gòu)的搭建

發(fā)布時間:2020-06-08 09:27:58 來源:億速云 閱讀:299 作者:Leah 欄目:云計算

如何搭建MySQL高可用MMM架構(gòu)?相信大部分人都還沒學(xué)會這個技能,為了讓大家學(xué)會,給大家總結(jié)了以下內(nèi)容,話不多說,一起往下看吧。

MMM高可用架構(gòu)

MMM(Master-Master replication manager for MySQL)是一套支持雙主故障切換和雙主日常管理的腳本程序。MMM使用Perl語言開發(fā),主要用來監(jiān)控和管理MySQL Master-Master(雙主)復(fù)制,可以說是mysql主主復(fù)制管理器。雖然叫做雙主復(fù)制,但是業(yè)務(wù)上同一時刻只允許對一個主進行寫入,另一臺備選主上提供部分讀服務(wù),以加速在主主切換時刻備選主的預(yù)熱,可以說MMM這套腳本程序一方面實現(xiàn)了故障切換的功能,另一方面其內(nèi)部附加的工具腳本也可以實現(xiàn)多個slave的read負載均衡。關(guān)于mysql主主復(fù)制配置的監(jiān)控、故障轉(zhuǎn)移和管理的一套可伸縮的腳本套件(在任何時候只有一個節(jié)點可以被寫入),這個套件也能對居于標(biāo)準(zhǔn)的主從配置的任意數(shù)量的從服務(wù)器進行讀負載均衡,所以你可以用它來在一組居于復(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架構(gòu)圖:

MySQL高可用MMM架構(gòu)的搭建

MMM這種模式,master,slaver容易被控死,兩個就兩個
MHA模式,有高可擴展性,一主雙備,一個區(qū)域。再擴展一主雙備,一個區(qū)域
都要安裝node

首先我們開五臺虛擬機,做規(guī)劃

主服務(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

配置阿里云源。每臺服務(wù)器都要裝

wget -O /etc/yum.repos.d/CentOS-Base.repo httP://mirrors.aliyun.com/repo/Centos-7.repo

安裝epel-release源,每臺服務(wù)器都要裝

yum -y install epel-release

yum clean all && yum makecache

搭建本地YUM源,所有服務(wù)器都要裝,除了監(jiān)控服務(wù)器

yum install mariadb-server mariadb -y`

修改主配置文件,出來監(jiān)控服務(wù)器其他四臺都要修改。復(fù)制,粘貼就行。

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

每臺服務(wù)器都要關(guān)防火墻,增強功能,開啟數(shù)據(jù)庫

systemctl stop firewalld.service 
setenforce 0
systemctl start mariadb.service 
[root@localhost ~]# mysql #進入M1數(shù)據(jù)庫

 進入M1服務(wù)器查看日志文件的名稱和位置值
MariaDB [(none)]> show master status; #查看日志文件的名稱和位置值
+------------------+----------+--------------+--------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB         |
+------------------+----------+--------------+--------------------------+
| mysql_bin.000003 |      245 |              | mysql,information_schema |
+------------------+----------+--------------+--------------------------+

進入M2服務(wù)器查看日志文件的名稱和位置值

MariaDB [(none)]> show master status;
+------------------+----------+--------------+--------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB         |
+------------------+----------+--------------+--------------------------+
| mysql_bin.000003 |      245 |              | mysql,information_schema |
+------------------+----------+--------------+--------------------------+

兩臺主服務(wù)器都執(zhí)行,給予權(quán)限

grant replication slave on *.* to 'replication'@'192.168.136.%' identified by '123456';`

M1服務(wù)器指向M2的服務(wù)器,地址,日志文件名稱,位置參數(shù)

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;

M2服務(wù)器指向M1的服務(wù)器,地址,日志文件名稱,位置參數(shù)

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)

------------------------------------------從服務(wù)器同步數(shù)據(jù)---------------------------------------

#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 

<host default>
    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 #代理密碼
</host>

<host db1>
    ip      192.168.136.191 #主服務(wù)器1地址
    mode    master
    peer    db2
</host>

<host db2>
    ip      192.168.136.168 #主服務(wù)器2地址
    mode    master
    peer    db1
</host>

<host db3>
    ip      192.168.136.185 #從服務(wù)器1地址
    mode    slave
</host>

<host db4>
    ip      192.168.136.184 #從服務(wù)器2地址
    mode    slave
</host>

<role writer> 
    hosts   db1, db2
    ips     192.168.136.200 #主服務(wù)的虛擬地址,可以漂移
    mode    exclusive
</role>

<role reader>
    hosts   db3, db4
    ips     192.168.136.210, 192.168.136.220  #從服務(wù)器1和2的虛擬地址
    mode    balanced

用scp把文件傳給其他服務(wù)器

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/

配置監(jiān)控服務(wù)器賬號

[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';

對應(yīng)著修改配置文件(M2,S1,S2)

[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 

查看各節(jié)點的情況

[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;

回到監(jiān)控服務(wù)器測試一下能不能登錄

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)]> 

創(chuàng)建一個表格再去其他服務(wù)器查看數(shù)據(jù)有沒有被同步

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)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI