1、MMM
mysql
一、簡介sql
MMM即Master-Master Replication Manager for MySQL(mysql主主複製管理器),是關於mysql主主複製配置的監控、故障轉移和管理的一套可伸縮的腳本套件(在任什麼時候候只有一個節點能夠被寫入),這個套件也能基於標準的主從配置的任意數量的從服務器進行讀負載均衡,因此你能夠用它來在一組居於複製的服務器啓動虛擬ip,除此以外,它還有實現數據備份、節點之間從新同步功能的腳本。
MySQL自己沒有提供replication failover的解決方案,經過MMM方案能實現服務器的故障轉移,從而實現mysql的高可用。數據庫
二、MMM的功能vim
MMM主要功能由下面三個腳本提供
mmm_mond :負責全部的監控工做的監控守護進程,決定節點的移除等等
mmm_agentd :運行在mysql服務器上的代理守護進程,經過簡單遠程服務集提供給監控節點安全
mmm_control :經過命令行管理mmm_mond進程bash
三、MMM的優缺點及應用場景服務器
優勢:安全性、穩定性高,可擴展性好,當主服務器掛掉之後,另外一個主當即接管,其餘的從服務器能自動切換,不用人工干預。架構
缺點:至少三個節點,對主機的數量有要求,須要實現讀寫分離,能夠在程序擴展上比較難實現。同時對主從(雙主)同步延遲要求比較高!所以不適合數據安全很是嚴格的場合。負載均衡
應用場所:高訪問量,業務增加快,而且要求實現讀寫分離的場景。ide
2、MMM架構原理圖
3、資源配置
一、服務器列表
服務器 | IP | 主機名 | server id |
monitoring host | 172.16.7.100 | monitor | - |
master 1 | 172.16.7.200 | db1 | 1 |
master 2 | 172.16.7.201 | db2 | 2 |
master 3 | 172.16.7.202 | db3 | 3 |
二、虛擬IP列表
IP | role | description |
172.16.7.1 | write | 對就用程序鏈接的VIP進行寫操做 |
172.16.7.2 | read | 對就用程序鏈接的VIP進行讀操做 |
172.16.7.3 | read |
4、MMM的實現
一、配置master 1
(1)修改/etc/my.cnf配置文件
server-id = 1 datadir = /mydata/data log-bin = /mydata/binglogs/master-bin relay_log = /mydata/relaylogs/relay binlog_format=mixed thread_concurrency = 4 log-slave-updates sync_binlog=1 auto_increment_increment=2 auto_increment-offset=1 |
(2)爲master2 和 slave 受權複製用戶
MariaDB [(none)]> grant replication slave,replication client on *.* to 'repluser'@'172.16.7.201' identified by 'repluser'; MariaDB [(none)]> grant replication slave,replication client on *.* to 'repluser'@'172.16.7.202' identified by 'repluser';
(3)查看狀態信息,從服務器鏈接主服務器時使用
MariaDB [(none)]> show master status; +-------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +-------------------+----------+--------------+------------------+ | master-bin.000001 | 755 | | | +-------------------+----------+--------------+------------------+ |
二、配置master 2
(1)修改/etc/my.cnf配置文件
server-id = 2 datadir = /mydata/data log-bin = /mydata/binglogs/master-bin relay_log = /mydata/relaylogs/relay binlog_format=mixed thread_concurrency = 4 log-slave-updates sync_binlog=1 auto_increment_increment=2 auto_increment-offset=2 |
(2)爲master1 和 slave 受權複製用戶
MariaDB [(none)]> grant replication slave,replication client on *.* to 'repluser'@'172.16.7.200' identified by 'repluser'; MariaDB [(none)]> grant replication slave,replication client on *.* to 'repluser'@'172.16.7.202' identified by 'repluser';
(3)查看狀態信息,從服務器鏈接主服務器時使用
MariaDB [(none)]> show master status; +-------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +-------------------+----------+--------------+------------------+ | master-bin.000001 | 755 | | | +-------------------+----------+--------------+------------------+ |
(4)master 2 鏈接 master 1
MariaDB [(none)]> change master to master_host='172.16.7.200',master_user='repluser',master_password='repluser',master_log_file='master-bin.000001',master_log_pos=755; MariaDB [(none)]> MariaDB [(none)]> start slave; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 172.16.7.200 Master_User: repluser Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-bin.000001 Read_Master_Log_Pos: 755 Relay_Log_File: relay.000003 Relay_Log_Pos: 536 Relay_Master_Log_File: master-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 755 Relay_Log_Space: 823 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1 Master_SSL_Crl: Master_SSL_Crlpath: Using_Gtid: No Gtid_IO_Pos:
(5)master 1 鏈接 master 2
MariaDB [(none)]> change master to master_host='172.16.7.201',master_user='repluser',master_password='repluser',master_log_file='master-bin.000001',master_log_pos=755; Query OK, 0 rows affected (0.02 sec) MariaDB [(none)]> start slave; Query OK, 0 rows affected (0.02 sec) MariaDB [(none)]> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 172.16.7.201 Master_User: repluser Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-bin.000002 Read_Master_Log_Pos: 327 Relay_Log_File: db1-relay-bin.000003 Relay_Log_Pos: 615 Relay_Master_Log_File: master-bin.000002 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 327 Relay_Log_Space: 954 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 2 Master_SSL_Crl: Master_SSL_Crlpath: Using_Gtid: No Gtid_IO_Pos:
(6)測試master 1 與master 2 是否能夠正常主從同步
=========== master 1 ================ MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ 4 rows in set (0.00 sec) MariaDB [(none)]> create database hlbr; Query OK, 1 row affected (0.00 sec) MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | hlbr | | information_schema | | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.00 sec) =========== master 2 ================ MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | hlbr | #同步成功 | information_schema | | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.02 sec) |
(7)測試master 2 與master 1 是否能夠正常主從同步
我在master 2 上建立了一個bynr庫,能夠同步到master 1 上,爲了減少篇幅,這裏就不貼圖了。
通過(6),(7)的測試,說明雙主已然成功
三、配置slave
(1)修改/etc/my.cnf配置文件
server-id = 3 datadir = /mydata/data log-bin = /mydata/binglogs/master-bin relay_log = /mydata/relaylogs/relay binlog_format=mixed thread_concurrency = 4 log-slave-updates sync_binlog=1 |
(2)slave鏈接master 1
MariaDB [(none)]> change master to master_host='172.16.7.200',master_user='repluser',master_password='repluser',master_log_file='master-bin.000001',master_log_pos=755; Query OK, 0 rows affected (0.05 sec) MariaDB [(none)]> start slave; Query OK, 0 rows affected (0.01 sec) MariaDB [(none)]> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 172.16.7.200 Master_User: repluser Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-bin.000001 Read_Master_Log_Pos: 1261 Relay_Log_File: relay.000002 Relay_Log_Pos: 1042 Relay_Master_Log_File: master-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 1261 Relay_Log_Space: 1329 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1 Master_SSL_Crl: Master_SSL_Crlpath: Using_Gtid: No Gtid_IO_Pos:
(3)再次測試,在master 1 上建立數據庫,去slave上查看
====================== master 1 ===================== MariaDB [(none)]> create database hhht; Query OK, 1 row affected (0.02 sec) ======================== slave ====================== MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | bynr | | hhht | | hlbr | #全部的所有同步成功 | information_schema | | mysql | | performance_schema | | test | +--------------------+ |
四、安裝配置mysql-mmm
由於monitor主機負責全部的監控工做,決定節點的移除等等,因此它必需要獲得全部master和slave的受權;且每臺數據庫服務器上都須要安裝mysql-mmm-agent,經過簡單遠程服務集提供給監控節點
(1)在3臺mysql服務器上安裝mysql-mmm-agent
由於有好多依賴關係,因此選擇yum安裝
[root@db1 ~]# yum -y install mysql-mmm-agent |
(2)在3臺mysql服務器上爲monitor受權
用戶 | description | privileges |
monitor user | 用於mmm檢查mysql服務器健康情況的用戶 | replication client |
agent user | 用於mmm代理爲只讀模式和複製等的用戶 | super,replication client,process |
replication user | 複製的用戶 | replication slave |
接下來要作的就是爲上表中的用戶受權給監控主機,因爲咱們的主主、主從已經作好了,權限也已經授過了,因此上表中的第三個就不需重複操做了,並且咱們只需在主服務器上受權剩下的兩個就行了,它會自動同步到另外兩臺mysql服務器上的
MariaDB [(none)]> grant replication client on *.* to 'monitor'@'172.16.7.100' identified by 'monitor'; MariaDB [(none)]> grant super,replication client,process on *.* to 'agent'@'172.16.7.100' identified by 'agent'; MariaDB [(none)]> flush privileges;
(3)在monitor節點上安裝mmm
[root@monitor ~]# yum -y install mysql-mmm* |
①、配置/etc/mysql-mmm/mmm_common.conf
[root@monitor ~]# vim /etc/mysql-mmm/mmm_common.conf active_master_role writer <host default> cluster_interface eth0 pid_path /var/run/mysql-mmm/mmm_agentd.pid bin_path /usr/libexec/mysql-mmm/ replication_user repluser #受權複製用戶 replication_password repluser #密碼 agent_user agent #代理用戶 agent_password agent #代理用戶密碼 </host> <host db1> ip 172.16.7.200 mode master #主的 peer db1 </host> <host db2> ip 172.16.7.201 mode master peer db2 </host> <host db3> ip 172.16.7.202 mode slave #從的 </host> <role writer> hosts db1, db2 ips 172.16.7.1 mode exclusive #exclusive表示排它 </role> <role reader> hosts db2, db3 ips 172.16.7.2, 172.16.7.3 mode balanced #balanced表示均衡 </role> |
②、拷貝此文件到3臺mysql服務器
這個不是數據庫中的數據,不能同步了,只能手動來了
[root@monitor ~]# scp /etc/mysql-mmm/mmm_common.conf root@172.16.7.202:/etc/mysql-mmm/ #3個節點都需這麼作
③、在monitor上修改mmm_mon.conf文件
[root@monitor ~]# vim /etc/mysql-mmm/mmm_mon.conf include mmm_common.conf <monitor> ip 172.16.7.100 #監控主機的ip 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 172.16.7.200, 172.16.7.201, 172.16.7.202 #各數據庫服務器的ip 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 monitor #監控用戶的用戶名 monitor_password monitor #密碼 </host> debug 0 #若是程序無序正常監控,可以使用debug 1進行排查 |
(4)在每一個DB上修改mmm_agent.conf
[root@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 db1 #這一行是標記此主機的角色(引用mmm_common.conf中的host段),當前主機是db幾,這裏就要改成this db幾 |
五、啓動MMM並測試
(1)啓動mysql-mmm-agent
[root@db1 ~]# service mysql-mmm-agent start Starting MMM Agent Daemon: [ OK ] #每一個DB上的mysql-mmm-agent都要啓動 |
(2)啓動monitor監控程序
[root@monitor ~]# chkconfig mysql-mmm-monitor on [root@monitor ~]# service mysql-mmm-monitor start Starting MMM Monitor Daemon: [ OK ] |
(3)在監控主機查看監控狀態
[root@monitor ~]# service mysql-mmm-monitor status mmm_mond (pid 1586) is running... [root@monitor ~]# mmm_control show db1(172.16.7.200) master/ONLINE. Roles: writer(172.16.7.1) db2(172.16.7.201) master/ONLINE. Roles: reader(172.16.7.3) db3(172.16.7.202) slave/ONLINE. Roles: reader(172.16.7.2) |
(4)設置db1離線,再查看狀態
[root@monitor ~]# mmm_control set_offline db1 #設置db1離線 OK: State of 'db1' changed to ADMIN_OFFLINE. Now you can wait some time and check all roles! [root@monitor ~]# mmm_control show db1(172.16.7.200) master/ADMIN_OFFLINE. Roles: #db1已經離線,VIP轉移到了db2上 db2(172.16.7.201) master/ONLINE. Roles: reader(172.16.7.3), writer(172.16.7.1) db3(172.16.7.202) slave/ONLINE. Roles: reader(172.16.7.2) |
在開始的時候,咱們設置了一個寫IP:172.16.7.1和兩個讀IP:172.16.7.二、172.16.7.3,由(3)和(4)能夠看出,在三個節點都正常的狀況下,寫IP是在master 1上,當master 1掛了的話,寫IP就飄到master 2上了,這就解決了mysql數據庫的單點故障,經過MMM實現了mysql數據庫的高可用