MMM(MySQL主主複製管理器)是一套支持雙主故障切換和雙主平常管理的腳本程序。主要用來監控和管理MySQL Master-Master(雙主)複製,雖然叫雙主複製,可是業務上同一時刻只容許對一個主進行寫入,另外一臺備選主上提供部分讀服務,以加速在主切換時備選主的預熱,能夠說MMM這套腳本程序一方面實現了故障切換的功能,另外一方面其內部附加的工具腳本也能夠實現多個Slave的read負載均衡。mysql
MMM時一套靈活的腳本程序,基於perl實現,用來對mysql replication進行監控和故障遷移,並能管理MySQL Master-Master複製的配置,如圖所示:
關於MMM高可用架構的說明以下:sql
實驗環境:(mariadb數據庫是mysql的一個分支,它們的命令、操做都同樣)數據庫
本實驗環境使用五臺服務器模擬搭建,實驗環境如表所示。
vim
(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主配置文件。tcp
[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臺數據庫服務器上。ide
[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-m一、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-m一、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-m一、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-m三、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)