MySQL 5.7已經開始支持了多源複製,相信小夥們都很激動,MySQL 5.7以前只能實現一主一從、一主多從或者多主多從的複製,若是想實現多主一從的複製,只好使用MariaDB,可是MariaDB又與官方的MySQL版本不兼容的,在MySQL 5.7版本已經能夠實現多主一從的複製了。MySQL 5.7版本相比以前的版本,不管在功能仍是性能、安全等方面都已經提高了很多,值得你們去研究和使用。 html
MySQL 5.7版本以前的最多見的複製方式,一主一從或者一主多從的架構:mysql
MySQL 5.7以後就能夠實現多主一從的複製:sql
多主一從架構帶來的好處(我的總結):數據庫
1、在從服務器進行數據彙總,若是咱們的主服務器進行了分庫分表的操做,爲了實現後期的一些數據統計功能,每每須要把數據彙總在一塊兒再統計。安全
2、若是咱們想在從服務器時時對主服務器的數據進行備份,在MySQL 5.7以前每個主服務器都須要一個從服務器,這樣很容易形成資源浪費,同時也加大了DBA的維護成本,但MySQL 5.7引入多源複製,能夠把多個主服務器的數據同步到一個從服務器進行備份。服務器
下面演示一下在MySQL 5.7下搭建多主一從的過程:架構
實驗環境:ide
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性能
1、分別在Master_1和Master_2上導出須要同步的數據庫:spa
在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/
2、在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)
3、分別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]#
4、登陸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)]>
5、驗證數據是否同步
在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)
成功的實現了多主一從的環境搭建,*。*
總結:
1、MySQL 5.7的多源複製,能有效的解決分庫分表的數據統計問題,同時也能夠實如今一臺從服務器對多臺主服務器的數據備份。
2、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 您的支持是對博主最大的鼓勵,感謝您的認真閱讀。本文版權歸做者全部,歡迎轉載,但請保留該聲明。 |