您好,登錄后才能下訂單哦!
下文內容主要給大家帶來有關MySQL-MMM高可用群集部署詳解,這里所講到的知識,與書籍略有不同,都是億速云專業技術人員在與用戶接觸過程中,總結出來的,具有一定的經驗分享價值,希望給廣大讀者帶來幫助。
MMM(MySQL主主復制管理器)是一套支持雙主故障切換和雙主日常管理的腳本程序。主要用來監控和管理MySQL Master-Master(雙主)復制,雖然叫雙主復制,但是業務上同一時刻只允許對一個主進行寫入,另一臺備選主上提供部分讀服務,以加速在主切換時備選主的預熱,可以說MMM這套腳本程序一方面實現了故障切換的功能,另一方面其內部附加的工具腳本也可以實現多個Slave的read負載均衡。
MMM時一套靈活的腳本程序,基于perl實現,用來對mysql replication進行監控和故障遷移,并能管理MySQL Master-Master復制的配置,如圖所示:
關于MMM高可用架構的說明如下:
實驗環境:(mariadb數據庫是mysql的一個分支,它們的命令、操作都一樣)
本實驗環境使用五臺服務器模擬搭建,實驗環境如表所示。
(1)安裝mariadb
1)所有服務器都配置ALI云源,然后安裝epel-release源。
[root@localhost ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@localhost ~]# yum -y install epel-release
[root@localhost ~]# yum clean all && yum makecache
2)搭建本地YUM源。
[root@localhost ~]# yum -y install mariadb-server mariadb
[root@localhost ~]# systemctl stop firewalld.service //關閉防火墻//
[root@localhost ~]# setenforce 0
3)修改mariadb-m1主配置文件。
[root@localhost ~]# vim /etc/my.cnf
[mysqld] //添加//
log_error=/var/lib/mysql/mysql.err
log=/var/lib/mysql/mysql_log.log
log_slow_queries=/var/lib/mysql_slow_queris.log
binlog-ignore-db=mysql,information_schema //不需要同步的數據庫名稱//
character_set_server=utf8
log_bin=mysql_bin //開啟binlog日志用于主從數據復制//
server_id=1 //每臺server-id的值不要相同//
log_slave_updates=true //此數據庫宕機,備用數據庫接管//
sync_binlog=1
auto_increment_increment=2 //字段一次遞增多少//
auto_increment_offset=1 //自增字段的起始值//
[root@localhost ~]# systemctl start mariadb.service //開啟mariadb//
[root@localhost ~]# netstat -anpt | grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 3434/mysqld
4)沒有問題后,把配置文件復制到其它3臺數據庫服務器上。
[root@localhost ~]# scp /etc/my.cnf root@192.168.126.139:/etc/ //m2//
[root@localhost ~]# scp /etc/my.cnf root@192.168.126.136:/etc/ //m3//
[root@localhost ~]# scp /etc/my.cnf root@192.168.126.137:/etc/ //m4//
注意:每臺mariadb主機的server-id不能相同,其他配置文件參數相同即可。
(2)配置mariadb-m1、mariadb-m2主主模式
1)先查看log bin和pos值的位置。
m1:
[root@localhost ~]# mysql
MariaDB [(none)]> show master status;
+------------------+----------+--------------+--------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+--------------------------+
| mysql_bin.000001 | 245 | | mysql,information_schema |
+------------------+----------+--------------+--------------------------+
1 row in set (0.00 sec)
m2:
[root@localhost ~]# mysql
MariaDB [(none)]> show master status;
+------------------+----------+--------------+--------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+--------------------------+
| mysql_bin.000003 | 245 | | mysql,information_schema |
+------------------+----------+--------------+--------------------------+
1 row in set (0.00 sec)
2)mariadb-m1、mariadb-m2互相提升訪問權限。
m1:
MariaDB [(none)]> grant replication slave on *.* to 'replication'@'192.168.126.%' identified by '123456';
Query OK, 0 rows affected (0.03 sec)
MariaDB [(none)]> change master to master_host='192.168.126.139',master_user='replication',master_password='123456',master_log_file='mysql_bin.000003',master_log_pos=411;
Query OK, 0 rows affected (0.18 sec)
m2:
MariaDB [(none)]> grant replication slave on *.* to 'replication'@'192.168.126.%' identified by '123456';
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> change master to master_host='192.168.126.138',master_user='replication',master_password='123456',master_log_file='mysql_bin.000001',master_log_pos=411;
Query OK, 0 rows affected (0.03 sec)
3)分別查看mariadb-m1、mariadb-m2服務器的主從狀態。
MariaDB [(none)]> start slave;
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.126.139
Master_User: replication
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql_bin.000003
Read_Master_Log_Pos: 411
Relay_Log_File: mariadb-relay-bin.000002
Relay_Log_Pos: 529
Relay_Master_Log_File: mysql_bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
主主同步配置完畢,查看同步狀態Slave_IO和Slave_SQL為YES,說明主主同步成功。
4)測試主主同步,在mariadb-m2新建一個庫dba。
m2:
MariaDB [(none)]> create database dba; //創建數據庫//
Query OK, 1 row affected (0.09 sec)
m1:
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| dba | //同步成功//
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.02 sec)
(3)配置mariadb-m3和mariadb-m4作為mariadb-m1的從庫
1)先查看mariadb-m1 master的狀態值。
MariaDB [(none)]> show master status;
+------------------+----------+--------------+--------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+--------------------------+
| mysql_bin.000001 | 581 | | mysql,information_schema |
+------------------+----------+--------------+--------------------------+
1 row in set (0.00 sec)
2)mariadb-m3、mariadb-m4分別執行。
MariaDB [(none)]> change master to master_host='192.168.126.138',master_user='replication',master_password='123456',master_log_file='mysql_bin.000001',master_log_pos=581;
Query OK, 0 rows affected (0.01 sec)
3)分別查看mariadb-m3和mariadb-m4服務器的主從狀態,結果如下:
MariaDB [(none)]> start slave;
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.126.138
Master_User: replication
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql_bin.000001
Read_Master_Log_Pos: 581
Relay_Log_File: mariadb-relay-bin.000002
Relay_Log_Pos: 529
Relay_Master_Log_File: mysql_bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
4)在mariadb-m1建立數據庫,測試主從、主主、同步情況。
MariaDB [(none)]> create database dba01; //在m1創建數據庫//
Query OK, 1 row affected (0.01 sec)
MariaDB [(none)]> show databases; //mariadb-m3查看結果//
+--------------------+
| Database |
+--------------------+
| information_schema |
| dba01 |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.03 sec)
MariaDB [(none)]> show databases; //mariadb-m4查看結果//
+--------------------+
| Database |
+--------------------+
| information_schema |
| dba01 |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.04 sec)
(1)在所有服務器上安裝MMM,注意,epel源要配置好。
[root@localhost ~]# yum -y install mysql-mmm*
(2)修改/etc/mysql-mmm/mmm_common.conf 配置文件,系統中所有主機的該配置文件內容一樣,包括監控主機mysql-monitor。
[root@localhost ~]# cd /etc/mysql-mmm/
[root@localhost 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
replication_password 123456 //登錄密碼
agent_user mmm_agent
agent_password 123456
</host>
<host db1> //主服務器m1//
ip 192.168.126.138
mode master
peer db2
</host>
<host db2> //主服務器m2//
ip 192.168.126.139
mode master
peer db1
</host>
<host db3> //主服務器m3//
ip 192.168.126.136
mode slave
</host>
<host db4> //主服務器m4//
ip 192.168.126.137
mode slave
</host>
<role writer>
hosts db1, db2 //m1 m2//
ips 192.168.126.188 //虛擬IP VIP//
mode exclusive
</role>
<role reader>
hosts db3, db4 //m3 m4//
ips 192.168.126.190, 192.168.126.199 //虛擬IP VIP//
mode balanced
</role>
(3)在監控主機上編輯/etc/mysql-mmm/mmm_mon.conf文件。
[root@localhost ~]# vim /etc/mysql-mmm/mmm_mon.conf
include mmm_common.conf
<monitor>
ip 127.0.0.1
pid_path /run/mysql-mmm-monitor.pid
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.126.138,192.168.126.139,192.168.126.136,192.168.126.137/監控服務器的IP/
auto_set_online 10 //自動上線時間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 //用戶名//
monitor_password 123456 //密碼//
(4)在所有數據庫上為mmm_agent和mmm_moniter授權。
MariaDB [(none)]> grant super, replication client, process on *.* to 'mmm_agent'@'192.168.126.%' identified by '123456';
MariaDB [(none)]> grant replication client on *.* to 'mmm_monitor'@'192.168.126.%' identified by '123456';
(5)修改所有數據庫的mmm_agent.conf。
[root@localhost mysql-mmm]# vim /etc/mysql-mmm/mmm_agent.conf
include mmm_common.conf
# The 'this' variable refers to this server. Proper operation requires
# that 'this' server (db1 by default), as well as all other servers, have the
# proper IP addresses set in mmm_common.conf.
this db1 //根據規劃進行逐一調整//
(6)在所有數據庫服務器上啟動mysql-mmm-agent。
[root@localhost mysql-mmm]# systemctl start mysql-mmm-agent.service
[root@localhost mysql-mmm]# systemctl enable mysql-mmm-agent.service //開機自啟動//
(7)啟動mysql-mmm-monitor監控主機。
[root@localhost ~]# systemctl start mysql-mmm-monitor.service
(8)在監控服務器上查看各節點的情況。
[root@localhost ~]# mmm_control show
db1(192.168.126.138) master/ONLINE. Roles: writer(192.168.126.188) //虛擬IP//
db2(192.168.126.139) master/ONLINE. Roles:
db3(192.168.126.136) slave/ONLINE. Roles: reader(192.168.126.190)
db4(192.168.126.137) slave/ONLINE. Roles: reader(192.168.126.199
(9)故障轉移切換
1)停止m1 確認 虛擬地址 188 是否移動到 m2 上。注意:主不會搶占
[root@localhost mysql-mmm]# systemctl stop mariadb.service //停止 m1主服務器//
[root@localhost ~]# mmm_control show
db1(192.168.126.138) master/HARD_OFFLINE. Roles: //離線狀態//
db2(192.168.126.139) master/ONLINE. Roles: writer(192.168.126.188)
db3(192.168.126.136) slave/ONLINE. Roles: reader(192.168.126.190)
db4(192.168.126.137) slave/ONLINE. Roles: reader(192.168.126.199)
[root@localhost mysql-mmm]# systemctl stop mariadb.service //停止 m3從服務器//
[root@localhost ~]# mmm_control show
db1(192.168.126.138) master/HARD_OFFLINE. Roles:
db2(192.168.126.139) master/ONLINE. Roles: writer(192.168.126.188)
db3(192.168.126.136) slave/HARD_OFFLINE. Roles:
db4(192.168.126.137) slave/ONLINE. Roles: reader(192.168.126.190), reader(192.168.126.199)
3)在m1服務器上為監控機地址授權登錄。
MariaDB [(none)]> grant all on *.* to 'testdba'@'192.168.126.140' identified by '123456';
MariaDB [(none)]> flush privileges; //刷新//
4)在監控服務器上登錄。
[root@localhost ~]# yum install mariadb-server mariadb -y
[root@localhost ~]# mysql -utestdba -p -h 192.168.126.188 //虛擬IP//
Enter password: //密碼123456//
.....//省略//
MariaDB [(none)]>
5)在監控服務器上創建數據,測試同步情況。
MariaDB [(none)]> create database abc01; Query OK, 1 row affected (0.01 sec) MariaDB [(none)]> show databases; //主服務器M1// +--------------------+ | Database | +--------------------+ | information_schema | | abc01 | | dba01 | | mysql | | performance_schema | | test | +--------------------+ 6 rows in set (0.03 sec) MariaDB [(none)]> show databases; //從服務器M3// +--------------------+ | Database | +--------------------+ | information_schema | | abc01 | | dba01 | | mysql | | performance_schema | | test | +--------------------+
6 rows in set (0.05 sec)
對于以上有關MySQL-MMM高可用群集部署詳解,如果大家還有更多需要了解的可以持續關注我們億速云的行業推新,如需獲取專業解答,可在官網聯系售前售后的,希望該文章可給大家帶來一定的知識更新。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。