MMM介紹mysql
MMM(Master-Master replication manager for MySQL)是一套支持雙主故障切換和雙主平常管理的腳本程序。MMM使用Perl語言開發,主要用來監控和管理MySQL Master-Master(雙主)複製,雖然叫作雙主複製,可是業務上同一時刻只容許對一個主進行寫入,另外一臺備選主上提供部分讀服務,以加速在主主切換時刻備選主的預熱,能夠說MMM這套腳本程序一方面實現了故障切換的功能,另外一方面其內部附加的工具腳本也能夠實現多個slave的read負載均衡。linux
MMM提供了自動和手動兩種方式移除一組服務器中複製延遲較高的服務器的虛擬ip,同時它還能夠備份數據,實現兩節點之間的數據同步等。因爲MMM沒法徹底的保證數據一致性,因此MMM適用於對數據的一致性要求不是很高,可是又想最大程度的保證業務可用性的場景。對於那些對數據的一致性要求很高的業務,很是不建議採用MMM這種高可用架構。sql
MySQL-MMM優缺點數據庫
優勢:高可用性,擴展性好,出現故障自動切換,對於主主同步,在同一時間只提供一臺數據庫寫操做,保證的數據的一致性。vim
缺點:Monitor節點是單點,能夠結合Keepalived實現高可用。緩存
MySQL-MMM工做原理安全
MMM(Master-Master replication managerfor Mysql,Mysql主主複製管理器)是一套靈活的腳本程序,基於perl實現,用來對mysql replication進行監控和故障遷移,服務器
並能管理mysql Master-Master複製的配置(同一時間只有一個節點是可寫的)。架構
mmm_mond:監控進程,負責全部的監控工做,決定和處理全部節點角色活動。此腳本須要在監管機上運行。負載均衡
mmm_agentd:運行在每一個mysql服務器上(Master和Slave)的代理進程,完成監控的探針工做和執行簡單的遠端服務設置。此腳本須要在被監管機上運行。
mmm_control:一個簡單的腳本,提供管理mmm_mond進程的命令。
mysql-mmm的監管端會提供多個虛擬IP(VIP),包括一個可寫VIP,多個可讀VIP,經過監管的管理,這些IP會綁定在可用mysql之上,當某一臺mysql宕機時,監管會將VIP遷移
至其餘mysql。在整個監管過程當中,須要在mysql中添加相關受權用戶,以便讓mysql能夠支持監理機的維護。受權的用戶包括一個mmm_monitor用戶和一個mmm_agent用戶,若是
想使用mmm的備份工具則還要添加一個mmm_tools用戶。
實驗基本環境
實驗系統:CentOS 7_x86_64
實驗前提:防火牆和selinux都關閉!
實驗說明:本實驗共有5臺主機,IP分配如表
實驗軟件:mariadb mysql-mmm mysql-mmm-monitor mysql-mmm-agent
功能,IP地址與別名分配表
功能 | IP | id |
主服務器一 | 192.168.137.10 | M1 |
主服務器二 | 192.168.137.11 | M2 |
從服務器一 | 192.168.137.12 | s1 |
從服務器二 | 192.168.137.13 | s2 |
mmm監控 | 192.168.137.14 | mo |
實驗描述拓撲圖
操做!
全部機器中!
關閉全部機器的防火牆和安全策略 systemctl stop firewalld.service setenforce 0獲取阿里雲的yum基礎配置 wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo 安裝epel源 yum -y install epel-release 清空yum緩存,從新創建元數據 yum clean all && yum makecache 安裝mariadb,MMM的客戶端和服務端 yum install mariadb-server mariadb –y yum install mysql-mmm* -y
M1中
修改m1配置文件,配置mariadb # 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 server_id=1log_slave_updates=true //容許slave更新日誌 sync_binlog=1auto_increment_increment=2 auto_increment_offset=1 啓動服務 systemctl start mariadb.service
使用scp命令把my.cnf這個配置文件同步複製到其餘的三臺mysql主機上,除了監控機器MO。 scp /etc/my.cnf root@192.168.137.11:/etc/ scp /etc/my.cnf root@192.168.137.12:/etc/ scp /etc/my.cnf root@192.168.137.13:/etc/ 務必單獨修改每臺機器/etc/my.cnf中 server_id=1這裏M2改爲了 server_id=2以此類推
逐個驗證時候成功安裝
[root@cent ~]# mysql Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 2Server version: 5.5.60-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)]>
主從複製,主主複製
M1,M2中
查看日誌以及位置參數 MariaDB [(none)]> show master status; 兩臺得出的結果是相同的 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) M1,M2互相受權 M1,M2: MariaDB [(none)]> grant replication slave on *.* to 'replication'@'192.168.137.%' identified by '123456'; 同步 M1: change master to master_host='192.168.137.11',master_user='replication',master_password='123456',master_log_file='mysql_bin.000003',master_log_pos=245; M2: change master to master_host='192.168.137.10',master_user='replication',master_password='123456',master_log_file='mysql_bin.000003',master_log_pos=245;
S1,S2中的同步,這裏都以M1爲同步對象。
change master to master_host='192.168.137.10',master_user='replication',master_password='123456',master_log_file='mysql_bin.000003',master_log_pos=245; flush privileges; //刷新
驗證salve
啓動salve MariaDB [(none)]> start slave; Query OK, 0 rows affected (0.00 sec) 查看slave狀態 MariaDB [(none)]> show slave status\G 看到這兩項爲yes即成功 Slave_IO_Running: Yes Slave_SQL_Running: Yes
配置MMM
隨便一臺機器
修改以下
vim /etc/mysql-mmm/mmm_common.conf <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 db1> ip 192.168.137.10 //M1地址 mode master peer db2 //指向M2 </host> <host db2> ip 192.168.137.11 //M2地址 mode master peer db1 //指向M1 </host> <host db3> ip 192.168.137.12 //S1地址 mode slave </host> <host db4> ip 192.168.137.13 //S2地址 mode slave </host> <role writer> hosts db1, db2 //M1與M2 ips 192.168.137.100 //M1與M2共同虛擬一個IP mode exclusive </role> <role reader> hosts db3, db4 //S1與S2 ips 192.168.137.200, 192.168.137.150 //S1,S2的虛擬IP mode balanced</role> 複製到其餘四臺主機,包括MO監控機 scp /etc/mysql-mmm/mmm_common.conf root@192.168.137.11:/etc/mysql-mmm/
MO監控機操做
vim /etc/mysql-mmm/mmm_mon.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.137.10,192.168.137.11,192.168.137.12,192.168.137.13,192.168.137.14//列出全部數據庫IP auto_set_online 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 //密碼 </host> debug 0
再MySQL中給MMM受權,每臺機器都要。
grant super, replication client,process on *.* to 'mmm_agent'@'192.168.137.%' identified by '123456'; grant replication client on *.* to 'mmm_monitor'@'192.168.137.%' identified by '123456'; flush privileges; //刷新
更新每臺機器的server名,不能相同,我設置了12345。
vim /etc/mysql-mmm/mmm_agent.conf 修改以下 this db1
設置五臺!
啓動服務
M1,M2,S1,S2啓動agent systemctl start mysql-mmm-agent.service systemctl enable mysql-mmm-agent.service 監控機啓動監控服務 systemctl start mysql-mmm-monitor.service
監控服務的查驗,以下一切正常!
查看被監控的主機 [root@zcent4 mysql-mmm]# mmm_control show db1(192.168.137.10) master/ONLINE. Roles: writer(192.168.137.100) db2(192.168.137.11) master/ONLINE. Roles: db3(192.168.137.12) slave/ONLINE. Roles: reader(192.168.137.200) db4(192.168.137.13) slave/ONLINE. Roles: reader(192.168.137.150) 切換虛擬ip綁定的主機 [root@zcent4 mysql-mmm]# mmm_control move_role writer db2 OK: Role 'writer' has been moved from 'db1' to 'db2'. Now you can wait some time and check new roles info![root@zcent4 mysql-mmm]# mmm_control show db1(192.168.137.10) master/ONLINE. Roles: db2(192.168.137.11) master/ONLINE. Roles: writer(192.168.137.100) db3(192.168.137.12) slave/ONLINE. Roles: reader(192.168.137.200) db4(192.168.137.13) slave/ONLINE. Roles: reader(192.168.137.150) 檢測監控功能完善性 [root@zcent4 mysql-mmm]# mmm_control checks alldb4 ping [last change: 2018/09/07 23:02:12] OK db4 mysql [last change: 2018/09/07 23:03:06] OK db4 rep_threads [last change: 2018/09/07 23:11:57] OK db4 rep_backlog [last change: 2018/09/07 23:11:52] OK: Backlog is nulldb2 ping [last change: 2018/09/07 22:57:09] OK db2 mysql [last change: 2018/09/07 22:57:09] OK db2 rep_threads [last change: 2018/09/07 22:57:09] OK db2 rep_backlog [last change: 2018/09/07 22:57:09] OK: Backlog is nulldb3 ping [last change: 2018/09/07 23:02:09] OK db3 mysql [last change: 2018/09/07 23:02:50] OK db3 rep_threads [last change: 2018/09/07 23:11:42] OK db3 rep_backlog [last change: 2018/09/07 23:11:36] OK: Backlog is nulldb1 ping [last change: 2018/09/07 22:57:09] OK db1 mysql [last change: 2018/09/07 22:57:09] OK db1 rep_threads [last change: 2018/09/07 22:57:09] OK db1 rep_backlog [last change: 2018/09/07 22:57:09] OK: Backlog is null
故障測試!
首先M1,M2受權給測試機器
MariaDB [(none)]> grant all on *.* to 'testdb'@'192.168.137.14' identified by '123456'; Query OK, 0 rows affected (0.01 sec) MariaDB [(none)]> flush privileges; Query OK, 0 rows affected (0.00 sec)
用監控機當客戶機測試,成功登錄
[root@zcent4 mysql-mmm]# mysql -utestdb -p123456 -h 192.168.137.100 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 2430Server version: 5.5.60-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 teest; Query OK, 1 row affected (0.00 sec) M1: MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mood | | mysql | | performance_schema | | teest | | test | +--------------------+ 6 rows in set (0.00 sec)
模擬M2掉線,查看是否自動切換主服務器的虛擬IP
M2: [root@cent ~]# systemctl stop mariadb.service 監控機: [root@zcent4 mysql-mmm]# mmm_control show db1(192.168.137.10) master/ONLINE. Roles: writer(192.168.137.100) db2(192.168.137.11) master/HARD_OFFLINE. Roles: db3(192.168.137.12) slave/ONLINE. Roles: reader(192.168.137.200) db4(192.168.137.13) slave/ONLINE. Roles: reader(192.168.137.150)
顯然是能夠的!讀寫複製正常
上線M2,可是注意!M2並不會搶佔,不帶這個功能!
模擬S2掉線,觀察狀況
S2:[root@cent ~]# systemctl stop mariadb.service 監控機:[root@zcent4 mysql-mmm]# mmm_control show db1(192.168.137.10) master/ONLINE. Roles: writer(192.168.137.100) db2(192.168.137.11) master/ONLINE. Roles: db3(192.168.137.12) slave/ONLINE. Roles: reader(192.168.137.150), reader(192.168.137.200) db4(192.168.137.13) slave/HARD_OFFLINE. Roles: 實行了頂替,期間讀寫複製正常
總結
1.monitor程序負責監控db服務器的狀態,包括Mysql數據庫、服務器是否運行、複製線程是否正常、主從延時等;它還用於控制agent程序處理故障。 2.若是master-db1主庫宕機,master-db2複製應用又落後於master-db1時就變成了主可寫狀態,這時的數據主沒法保證一致性。 3.monitor會每隔幾秒鐘監控db服務器的狀態,若是db服務器已經從故障變成了正常,那麼monitor會自動在60s以後將其設置爲online狀態 (默認是60s能夠設爲其它的值),我上面改爲了10. 4.實驗過程當中遇到從服務器沒法啓動agent,重啓解決了,緣由不詳。