您好,登錄后才能下訂單哦!
如何搭建MySQL高可用MMM架構?相信大部分人都還沒學會這個技能,為了讓大家學會,給大家總結了以下內容,話不多說,一起往下看吧。
MMM(Master-Master replication manager for MySQL)是一套支持雙主故障切換和雙主日常管理的腳本程序。MMM使用Perl語言開發,主要用來監控和管理MySQL Master-Master(雙主)復制,可以說是mysql主主復制管理器。雖然叫做雙主復制,但是業務上同一時刻只允許對一個主進行寫入,另一臺備選主上提供部分讀服務,以加速在主主切換時刻備選主的預熱,可以說MMM這套腳本程序一方面實現了故障切換的功能,另一方面其內部附加的工具腳本也可以實現多個slave的read負載均衡。關于mysql主主復制配置的監控、故障轉移和管理的一套可伸縮的腳本套件(在任何時候只有一個節點可以被寫入),這個套件也能對居于標準的主從配置的任意數量的從服務器進行讀負載均衡,所以你可以用它來在一組居于復制的服務器啟動虛擬ip,除此之外,它還有實現數據備份、節點之間重新同步功能的腳本。
MMM提供了自動和手動兩種方式移除一組服務器中復制延遲較高的服務器的虛擬ip,同時它還可以備份數據,實現兩節點之間的數據同步等。由于MMM無法完全的保證數據一致性,所以MMM適用于對數據的一致性要求不是很高,但是又想最大程度的保證業務可用性的場景。MySQL本身沒有提供replication failover的解決方案,通過MMM方案能實現服務器的故障轉移,從而實現mysql的高可用。對于那些對數據的一致性要求很高的業務,非常不建議采用MMM這種高可用架構。
MMM這種模式,master,slaver容易被控死,兩個就兩個
MHA模式,有高可擴展性,一主雙備,一個區域。再擴展一主雙備,一個區域
都要安裝node
主服務器1 192.168.136.191 db1
主服務器2 192.168.136.168 db2
從服務器1 192.168.136.185 db3
從服務器2 192.168.136.184 db4
監控服務器 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 #進入M1數據庫
進入M1服務器查看日志文件的名稱和位置值
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;
#兩臺服務器都執行開啟同步數據
MariaDB [(none)]> slave start;
#兩臺主服務器都執行,查看同步數據的狀態
MariaDB [(none)]> show slave status\G;
#看到下面的IO線程和狀態都是YES就是正確了
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
#我們測試一下兩個主服務器是否能同步數據
M1創建數據一個數據庫
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從服務器
[root@localhost ~]# mysql
#都指向M1主服務器的地址,日志文件和參數
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創建一個數據庫
MariaDB [(none)]> create database school;
Query OK, 1 row affected (0.00 sec)
#其他三臺服務器都有這個數據庫
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| school |
| test |
+--------------------+
#所有服務器都要裝mmm源碼包
yum -y install mysql-mmm*
#到第一臺主服務器開始配置
[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 #網卡是ens33
pid_path /run/mysql-mmm-agent.pid
bin_path /usr/libexec/mysql-mmm/
replication_user replication #用戶
replication_password 123456 #我們設置的密碼
agent_user mmm_agent
agent_password 123456 #代理密碼
</host>
<host db1>
ip 192.168.136.191 #主服務器1地址
mode master
peer db2
</host>
<host db2>
ip 192.168.136.168 #主服務器2地址
mode master
peer db1
</host>
<host db3>
ip 192.168.136.185 #從服務器1地址
mode slave
</host>
<host db4>
ip 192.168.136.184 #從服務器2地址
mode slave
</host>
<role writer>
hosts db1, db2
ips 192.168.136.200 #主服務的虛擬地址,可以漂移
mode exclusive
</role>
<role reader>
hosts db3, db4
ips 192.168.136.210, 192.168.136.220 #從服務器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
在所有數據庫給mmm_agent授權
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
#回到監控服務器
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
#測試監控服務器功能是否完善
[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)
#第一臺主服務器關閉數據庫模擬故障
[root@localhost mysql-mmm]# systemctl stop mariadb.service
#回到監控服務器測試,虛擬網址就變更了
[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)
#再把第一臺主服務器開啟數據庫
[root@localhost mysql-mmm]# systemctl start mariadb.service
#在回到監控服務器查看主服務器狀態
[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)
#監控服務器
[root@localhost mysql-mmm]# yum install mariadb-server mariadb -y
#再M1服務器為監控器地址授權登錄
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架構就搭建成功了,如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。