一:簡介
MMM 即Master-Master Replication Manager for MySQL(mysql主主複製管理器)關於mysql主主複製配置的監控、故障轉移和管理的一套可伸縮的腳本套件(在任何時候只有一個節點可以被寫 入),這個套件也能對居於標準的主從配置的任意數量的從服務器進行讀負載均衡,所以你可以用它來在一組居於複製的服務器啓動虛擬ip,除此之外,它還有實 現數據備份、節點之間重新同步功能的腳本。MySQL本身沒有提供replication failover的解決方案,通過MMM方案能實現服務器的故障轉移,從而實現mysql的高可用。MMM不僅能提供浮動IP的功能,更可貴的是如果當前 的主服務器掛掉後,會將你後端的從服務器自動轉向新的主服務器進行同步複製,不用手工更改同步配置。這個方案是目前比較成熟的解決方案.
二:試驗拓撲圖
從拓撲圖中可以看出DB2與DB1互爲主從,DB1是DB3的主服務器.而上面的monitor起到動靜讀寫分離.使DB1爲只寫服務器,DB2和DB3爲只讀服務器.
三:配置過程
1)配置的DB1 在DB1的/etc/my.cnf下添加如下配置選項 [mysqld] log-bin=master-bin #二進制日誌文件 server-id=1 #server ID 各個的DB是不一樣的 binlog_format=row #二進制日誌文件記錄格式 log-slave-updates sync_binlog=1 auto_increment_increment=2 auto_increment_offset=1 給用戶授權 MariaDB [(none)]> grant replication slave,replication client on *.* to 'llh'@'172.16.16.1' identified by 'replpass'; Query OK, 0 rows affected (0.12 sec) MariaDB [(none)]> grant replication slave,replication client on *.* to 'llh'@'172.16.16.3' identified by 'replpass'; Query OK, 0 rows affected (0.00 sec) 查看二進制日誌 MariaDB [(none)]> show master status; +-------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +-------------------+----------+--------------+------------------+ | master-bin.000009 | 245 | | | +-------------------+----------+--------------+------------------+ 2)配置DB2 log-bin=mysql-bin binlog_format=row log-slave-updates sync_binlog=1 auto_increment_increment=2 auto_increment_offset=2 server-id=2 給用戶授權 MariaDB [(none)]> grant replication slave,replication client on *.* to 'repluser'@'172.16.16.5' identified by 'replpass'; Query OK, 0 rows affected (0.15 sec) MariaDB [(none)]> show master status -> ; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000011 | 541 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.04 sec) 3)配置DB3 [mysqld] server-id=3 log-bin=mysql-bin log-slave-updates relay-log=relay-log-bin 在DB3上配置主從服務,連接DB1爲主服務 MariaDB [(none)]> change master to master_host='172.16.16.5',master_user='llh',master_password='llh',master_log_file='master-bin.000009',master_log_pos=245; MariaDB [(none)]> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 172.16.16.5 Master_User: llh Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-bin.000009 Read_Master_Log_Pos: 245 Relay_Log_File: relay-log-bin.000012 Relay_Log_Pos: 530 Relay_Master_Log_File: master-bin.000009 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: 245 Relay_Log_Space: 866 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 1 row in set (0.00 sec) ERROR: No query specified 配置DB1爲DB2的主服務器 MariaDB [(none)]> change master to master_host='172.16.16.5',master_user='llh',master_password='llh',master_log_file='master-bin.000009',master_log_pos=245; Query OK, 0 rows affected (0.03 sec) MariaDB [(none)]> start slave; Query OK, 0 rows affected (0.07 sec) MariaDB [(none)]> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 172.16.16.5 Master_User: llh Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-bin.000009 Read_Master_Log_Pos: 245 Relay_Log_File: stu16-relay-bin.000002 Relay_Log_Pos: 530 Relay_Master_Log_File: master-bin.000009 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: 245 Relay_Log_Space: 824 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 1 row in set (0.01 sec) 配置DB2是DB1的主服務器 MariaDB [(none)]> change master to master_host='172.16.16.1',master_user='llh',master_password='llh',master_log_file='master-bin.000011',master_log_pos=541; Query OK, 0 rows affected (0.03 sec) MariaDB [(none)]> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 172.16.16.1 Master_User: llh Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000011 Read_Master_Log_Pos: 541 Relay_Log_File: localhost-relay-bin.000016 Relay_Log_Pos: 486 Relay_Master_Log_File: mysql-bin.000011 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: 541 Relay_Log_Space: 1068 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 1 row in set (0.00 sec)
四:測試主從是否建立
1)在DB2上建立庫llh MariaDB [(none)]> create database llh; Query OK, 1 row affected (0.08 sec) MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | llh | | mysql | | mytest | | performance_schema | | test | | testdb | +--------------------+ 7 rows in set (0.00 sec) MariaDB [(none)]> 在DB1上查看是否有表llh MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | llh | #有所創建的表說名和數據可以同步過來. | mysql | | mytest | | performance_schema | | test | | testdb | +--------------------+ 7 rows in set (0.06 sec) MariaDB [(none)]> 在DB1 上創建表name 並插入數據. MariaDB [llh]> create table name(name char(20)); Query OK, 0 rows affected (0.22 sec) MariaDB [llh]> insert into name (name) values ('llh'); Query OK, 1 row affected (0.02 sec) 在DB3上看是否能夠查到數據 MariaDB [llh]> show tables; +---------------+ | Tables_in_llh | +---------------+ | name | +---------------+ 1 row in set (0.11 sec) MariaDB [llh]> select *from name; #有數據說明數據可以同步過來. +------+ | name | +------+ | llh | +------+ 1 row in set (0.06 sec) MariaDB [llh]> 綜上驗證說明三者之間的關係已經建立.
五:安裝mysql-mmm-agent
在DB1~3上安裝mysql-mmmo-agent 在每一個節點上要給Monitor授權用戶 (三個節點都要授權) MariaDB [(none)]> GRANT SUPER, REPLICATION CLIENT, PROCESS ON *.* TO 'mmm_agent'@'172.16.16.2' IDENTIFIED BY '123456'; Query OK, 0 rows affected (0.01 sec) MariaDB [(none)]> GRANT REPLICATION CLIENT ON *.* TO 'mmm_monitor'@'172.16.16.2' IDENTIFIED BY '123456'; Query OK, 0 rows affected (0.01 sec) MariaDB [(none)]> GRANT REPLICATION CLIENT ON *.* TO 'llh'@'172.16.16.2' IDENTIFIED BY 'llh'; Query OK, 0 rows affected (0.01 sec) 在Monitor節點上要安裝 yum -y install mysql-mmm* #包括監控端,所有都要安裝.
六:在Monitor端的設置/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 llh #用戶 replication_password llh #密碼 agent_user mmm_agent #代理用戶 agent_password 123456 #代理密碼 </host> <host db1> ip 172.16.16.5 mode master peer db2 </host> <host db2> ip 172.16.16.1 mode master peer db1 </host> <host db3> #默認是註銷的,此處要把這個啓動 ip 172.16.16.3 mode slave </host> <role writer> hosts db1, db2 ips 172.16.16.6 將此文件分發到各DB1~3中的/etc/mysql-mmm/下
七:每一個DB中都會有mmm_agent的配置文件,編輯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 db2 修改最後一處的this db2 對應爲自己host. 這個地方要修改對
八:在Monitor上,修改mmm_mon.conf文件
include mmm_common.conf <monitor> ip 172.16.16.2 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.16.5, 172.16.16.1 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 #監控DB用戶 monitor_password 123456 #監控DB密碼 </host> debug 0
九:啓動服務
DB1~DB3 :service mysql-mmm-agent start
Monitor :service mysql-mmm-monitor start
查看服務是否起來
各DB已經ONLINE 且DB1爲writer; DB2和DB3爲reader.
模擬DB2下線
服務自動轉到DB3上.
模擬DB2上線