MMM(Master-Master replication manager for MySQL)是一套支持雙主故障切換和雙主平常管理的腳本程序。MMM使用Perl語言開發,主要用來監控和管理MySQL Master-Master(雙主)複製,雖然叫作雙主複製,可是業務上同一時刻只容許對一個主進行寫入,另外一臺備選主上提供部分讀服務,以加速在主主切換時刻備選主的預熱,能夠說MMM這套腳本程序一方面實現了故障切換的功能,另外一方面其內部附加的工具腳本也能夠實現多個slave的read負載均衡。html
MMM提供了自動和手動兩種方式移除一組服務器中複製延遲較高的服務器的虛擬ip,同時它還能夠備份數據,實現兩節點之間的數據同步等。因爲MMM沒法徹底的保證數據一致性,因此MMM適用於對數據的一致性要求不是很高,可是又想最大程度的保證業務可用性的場景。對於那些對數據的一致性要求很高的業務,很是不建議採用MMM這種高可用架構。mysql
優勢:sql
1 穩定和成熟的開源產品,通過了時間的考驗 核心技術是mysql本身的技術,只是使用腳本程序來控制,因此在原理上比較容易理解,並且管理可以更智能化。 2 安裝簡單,配置簡單,使用簡單 3 功能強大 (HA,failover,tools套件,cluster模式能夠一個monitor管理多個mmm組)
缺點:數據庫
1 因爲架構裏只有一個寫入點,因此擴展性是有限的,可是對通常中型企業夠用了。 解決方案:對於大應用能夠採起垂直拆分到多個mmm架構的方式,使用mmm cluster來管理。 2 對於讀寫分離和讀負載均衡仍是要程序來開發或者使用其餘工具完成。
MySQL-MMM架構圖:vim
mysql-mmm運行機制:安全
mysql-mmm安裝需求服務器
Server n+1: N檯安裝mysql的機器和1檯安裝mmm monitor的機器。 2*(n+1)Ips: 每一個主機一個固定ip、一個虛擬IP(reader role),全局一個writer role IP
Monitor User: 一個能夠在mmm monitor機器上使用的而且擁有REPLICATION,CLIENT權限的mysql用戶 Agent User: 一個能夠在mmm agent機器上使用的而且擁有super,replication,client,process權限的mysql用戶 Replication user: 一個slaves主機上可使用的而且有用replication slave權限的用戶 Tools user: 一個mmm tools主機可使用的,而且有用super,replication client,reload權限的mysql用戶
1.角色:網絡
2.虛擬ip規劃:多線程
3.hosts文件配置(所有機器):架構
192.168.1.11 master-db1 192.168.1.12 master-db2 192.168.1.13 slave-db1 192.168.1.14 slave-db2 192.168.1.15 mmm-monitor
4.其餘:
1 關閉iptables 2 同步時間 3 配置yum和epel源
1.mysql安裝
因爲篇幅有限,安裝步驟見小弟另外一篇文章http://www.cnblogs.com/panwenbin-logs/p/8260931.html
2.編輯配置文件/etc/my.cnf:
master-db1
[mysqld]
server-id = 1 datadir = /Data/apps/mysql-5.6.36/data log-bin = /Data/apps/mysql-5.6.36/data/mysql-bin binlog_format = ROW relay_log = /Data/apps/mysql-5.6.36/data/relay-log auto-increment-increment = 2 auto-increment-offset = 1 sync_binlog = 1 sync_master_info = 1 sync_relay_log = 1 sync_relay_log_info = 1 max_binlog_size = 100M log_slave_updates = 1 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
master-db2
[mysqld] server-id = 2 datadir = /Data/apps/mysql-5.6.36/data log-bin = /Data/apps/mysql-5.6.36/data/mysql-bin binlog_format = ROW relay_log = /Data/apps/mysql-5.6.36/data/relay-log auto-increment-increment = 2 auto-increment-offset = 2 sync_binlog = 1 sync_master_info = 1 sync_relay_log = 1 sync_relay_log_info = 1 max_binlog_size = 100M log_slave_updates = 1 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
slave-db1
[mysqld] server-id = 3 datadir = /Data/apps/mysql-5.6.36/data log-bin = /Data/apps/mysql-5.6.36/data/mysql-bin binlog_format = ROW relay_log = /Data/apps/mysql-5.6.36/data/relay-log sync_binlog = 1 sync_master_info = 1 sync_relay_log = 1 sync_relay_log_info = 1 max_binlog_size = 100M log_slave_updates = 1 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
slave-db2
[mysqld] server-id = 4 datadir = /Data/apps/mysql-5.6.36/data log-bin = /Data/apps/mysql-5.6.36/data/mysql-bin binlog_format = ROW relay_log = /Data/apps/mysql-5.6.36/data/relay-log sync_binlog = 1 sync_master_info = 1 sync_relay_log = 1 sync_relay_log_info = 1 max_binlog_size = 100M log_slave_updates = 1 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
3.重啓以上四臺mysql服務
service mysqld restart
4.在master-db1上建立mmm架構中須要的用戶和權限
[root@master-db1 ~]# mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.6.36-log MySQL Community Server (GPL) ....... Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> GRANT REPLICATION CLIENT ON *.* TO 'mmm_monitor'@'192.168.1.%' IDENTIFIED BY '123456'; Query OK, 0 rows affected (0.19 sec) mysql> GRANT SUPER,REPLICATION CLIENT,PROCESS ON *.* TO 'mmm_agent'@'192.168.1.%' IDENTIFIED BY '123456'; Query OK, 0 rows affected (0.00 sec) mysql> GRANT REPLICATION SLAVE ON *.* TO 'replication'@'192.168.1.%' IDENTIFIED BY '123456'; Query OK, 0 rows affected (0.01 sec)
5.查看二進制日誌位置
mysql> FLUSH TABLES WITH READ LOCK; Query OK, 0 rows affected (0.07 sec) mysql> SHOW MASTER STATUS; +------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 796 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.05 sec)
6.不要關閉這個mysql進程鏈接,避免鎖失效,咱們另起一個ssh鏈接db1服務器,進行數據庫備份:
[root@master-db1 ~]# mysqldump --all-databases > /tmp/database-backup.sql
7.回到剛纔mysql進程,進行解鎖:
mysql> UNLOCK TABLES; Query OK, 0 rows affected (0.05 sec)
8.將database-backup.sql文件複製到其餘db節點:
[root@master-db1 ~]# scp /tmp/database-backup.sql master-db2:/tmp [root@master-db1 ~]# scp /tmp/database-backup.sql slave-db1:/tmp [root@master-db1 ~]# scp /tmp/database-backup.sql slave-db2:/tmp
9.master-db,slave-db1,slave-db2三臺主機導入sql文件,並刷新權限:
[root@master-db2 ~]# mysql < /tmp/database-backup.sql [root@master-db2 ~]# mysql -e "FLUSH PRIVILEGES;"
1.在其餘三臺mysql上將master-db1設爲主服務器
[root@master-db2 ~]# mysql mysql> CHANGE MASTER TO MASTER_HOST='192.168.1.11',MASTER_USER='replication',MASTER_PASSWORD='123456',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=796; Query OK, 0 rows affected, 2 warnings (0.11 sec) mysql> start slave; Query OK, 0 rows affected (0.11 sec)
mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.1.11 Master_User: replication Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 796 Relay_Log_File: relay-log.000002 Relay_Log_Pos: 283 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes
2.查看master-db2的master日誌位置:
mysql> SHOW MASTER STATUS; +------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 636231 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
3.在master-db1上操做,將master-db2設置爲主:
mysql> CHANGE MASTER TO MASTER_HOST='192.168.1.12',MASTER_USER='replication',MASTER_PASSWORD='123456',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=636231; Query OK, 0 rows affected, 2 warnings (0.03 sec) mysql> start slave; Query OK, 0 rows affected (0.01 sec) mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.1.12 Master_User: replication Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 636231 Relay_Log_File: relay-log.000002 Relay_Log_Pos: 283 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes
1.建立Tools user
useradd -s /sbin/nologin mmmd #全部機器
2.查看mmm版本:
yum list all|grep ^mysql-mmm mysql-mmm.noarch 2.2.1-2.el6 @epel mysql-mmm-agent.noarch 2.2.1-2.el6 @epel mysql-mmm-monitor.noarch 2.2.1-2.el6 epel mysql-mmm-tools.noarch 2.2.1-2.el6 epel
3.在mmm-monitor上安裝:
[root@mmm-monitor ~]# yum -y install mysql-mmm-monitor
4.在四臺mysql服務器上安裝:
yum -y install mysql-mmm-agent
5.編寫配置文件,五臺主機必須一致:
[root@mmm-monitor ~]# vim /etc/mysql-mmm/mmm_common.conf active_master_role writer #積極的master角色的標示,全部的db服務器要開啓read_only參數,對於writer服務器監控代理會自動將read_only屬性關閉。 <host default> cluster_interface eth0 #羣集的網絡接口 pid_path /var/run/mysql-mmm/mmm_agentd.pid #pid路徑 bin_path /usr/libexec/mysql-mmm/ #可執行文件路徑 replication_user replication #複製用戶 replication_password 123456 #複製用戶密碼 agent_user mmm_agent #代理用戶 agent_password 123456 #代理用戶密碼 </host>
<host master-db1> #master-db1的host名稱 ip 192.168.1.11 #master-db1的ip mode master #角色屬性,master表明是主 peer master-db2 #與master-db1對等的服務器的host名,也就是master-db2的服務器host名 </host>
<host master-db2> #和master-db1的概念同樣 ip 192.168.1.12 mode master peer master-db1 </host>
<host slave-db1> #從庫的host名,若是存在多個從庫能夠重複同樣的配置 ip 192.168.1.13 #從的ip mode slave #slave的角色屬性表明當前host是從 </host>
<host slave-db2> #和slave-db1的概念同樣 ip 192.168.1.14 mode slave </host>
<role writer> #writer角色配置 hosts master-db1, master-db2 #能進行寫操做的服務器的host名,若是不想切換寫操做這裏能夠只配置master,這樣也能夠避免由於網絡延時而進行write的切換,可是一旦master出現故障那麼當前的MMM就沒有writer了只有對外的read操做。 ips 192.168.1.250 #對外提供的寫操做的虛擬IP mode exclusive #exclusive表明只容許存在一個主,也就是隻能提供一個寫的IP </role>
<role reader> #read角色配置 hosts master-db1, master-db2, slave-db1, slave-db2 #對外提供讀操做的服務器的host名,固然這裏也能夠把master加進來 ips 192.168.1.251, 192.168.1.252, 192.168.1.253, 192.168.1.254 #對外提供讀操做的虛擬ip,這三個ip和host不是一一對應的,而且ips也hosts的數目也能夠不相同,若是這樣配置的話其中一個hosts會分配兩個ip mode balanced #balanced表明負載均衡 </role>
6.複製到其餘服務器上
scp /etc/mysql-mmm/mmm_common.conf 192.168.1.11:/etc/mysql-mmm/mmm_common.conf scp /etc/mysql-mmm/mmm_common.conf 192.168.1.12:/etc/mysql-mmm/mmm_common.conf scp /etc/mysql-mmm/mmm_common.conf 192.168.1.13:/etc/mysql-mmm/mmm_common.conf scp /etc/mysql-mmm/mmm_common.conf 192.168.1.14:/etc/mysql-mmm/mmm_common.conf
7.在全部的MySQL上修改mmm_agent.conf,只須要修改master-db1這裏,是哪臺就改爲哪臺,這裏只給出master-db1的:
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 master-db1
8.配置mmm-monitor上的mmm_mon.conf:
[root@mmm-monitor ~]# vim /etc/mysql-mmm/mmm_mon.conf include mmm_common.conf <monitor> ip 192.168.1.15 pid_path /var/run/mysql-mmm/mmm_mond.pid bin_path /usr/libexec/mysql-mmm status_path /var/lib/mysql-mmm/mmm_mond.status ping_ips 192.168.1.11, 192.168.1.12, 192.168.1.13, 192.168.1.14 auto_set_online 60 # 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
9.啓動服務:
在mmm-monitor啓動:
[root@mmm-monitor ~]# chkconfig mysql-mmm-monitor on [root@mmm-monitor ~]# service mysql-mmm-monitor start
在全部mysql服務器上啓動
chkconfig mysql-mmm-agent on service mysql-mmm-agent start
服務器讀寫採有VIP地址進行讀寫,出現故障時VIP會漂移到其它節點,由其它節點提供服務。
mysql-mmm故障處理機制:
1.首先查看整個集羣的狀態,能夠看到整個集羣狀態正常
[root@mmm-monitor ~]# mmm_control show master-db1(192.168.1.11) master/ONLINE. Roles: reader(192.168.1.251), writer(192.168.1.250) master-db2(192.168.1.12) master/ONLINE. Roles: reader(192.168.1.254) slave-db1(192.168.1.13) slave/ONLINE. Roles: reader(192.168.1.252) slave-db2(192.168.1.14) slave/ONLINE. Roles: reader(192.168.1.253)
2.關閉master-db1上的mysql服務,模擬mysql宕機
[root@master-db1 ~]# service mysqld stop Shutting down MySQL... [肯定]
3.mmm-monitor上查看集羣狀態
[root@mmm-monitor ~]# mmm_control show master-db1(192.168.1.11) master/HARD_OFFLINE. Roles: master-db2(192.168.1.12) master/ONLINE. Roles: reader(192.168.1.254), writer(192.168.1.250) slave-db1(192.168.1.13) slave/ONLINE. Roles: reader(192.168.1.251), reader(192.168.1.252) slave-db2(192.168.1.14) slave/ONLINE. Roles: reader(192.168.1.253)
從顯示結果能夠看出master-db1的狀態有ONLINE轉換爲HARD_OFFLINE,寫VIP轉移到了master-db2主機上。
4.查看slave-db1和slave-db2主從狀態
mysql> show slave status\G #slave-db1 *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.1.12 Master_User: replication Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 636231 Relay_Log_File: relay-log.000002 Relay_Log_Pos: 283 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes
mysql> show slave status\G #slave-db2 *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.1.12 Master_User: replication Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 636231 Relay_Log_File: relay-log.000002 Relay_Log_Pos: 283 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes
能夠看到寫請求的VIP已經轉移到master-db2上了,且從節點的主都指向了master-db2
5.啓動master-db1的mysql服務
[root@master-db1 ~]# service mysqld start Starting MySQL...... [肯定]
6.再次查看集羣狀態(大概等待一分鐘左右)
[root@mmm-monitor ~]# mmm_control show master-db1(192.168.1.11) master/ONLINE. Roles: reader(192.168.1.252) master-db2(192.168.1.12) master/ONLINE. Roles: reader(192.168.1.254), writer(192.168.1.250) slave-db1(192.168.1.13) slave/ONLINE. Roles: reader(192.168.1.251) slave-db2(192.168.1.14) slave/ONLINE. Roles: reader(192.168.1.253)
7.再次slave-db1和slave-db2主從狀態
mysql> show slave status\G #slave-db1 *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.1.12 Master_User: replication Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 636231 Relay_Log_File: relay-log.000002 Relay_Log_Pos: 283 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes
mysql> show slave status\G #slave-db2 *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.1.12 Master_User: replication Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 636231 Relay_Log_File: relay-log.000002 Relay_Log_Pos: 283 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes
能夠看到主庫啓動不會接管主,直到現有的主再次宕機。
1.對外提供讀寫的虛擬IP是由monitor程序控制。若是monitor沒有啓動那麼db服務器不會被分配虛擬ip,可是若是已經分配好了虛擬ip,當monitor程序關閉了原先分配的虛擬ip不會當即關閉外部程序還能夠鏈接訪問(只要不重啓網絡),這樣的好處就是對於monitor的可靠性要求就會低一些,可是若是這個時候其中的某一個db服務器故障了就沒法處理切換,也就是原先的虛擬ip仍是維持不變,掛掉的那臺DB的虛擬ip會變的不可訪問。 2.agent程序受monitor程序的控制處理write切換,從庫切換等操做。若是monitor進程關閉了那麼agent進程就起不到什麼做用,它自己不能處理故障。 3.monitor程序負責監控db服務器的狀態,包括Mysql數據庫、服務器是否運行、複製線程是否正常、主從延時等;它還用於控制agent程序處理故障。 4.monitor會每隔幾秒鐘監控db服務器的狀態,若是db服務器已經從故障變成了正常,那麼monitor會自動在60s以後將其設置爲online狀態(默認是60s能夠設爲其它的值),有監控端的配置文件參數「auto_set_online」決定,羣集服務器的狀態有三種分別是:HARD_OFFLINE→AWAITING_RECOVERY→online 5.默認monitor會控制mmm_agent會將writer db服務器read_only修改成OFF,其它的db服務器read_only修改成ON,因此爲了嚴謹能夠在全部的服務器的my.cnf文件中加入read_only=1由monitor控制來控制writer和read,root用戶和複製用戶不受read_only參數的影響。
(1)master-db2備選主節點宕機不影響集羣的狀態,就是移除了master-db2備選節點的讀狀態。 (2)master-db1主節點宕機,由master-db2備選主節點接管寫角色,slave-db1,slave-db2指向新master2主庫進行復制,slave-db1,slave-db2會自動change master到master2. (3)若是master-db1主庫宕機,master-db2複製應用又落後於master-db1時就變成了主可寫狀態,這時的數據主沒法保證一致性。 若是master-db2,slave-db1,slave-db2延遲於master-db1主,這個時master-db1宕機,slave-db1,slave-db2將會等待數據追上master-db1後,再從新指向新的主master-db2進行復制操做,這時的數據也沒法保證同步的一致性。 (4)若是採用MMM高可用架構,主,主備選節點機器配置同樣,並且開啓半同步進一步提升安全性或採用MariaDB/mysql5.7進行多線程從複製,提升複製的性能。