MySQL主從複製----同步與非同步配置

來源:互聯網
上載者:User

標籤:資料庫   半同步   同步   mysql   

簡單來講MySQL的主從複製就是一個C/S架構的應用。master可以認為是我們通常意義上所認為的server,slave可以當作是一台client。slave上的I/O線程去請求master上資料,而master驗證通過slave的資訊後就允許slave接入,然後進行資料變化資訊的發送。

一、MySQL主從複製原理

這裡我以MySQL5.5為例來說一下MySQL的主從複製的原理:

650) this.width=650;" src="http://7xaxaa.com1.z0.glb.clouddn.com/MySQL%E4%B8%BB%E4%BB%8E%E5%A4%8D%E5%88%B6%E5%8E%9F%E7%90%86.jpg" width="357" height="306" alt="MySQL主從複製原理.jpg" />

    首先由備節點的I/O線程負責向主節點請求資料,主節點驗證通過以後會由dump線程把資料發送給備用節點。備用節點的I/O線程收到資源後會把把這些資料寫入到中繼日誌,備節點的SQL線程檢測到中繼日誌變更後會立刻根據中繼日誌的內容跟新備庫的內容。這樣就完成了同步的過程。

    二、常見的複製模型

1、一主一從模型

650) this.width=650;" src="http://7xaxaa.com1.z0.glb.clouddn.com/1.jpg" width="285" height="133" alt="1.jpg" />

這種架構的優點就是比較簡單,搭建和維護都比較容易,成本也比較低。對於一些負載量不是特別大、可靠性要求不是特別高的場合,完全可以採用這種模型。但是對於一些負載比較大網站,和對可用性要求比較高的場合,這種架構就不太適用了。因為如果訪問量比較大,Master節點的壓力會比較的,另外如果Master崩潰,也會導致業務的終止。

2、一主多從模型

650) this.width=650;" src="http://7xaxaa.com1.z0.glb.clouddn.com/2.jpg" width="395" height="314" alt="2.jpg" />

       在絕大多數情境中,我們的應用都是讀多寫。我們使用這種架構,通過讀寫分離的技術,可以有效降低Master上讀的壓力。我們在後端的slave上可以做一些資料備份,資料採礦等方面的工作。但是如果備庫比較多,同時主庫又要負責其他的請求時,主庫的壓力會明顯增大,此時主庫會成為整個系統的效能瓶頸。

 

    當然,還有其他的複製模型,比如多級中繼,和環狀複製等,這些複製的基本原理都和上面的差不多,這裡不再詳細的解釋了。

二、配置主從複製

1、非同步複製

主從同步的條件:

Master:

       a:啟用二進位日誌;

       b:選擇一個server-id

       c:建立具有複製許可權的使用者

Slave:

       a:啟用中繼日誌

       b:選擇一個唯一的server-id

       c:串連主伺服器,並開始複製資料

1、首先在主庫上建立用於複製的最小許可權的使用者

mysql> grant replication slave,replication client on *.* to [email protected]‘10.12.%‘    -> identified by ‘123456‘;Query OK, 0 rows affected (0.03 sec)


2、在從庫上串連主庫

mysql> CHANGE MASTER TO MASTER_HOST=‘10.12.128.19‘,MASTER_PORT=3306,MASTER_USER=‘repl‘, MASTER_PASSWORD=‘123456‘,MASTER_LOG_FILE=‘mysql-bin.000006‘, MASTER_LOG_POS=451;
#查看複製的狀態mysql> show slave status\G*************************** 1. row ***************************               Slave_IO_State: Waiting for master to send event                  Master_Host: 10.12.128.19                  Master_User: repl                  Master_Port: 3306                Connect_Retry: 60              Master_Log_File: mysql-bin.000006          Read_Master_Log_Pos: 1512               Relay_Log_File: relay_index.000002                Relay_Log_Pos: 283        Relay_Master_Log_File: mysql-bin.000006             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: 1512              Relay_Log_Space: 452              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: 0Master_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: 3306                  Master_UUID: 97f33396-ed12-11e4-921a-000c29e8ee06             Master_Info_File: /mydata/data5.6/master.info                    SQL_Delay: 0          SQL_Remaining_Delay: NULL      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it           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: 01 row in set (0.00 sec)


3、然後再從庫上執行:

#啟動複製mysql> start slave;

    也可以單獨啟動IO線程和SQL線程。

(如果從庫的IO線程狀態一直是connecting的狀態,可能是防火牆的原因,一般來說關閉防火牆或者配置防火牆規則就可以了)

2、半同步複製

半同步複製是基於Google為MySQL開發的半同步複製的外掛程式。半同步複製的原理是,一個事務在主伺服器上執行完成後,必須至少確保至少在一台從伺服器上執行完成後,事務才算提交成功。如果在一定時間內從伺服器沒有響應,則會自動降級為非同步複製。

這個半同步複製是建立在非同步複製的基礎之上進行的。

首先需要安裝Google的半同步外掛程式: 

master:install plugin rpl_semi_master soname ‘semisync_master.so‘;slave:install plugin rpl_semi_sync_slave soname ‘semisync_slave.so‘;

然後開啟半同步的功能

master:set global rpl_semi_sync_master_enabled = 1;set global rpl_semi_sync_master_timeout = 100; //以毫秒計算
slave:set global rpl_semi_sync_slave = ON;

在從庫上還需要重啟IO線程:

stop slave IO_thread;start slave IO_thread;

分別在主庫和備庫上查看半同步外掛程式啟動並執行狀態:

mysql> show global status like ‘rpl%‘;+--------------------------------------------+-------+| Variable_name                              | Value |+--------------------------------------------+-------+| Rpl_semi_sync_master_clients               | 1     || Rpl_semi_sync_master_net_avg_wait_time     | 0     || Rpl_semi_sync_master_net_wait_time         | 0     || Rpl_semi_sync_master_net_waits             | 0     || Rpl_semi_sync_master_no_times              | 1     || Rpl_semi_sync_master_no_tx                 | 8     || Rpl_semi_sync_master_status                | ON    || Rpl_semi_sync_master_timefunc_failures     | 0     || Rpl_semi_sync_master_tx_avg_wait_time      | 0     || Rpl_semi_sync_master_tx_wait_time          | 0     || Rpl_semi_sync_master_tx_waits              | 0     || Rpl_semi_sync_master_wait_pos_backtraverse | 0     || Rpl_semi_sync_master_wait_sessions         | 0     || Rpl_semi_sync_master_yes_tx                | 0     |+--------------------------------------------+-------+14 rows in set (0.04 sec)
mysql> show global status like ‘rpl%‘;+----------------------------+-------+| Variable_name              | Value |+----------------------------+-------+| Rpl_semi_sync_slave_status | ON    |+----------------------------+-------+1 row in set (0.04 sec)

可以看到,主庫和備庫的半同步外掛程式都已經處於啟用狀態。

至此,非同步主從配置結束。


本文出自 “張志亮” 部落格,請務必保留此出處http://zhilight.blog.51cto.com/9880628/1639572

MySQL主從複製----同步與非同步配置

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.