MySQL多源複製搭建

来源:http://www.cnblogs.com/godsonfly/archive/2017/05/19/6879694.html
-Advertisement-
Play Games

1.1 實驗概要 1.1.1 實驗假設 本實驗假設已經完成操作系統和MySQL安裝部署。 1.1.2 實驗目的 MySQL5.7的多源複製技術搭建部署,然後簡單測試。 1.1.3 環境信息 操作系統 MySQL版本 伺服器地址 伺服器角色 Centos7 5.7.18 192.168.102.23 ...


1.1     實驗概要

1.1.1  實驗假設

本實驗假設已經完成操作系統和MySQL安裝部署。

1.1.2  實驗目的

MySQL5.7的多源複製技術搭建部署,然後簡單測試。

1.1.3  環境信息

操作系統

MySQL版本

伺服器地址

伺服器角色

Centos7

5.7.18

192.168.102.23

source 1

Centos7

5.7.18

192.168.102.24

source 2

Centos7

5.7.18

192.168.102.25

target

1.1.4  實驗規劃

伺服器地址

伺服器角色

MySQL庫

賬戶

192.168.102.23

source 1

emily

repl23

192.168.102.24

source 2

evelyn

repl24

192.168.102.25

target

 

 

 

source 1.emilyàtarget

source 2.evelynàtarget

username:

source 1:repl23

source 2:repl24

1.2     實驗操作

1.2.1  源端備份資料庫

分別從source1和source2分別備份出emily和evelyn庫,然後分別copy到target中。其中操作步驟如下:

source 1:

##備份

[root@dsm-db-102023 11:26:50 /root]

#mysqldump -uroot –pmysql --single-transaction --master-data=2 --databases emily > /root/dump/emily.sql

##傳輸

[root@dsm-db-102023 11:34:38 /root/dump]

#scp /root/dump/emily.sql [email protected]:/root/dump/

source 2:

##備份

[root@test-mysql-10224 11:19:47 /root]

#mysqldump -uroot -pmysql --single-transaction --master-data=2 --databases evelyn > /root/dump/evelyn.sql

##傳輸

[root@dsm-db-102023 11:34:38 /root/dump]

#scp /root/dump/emily.sql [email protected]:/root/dump/

1.2.2  創建同步賬戶

分別在source1和source2中創建同步賬戶repl23、repl24

source1

[email protected]:3306 [emily]>create user repl23 identified by "repl";

[email protected]:3306 [(none)]>grant replication slave on *.* to 'repl23'@'%';

source2

[email protected]:3306 [evelyn]>create user repl24 identified by "repl";

[email protected]:3306 [evelyn]>grant replication slave on *.* to 'repl24'@'%';

1.2.3  目標端恢複數據庫

## 恢復evelyn庫

[email protected]:3306 [(none)]>reset master

[root@dsm-db-102025 14:01:24 /root/dump]

#mysql -uroot -pmysql < evelyn.sql

## 查看gtid_purged

[email protected]:3306 [(none)]>show global variables like '%gtid_purged%';

+---------------+------------------------------------------+

| Variable_name | Value                                    |

+---------------+------------------------------------------+

| gtid_purged   | 921a9068-24d2-11e7-99b5-005056b59593:1-287,

bd783f44-258f-11e7-914b-005056b5d312:1-28071 |

+---------------+------------------------------------------+

 

## 恢復emily庫

[email protected]:3306 [(none)]>reset master

[root@dsm-db-102025 14:21:22 /root/dump]

#mysql -uroot -pmysql < /root/dump/emily.sql;

##設置source1和source2的gtid_purged

[email protected]:3306 [(none)]>show variables like '%gtid_purged%';

+---------------+------------------------------------------+

| Variable_name | Value                                    |

+---------------+------------------------------------------+

| gtid_purged   | 7937ac78-3c39-11e7-b59e-005056b5d25f:1-4 |

+---------------+------------------------------------------+

## set global gtid_purged='921a9068-24d2-11e7-99b5-005056b59593:1-287,bd783f44-258f-11e7-914b-005056b5d312:1-28071,7937ac78-3c39-11e7-b59e-005056b5d25f:1-4';

 

[email protected]:3306 [(none)]>reset master;

Query OK, 0 rows affected (0.01 sec)

 

[email protected]:3306 [(none)]>set global gtid_purged='921a9068-24d2-11e7-99b5-005056b59593:1-287,bd783f44-258f-11e7-914b-005056b5d312:1-28071,7937ac78-3c39-11e7-b59e-005056b5d25f:1-4';

Query OK, 0 rows affected (0.00 sec)

 

[email protected]:3306 [(none)]>show variables like '%gtid_purged%';

+---------------+------------------------------------------------------------------------------------------------------------------------------------+

| Variable_name | Value                                                                                                                              |

+---------------+------------------------------------------------------------------------------------------------------------------------------------+

| gtid_purged   | 7937ac78-3c39-11e7-b59e-005056b5d25f:1-4,

921a9068-24d2-11e7-99b5-005056b59593:1-287,

bd783f44-258f-11e7-914b-005056b5d312:1-28071 |

+---------------+------------------------------------------------------------------------------------------------------------------------------------+

1 row in set (0.01 sec)

 

[email protected]:3306 [(none)]>

1.2.4  修改MySQL存儲方式

修改MySQL存儲master-info和relay-info的方式,即從文件存儲改為表存儲

## 線上修改

STOP SLAVE;

SET GLOBAL master_info_repository = 'TABLE';

SET GLOBAL relay_log_info_repository = 'TABLE';

##修改配置文件

[mysqld]

master_info_repository=TABLE

relay_log_info_repository=TABLE

1.2.5  同步操作

  • change master

登錄slave進行同步操作,分別change master到兩台master主機,多源複製需要標註

FOR CHANNEL ‘CHANNEL_NAME’區分

##source 1

[email protected]:3306 [(none)]>CHANGE MASTER TO MASTER_HOST='192.168.102.23',MASTER_USER='repl23', MASTER_PASSWORD='repl',master_auto_position=1 FOR CHANNEL 'repl23';

##source 2

[email protected]:3306 [(none)]>CHANGE MASTER TO MASTER_HOST='192.168.102.24',MASTER_USER='repl24', MASTER_PASSWORD='repl',master_auto_position=1 FOR CHANNEL 'repl24';

  • 啟動slave

啟動所有同步: start slave;

啟動單個同步: start slave for channel ‘channel_name’;

##啟動source 1

[email protected]:3306 [(none)]>start slave for channel 'repl23';

##啟動source 2

[email protected]:3306 [(none)]>start slave for channel 'repl24';

  • 檢查slave狀態

檢查所有slave: show slave status\G;

檢查單個slave: show slave status for chennel ‘channel_name’\G;

 

## source 1

[email protected]:3306 [(none)]>show slave status for channel 'repl23'\G;

*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.102.23
Master_User: repl23
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 2849
Relay_Log_File: relay-bin-repl23.000008
Relay_Log_Pos: 454
Relay_Master_Log_File: mysql-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: 2849
Relay_Log_Space: 749
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: 233306
Master_UUID: 7937ac78-3c39-11e7-b59e-005056b5d25f
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: 7937ac78-3c39-11e7-b59e-005056b5d25f:5-13
Executed_Gtid_Set: 7937ac78-3c39-11e7-b59e-005056b5d25f:1-13,
921a9068-24d2-11e7-99b5-005056b59593:1-287,
bd783f44-258f-11e7-914b-005056b5d312:1-28090
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name: repl23
Master_TLS_Version:
1 row in set (0.00 sec)

ERROR:
No query specified

 

##source 2

[email protected]:3306 [(none)]>show slave status for channel 'repl24'\G;

*************************** 1. row ***************************

               Slave_IO_State:

                  Master_Host: 192.168.102.24

                  Master_User: repl24

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000040

          Read_Master_Log_Pos: 185363

               Relay_Log_File: relay-bin-repl24.000003

                Relay_Log_Pos: 454

        Relay_Master_Log_File: mysql-bin.000040

             Slave_IO_Running: No

            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: 185363

              Relay_Log_Space: 4318

              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: NULL

Master_SSL_Verify_Server_Cert: No

                Last_IO_Errno: 1595

                Last_IO_Error: Relay log write failure: could not queue event from master

               Last_SQL_Errno: 0

               Last_SQL_Error:

  Replicate_Ignore_Server_Ids:

             Master_Server_Id: 243306

                  Master_UUID: bd783f44-258f-11e7-914b-005056b5d312

             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: 170519 16:12:33

     Last_SQL_Error_Timestamp:

               Master_SSL_Crl:

           Master_SSL_Crlpath:

           Retrieved_Gtid_Set: bd783f44-258f-11e7-914b-005056b5d312:28072-28088

            Executed_Gtid_Set: 7937ac78-3c39-11e7-b59e-005056b5d25f:1-11,

921a9068-24d2-11e7-99b5-005056b59593:1-287,

bd783f44-258f-11e7-914b-005056b5d312:1-28088

                Auto_Position: 1

         Replicate_Rewrite_DB:

                 Channel_Name: repl24

           Master_TLS_Version:

1 row in set (0.00 sec)

 

ERROR:

No query specified

1.2.6  驗證同步

##source 1

[email protected]:3306 [emily]>insert into emily(id,name)values(2,'evelyn');

Query OK, 1 row affected (0.00 sec)

 

[email protected]:3306 [emily]>select * from emily;

+------+--------+

| id   | name   |

+------+--------+

|    1 | emily  |

|    2 | evelyn |

+------+--------+

2 rows in set (0.00 sec)

 

##target驗證

 

[email protected]:3306 [emily]>select * from emily;

+------+--------+

| id   | name   |

+------+--------+

|    1 | emily  |

|    2 | evelyn |

+------+--------+

2 rows in set (0.00 sec)

 

 

## source 2

[email protected]:3306 [evelyn]>insert into evelyn(id,name)values(2,'emily');

Query OK, 1 row affected (1.00 sec)

 

[email protected]:3306 [evelyn]>select * from evelyn;

+------+--------+

| id   | name   |

+------+--------+

|    1 | evelyn |

|    2 | emily  |

+------+--------+

2 rows in set (0.00 sec)

## target端

[email protected]:3306 [evelyn]>select * from evelyn;

+------+--------+

| id   | name   |

+------+--------+

|    1 | evelyn |

|    2 | emily  |

+------+--------+

2 rows in set (0.00 sec)

1.2.7  監控

[email protected]:3306 [evelyn]>

select * from performance_schema.replication_connection_status\G;

*************************** 1. row ***************************

             CHANNEL_NAME: repl23

               GROUP_NAME:

              SOURCE_UUID: 7937ac78-3c39-11e7-b59e-005056b5d25f

                THREAD_ID: 71

            SERVICE_STATE: ON

COUNT_RECEIVED_HEARTBEATS: 78

 LAST_HEARTBEAT_TIMESTAMP: 2017-05-19 17:37:05

 RECEIVED_TRANSACTION_SET: 7937ac78-3c39-11e7-b59e-005056b5d25f:5-13

        LAST_ERROR_NUMBER: 0

       LAST_ERROR_MESSAGE:

     LAST_ERROR_TIMESTAMP: 0000-00-00 00:00:00

*************************** 2. row ***************************

             CHANNEL_NAME: repl24

               GROUP_NAME:

              SOURCE_UUID: bd783f44-258f-11e7-914b-005056b5d312

                THREAD_ID: 73

            SERVICE_STATE: ON

COUNT_RECEIVED_HEARTBEATS: 13

 LAST_HEARTBEAT_TIMESTAMP: 2017-05-19 17:37:09

 RECEIVED_TRANSACTION_SET: bd783f44-258f-11e7-914b-005056b5d312:28072-28090

        LAST_ERROR_NUMBER: 0

       LAST_ERROR_MESSAGE:

     LAST_ERROR_TIMESTAMP: 0000-00-00 00:00:00

2 rows in set (0.00 sec)

 

ERROR:

No query specified

 

[email protected]:3306 [emily]>

select * from performance_schema.replication_connection_configuration\G;

*************************** 1. row ***************************

                 CHANNEL_NAME: repl23

                         HOST: 192.168.102.23

                         PORT: 3306

                         USER: repl23

            NETWORK_INTERFACE:

                AUTO_POSITION: 1

                  SSL_ALLOWED: NO

                  SSL_CA_FILE:

                  SSL_CA_PATH:

              SSL_CERTIFICATE:

                   SSL_CIPHER:

                      SSL_KEY:

SSL_VERIFY_SERVER_CERTIFICATE: NO

                 SSL_CRL_FILE:

                 SSL_CRL_PATH:

    CONNECTION_RETRY_INTERVAL: 60

       CONNECTION_RETRY_COUNT: 86400

           HEARTBEAT_INTERVAL: 30.000

                  TLS_VERSION:

*************************** 2. row ***************************

                 CHANNEL_NAME: repl24

                         HOST: 192.168.102.24

                         PORT: 3306

                         USER: repl24

            NETWORK_INTERFACE:

                AUTO_POSITION: 1

                  SSL_ALLOWED: NO

                  SSL_CA_FILE:

                  SSL_CA_PATH:

              SSL_CERTIFICATE:

                   SSL_CIPHER:

                      SSL_KEY:

SSL_VERIFY_SERVER_CERTIFICATE: NO

                 SSL_CRL_FILE:

                 SSL_CRL_PATH:

    CONNECTION_RETRY_INTERVAL: 60

       CONNECTION_RETRY_COUNT: 86400

           HEARTBEAT_INTERVAL: 30.000

                  TLS_VERSION:

2 rows in set (0.00 sec)

 

ERROR:

No query specified

 

1.2.8  跳過事務

##GTID

STOP SLAVE FOR CHANNEL ‘CHANNEL_NAME’;

SET SESSION GTID_NEXT=’’;

BEGIN;COMMIT;

SET SESSION GTID_NEXT=’AUTOMATIC’;

START SLAVE FOR CHANNEL ‘CHANNEL_NAME’;

## binlog+position

stop slave sql_thread FOR CHANNEL ‘CHANNEL_NAME’;;

set global sql_slave_skip_counter=1;

start slave sql_thread FOR CHANNEL ‘CHANNEL_NAME’;;

 

 

##

[email protected]:3306 [(none)]>set session gtid_next='bd783f44-258f-11e7-914b-005056b5d312:28083';

Query OK, 0 rows affected (0.00 sec)

 

[email protected]:3306 [(none)]>begin;commit;

Query OK, 0 rows affected (0.00 sec)

 

[email protected]:3306 [(none)]>set session gtid_next=automatic;

Query OK, 0 rows affected (0.00 sec)

 

[email protected]:3306 [(none)]>start slave for channel 'repl24';

Query OK, 0 rows affected (0.00 sec)

 

1.2.9  遇見錯誤

1、  Last_IO_Error: Relay log write failure: could not queue event from master  ##repl24

Last_IO_Error: Fatal error: Failed to run 'after_read_event' hook          ##repl23

stop slave

start slave

最後發現是開啟一個源開啟了半同步複製,一個源沒有開題半同步複製。

2、  清除slave信息

reset slave

## 創建多源複製過程中,發現有一個slave沒有channel_name,使用如下語句清除slave信息

reset slave all for channel '';

3、  [email protected]:3306 [evelyn]>uninstall plugin rpl_semi_sync_master;

Query OK, 0 rows affected (0.01 sec)

[email protected]:3306 [evelyn]>uninstall plugin rpl_semi_sync_slave;

Query OK, 0 rows affected (0.00 sec)

4、   

 

參考資料:

http://www.cnblogs.com/xuanzhi201111/p/5151666.html

http://dev.mysql.com/doc/refman/5.7/en/change-master-to.html

http://www.longlong.asia/2015/10/21/mysql57-new-features.html


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 0x00 背景 這兩天處於轉牛角尖的狀態,非常不好。但是上一篇的中提到的問題總算是總結了些東西。 傳送門:疑問點0x02(4) 0x01 測試過程 (1)測試環境情況:創建瞭如下測試表test, mysql> select * from test;+ + + +| user_id | user | ...
  • 【1. 問題描述】 【2. 查找原因】 【3. 解決問題】 本文網址[tom-and-jerry發佈於2017-05-20 18:46] http://www.cnblogs.com/tom-and-jerry/p/6882857.html ...
  • 如何用VBA操作MySQL資料庫?如何直接使用Excel操作MySQL資料庫? ...
  • 本文介紹一個簡單的SQL腳本,實現收縮整個Microsoft SQL Server實例所有非系統DB文件大小的功能。 作為一個與SQL天天打交道的程式猿,經常會遇到DB文件太大,把空間占滿的情況: 而對於開發測試人員來說,如果DB數據不是特別重要的話,不會特意擴大磁碟空間,而是直接利用SQL的Shr ...
  • 2016年5月25日上午,由貴州省政府舉辦的以"大數據開啟智能時代"為主題的中國大數據產業峰會在貴陽開幕,國務院總理李克強發表主旨演講。騰訊集團馬化騰,高通公司總裁Derek Aberle,百度公司李彥巨集,微軟全球陸奇,京東集團劉強東,阿裡巴巴王堅,滴滴出行程維,HTC王雪紅,戴爾Michael D ...
  • 目錄結構: // <![CDATA[ function shocon(){ document.getElementById("modol").style.display="block"; } function clocon(){ document.getElementById("modol").st ...
  • 專職做DBA已經6年多的事件了,看同行、同事犯了太多的錯誤,自己也犯了非常多的錯誤。一路走來,感觸非常深。然而絕大多數的錯誤其實都是很低級的錯誤。有的是因為不瞭解某個引擎的特性導致;有的是因為對線上環境不瞭解導致;有的是因為經驗不足導致;一路上,跌跌撞撞,從小公司DBA,到騰訊高級DBA,再到現在的... ...
  • 下麵一步一步介紹一下如何在Red Hat Enterprise Linux系統上為SQL Server配置共用磁碟集群(Shared Disk Cluster)及其相關使用(僅供測試學習之用,基礎篇) 一. 創建共用磁碟和 Cluster 微軟官方配置文檔:https://docs.microsof ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...