MySQL 5.7已經開始支持了多源復制,相信小伙們都很激動,MySQL 5.7之前只能實現一主一從、一主多從或者多主多從的復制,如果想實現多主一從的復制,只好使用MariaDB,但是MariaDB又與官方的MySQL版本不兼容的,在MySQL 5.7版本已經可以實現多主一從的復制了。MySQL 5.7版本相比之前的版本,無論在功能還是性能、安全等方面都已經提升了不少,值得大家去研究和使用。
MySQL 5.7版本之前的最常見的復制方式,一主一從或者一主多從的架構:
MySQL 5.7之后就可以實現多主一從的復制:
多主一從架構帶來的好處(個人總結):
一、在從服務器進行數據匯總,如果我們的主服務器進行了分庫分表的操作,為了實現后期的一些數據統計功能,往往需要把數據匯總在一起再統計。
二、如果我們想在從服務器時時對主服務器的數據進行備份,在MySQL 5.7之前每一個主服務器都需要一個從服務器,這樣很容易造成資源浪費,同時也加大了DBA的維護成本,但MySQL 5.7引入多源復制,可以把多個主服務器的數據同步到一個從服務器進行備份。
下面演示一下在MySQL 5.7下搭建多主一從的過程:
實驗環境:
Master_1: 192.168.10.128 Master_2: 192.168.10.129 Slave_3: 192.168.10.130
MySQL 5.7的搭建過程,我在這里就不作過多的演示,詳細的搭建過程可以參考我之前寫的博客:http://www.cnblogs.com/xuanzhi201111/p/5148113.html
一、分別在Master_1和Master_2上導出需要同步的數據庫:
在Master_1:
[root@Master_1 mysql]# mysqldump -uroot -p123456 --master-data=2 --single-transaction --databases --add-drop-database xuanzhi >xuanzhi.sql
在Master_2:
[root@Master_2 mysql]# mysqldump -uroot -p123456 --master-data=2 --single-transaction --databases --add-drop-database xuanzhi_2 >xuanzhi_2.sql
把分別把備份scp到Slave上:
[root@Master_1 mysql]# scp -P22 xuanzhi.sql 192.168.10.130:/data/service/mysql/
[root@Master_2 mysql]# scp -P22 xuanzhi_2.sql 192.168.10.130:/data/service/mysql/
二、在Master_1和Master_2上創建復制賬號,這個操作跟MySQL 5.7之前版本一樣:
在Master_1:
<Master_1>[(none)]> grant replication slave on *.* to 'repl'@'192.168.10.130' identified by '123456'; Query OK, 0 rows affected, 1 warning (0.00 sec)
在Master_2:
<Master_2> [(none)]> grant replication slave on *.* to 'repl'@'192.168.10.130' identified by '123456'; Query OK, 0 rows affected, 1 warning (0.02 sec)
三、分別Slave上把Master_1和Master_2的數據導入Slave服務器,在導入前先修改MySQL存儲master-info和relay-info的方式,即從文件存儲改為表存儲,在my.cnf里添加以下選擇:
master_info_repository=TABLE relay_log_info_repository=TABLE
也可以在線修改,灰常方便:
<Slave> [(none)]> stop slave; Query OK, 0 rows affected (0.02 sec) <Slave> [(none)]> SET GLOBAL master_info_repository = 'TABLE'; Query OK, 0 rows affected (0.00 sec) <Slave> [(none)]> SET GLOBAL relay_log_info_repository = 'TABLE'; Query OK, 0 rows affected (0.00 sec) <Slave> [(none)]>
更多的詳細解析可以參考:http://dev.mysql.com/doc/refman/5.7/en/slave-logs.html
下面進行數據導入:
[root@Slave mysql]# mysql -uroot -p <./xuanzhi.sql
[root@Slave mysql]# mysql -uroot -p123456 <./xuanzhi_2.sql
分別找出Master_1和Master_2的binlog位置和Pos位置:
[root@Slave mysql]# cat xuanzhi.sql |grep " CHANGE MASTER" -- CHANGE MASTER TO MASTER_LOG_FILE='Master_1-bin.000001', MASTER_LOG_POS=1539; [root@Slave mysql]# cat xuanzhi_2.sql |grep " CHANGE MASTER" -- CHANGE MASTER TO MASTER_LOG_FILE='Master_2-bin.000003', MASTER_LOG_POS=630; [root@Slave mysql]#
四、登錄Slave進行同步操作,分別change master到兩台Master服務器,后面以FOR CHANNEL 'CHANNEL_NAME'區分
<Slave> [(none)]> CHANGE MASTER TO MASTER_HOST='192.168.10.128',MASTER_USER='repl', MASTER_PASSWORD='123456',MASTER_LOG_FILE='Master_1-bin.000001',MASTER_LOG_POS=1539 FOR CHANNEL 'Master_1'; Query OK, 0 rows affected, 2 warnings (0.05 sec) <Slave> [(none)]> CHANGE MASTER TO MASTER_HOST='192.168.10.129',MASTER_USER='repl', MASTER_PASSWORD='123456',MASTER_LOG_FILE='Master_2-bin.000003',MASTER_LOG_POS=630 FOR CHANNEL 'Master_2'; Query OK, 0 rows affected, 2 warnings (0.04 sec)
進行啟動slave操作,可以通過start slave的方式去啟動所有的復制,也可以通過啟動單個復制源的方式,下面進行單個復制源的啟動進行演示(停止也是一樣):
<Slave> [(none)]> start slave for CHANNEL 'Master_1'; Query OK, 0 rows affected (0.01 sec) <Slave> [(none)]> start slave for CHANNEL 'Master_2'; Query OK, 0 rows affected (0.02 sec)
正常啟動后,可以查看同步的狀態:執行SHOW SLAVE STATUS FOR CHANNEL 'channel_name'\G
查看復制源Master_1的同步狀態:
<Slave> [(none)]> SHOW SLAVE STATUS FOR CHANNEL 'Master_1'\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.10.128 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: Master_1-bin.000001 Read_Master_Log_Pos: 1987 Relay_Log_File: localhost-relay-bin-master_1.000002 Relay_Log_Pos: 771 Relay_Master_Log_File: Master_1-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: 1987 Relay_Log_Space: 991 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: 100128 Master_UUID: 44b653d4-8843-11e5-b97e-000c29dfaaf7 Master_Info_File: mysql.slave_master_info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0 Replicate_Rewrite_DB: Channel_Name: master_1 Master_TLS_Version: 1 row in set (0.00 sec)
查看復制源Master_2的同步狀態:
<Slave> [(none)]> SHOW SLAVE STATUS FOR CHANNEL 'Master_2'\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.10.129 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: Master_2-bin.000003 Read_Master_Log_Pos: 1078 Relay_Log_File: localhost-relay-bin-master_2.000002 Relay_Log_Pos: 771 Relay_Master_Log_File: Master_2-bin.000003 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: 1078 Relay_Log_Space: 991 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: 100129 Master_UUID: 583f5433-43ef-11e5-8958-000c29d5bdfa Master_Info_File: mysql.slave_master_info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0 Replicate_Rewrite_DB: Channel_Name: master_2 Master_TLS_Version: 1 row in set (0.00 sec)
也可以通過查看performance_schema相關的表查看同步狀態,執行命令:SELECT * FROM performance_schema.replication_connection_status; 監控復制狀態。
+--------------+------------+--------------------------------------+-----------+---------------+---------------------------+--------------------------+--------------------------+-------------------+--------------------+----------------------+ | CHANNEL_NAME | GROUP_NAME | SOURCE_UUID | THREAD_ID | SERVICE_STATE | COUNT_RECEIVED_HEARTBEATS | LAST_HEARTBEAT_TIMESTAMP | RECEIVED_TRANSACTION_SET | LAST_ERROR_NUMBER | LAST_ERROR_MESSAGE | LAST_ERROR_TIMESTAMP | +--------------+------------+--------------------------------------+-----------+---------------+---------------------------+--------------------------+--------------------------+-------------------+--------------------+----------------------+ | master_1 | | 44b653d4-8843-11e5-b97e-000c29dfaaf7 | 34 | ON | 184 | 2015-08-14 08:06:10 | | 0 | | 0000-00-00 00:00:00 | | master_2 | | 583f5433-43ef-11e5-8958-000c29d5bdfa | 36 | ON | 183 | 2015-08-14 08:06:24 | | 0 | | 0000-00-00 00:00:00 | +--------------+------------+--------------------------------------+-----------+---------------+---------------------------+--------------------------+--------------------------+-------------------+--------------------+----------------------+ 2 rows in set (0.00 sec) <Slave> [(none)]>
五、驗證數據是否同步
在Master_1上插入兩條數據:
<Master_1>[xuanzhi]> insert into tb1(name) values ('user1'),('user2'); Query OK, 2 rows affected (0.01 sec)
在Master_2上插入兩條數據:
<Master_2> [xuanzhi_2]> insert into tb2(name) values ('user3'),('user4'); Query OK, 2 rows affected (0.04 sec)
回到Slave上查看數據是否正常把數據同步過來了:
<Slave> [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | xuanzhi | | xuanzhi_2 | +--------------------+ 6 rows in set (0.03 sec) <Slave> [(none)]> select * from xuanzhi.tb1; +----+-------+ | id | name | +----+-------+ | 1 | user1 | | 2 | user2 | +----+-------+ 2 rows in set (0.00 sec) <Slave> [(none)]> select * from xuanzhi_2.tb2; +----+-------+ | id | name | +----+-------+ | 1 | user3 | | 2 | user4 | +----+-------+ 2 rows in set (0.00 sec)
成功的實現了多主一從的環境搭建,*。*
總結:
一、MySQL 5.7的多源復制,能有效的解決分庫分表的數據統計問題,同時也可以實現在一台從服務器對多台主服務器的數據備份。
二、MySQL 5.7的多源復制的出現,我們就不需要使用MariaDB 的多主一從的架構了,讓很多小伙伴又看到了新的希望。
參考資料:
http://dev.mysql.com/doc/refman/5.7/en/change-master-to.html
http://www.longlong.asia/2015/10/21/mysql57-new-features.html
作者:陸炫志 出處:xuanzhi的博客 http://www.cnblogs.com/xuanzhi201111 您的支持是對博主最大的鼓勵,感謝您的認真閱讀。本文版權歸作者所有,歡迎轉載,但請保留該聲明。
|