1. 程式人生 > 資料庫 >MySQL複製優點、原理詳解

MySQL複製優點、原理詳解

複製是將主資料庫的DDL和DML操作通過二進位制日誌傳到從庫上,然後再從庫重做,從而使得從庫和主庫保持資料的同步。MySQL可以從一臺主庫同時向多臺從庫進行復制,從庫同時也可以作為其他從庫的主庫,實現鏈式複製。

MySQL複製的優點:

  • 主庫故障,可以快速切換至從庫提供服務;
  • 在從庫執行查詢操作,降低主庫的訪問壓力;
  • 在從庫執行備份,避免備份期間對主庫影響;

MySQL複製原理

1、MySQL主庫在事務提交時會把資料變更作為事件Events記錄在Binlog中,主庫上的sync_binlog引數控制Binlog日誌重新整理到磁碟;

2、主庫推送Binlog中的事件到從庫的Relay Log,之後從庫根據Relay Log進行重做,通過邏輯複製來達到主從庫的資料一致;

MySQL通過3個執行緒來完成主從庫間的資料複製:其中Binlog Dump執行緒執行在主庫上,I/O執行緒和SQL執行緒執行在從庫上。當在從庫啟動複製(Start Slave)時,首先建立I/O執行緒連線主庫,主庫隨後建立Binlog Dump執行緒讀取資料庫事件併發送給I/O執行緒,I/O執行緒獲取到事件資料後更新到從庫的Relay Log中,之後從庫上的SQL執行緒讀取Relay Log中更新的資料庫事件並應用,

如下圖所示:


檢視主庫:

mysql> show processlist\G; 
*************************** 1. row *************************** 
   Id: 3 
  User: root 
  Host: 10.24.33.187:54194 
   db: NULL 
Command: Sleep 
  Time: 176 
 State:  
  Info: NULL 
*************************** 2. row *************************** 
   Id: 4 
  User: root 
  Host: 10.24.33.187:54195 
   db: NULL 
Command: Sleep 
  Time: 176 
 State:  
  Info: NULL 
*************************** 3. row *************************** 
   Id: 8 
  User: root 
  Host: localhost 
   db: test 
Command: Query 
  Time: 0 
 State: starting 
  Info: show processlist 
*************************** 4. row *************************** 
   Id: 12 
  User: repl 
  Host: dsz884.hcg.homecredit.net:39731 
   db: NULL 
Command: Binlog Dump  --Binlog Dump執行緒 
  Time: 87 
 State: Master has sent all binlog to slave; waiting for more updates --由此可見,以“推送”的方式同步 
  Info: NULL 
4 rows in set (0.00 sec) 
 
ERROR:  
No query specified 

檢視備庫:

mysql> show processlist\G; 
*************************** 1. row *************************** 
   Id: 1 
  User: system user 
  Host:  
   db: NULL 
Command: Connect 
  Time: 4427 
 State: Waiting for master to send event 
  Info: NULL 
*************************** 2. row *************************** 
   Id: 2 
  User: system user 
  Host:  
   db: NULL 
Command: Connect 
  Time: 2044 
 State: Slave has read all relay log; waiting for more updates 
  Info: NULL 

由此可見,MySQL複製是非同步的,從庫和主庫存在一定的延時。

複製相關的日誌

1、BinlogBinlog會記錄mysql中所有的資料修改操作,可以通過如下方式檢視Binlog的格式,對應有三種,分別為Statement、Row和Mixed:

mysql> show variables like '%binlog_format%'; 
+---------------+-------+ 
| Variable_name | Value | 
+---------------+-------+ 
| binlog_format | ROW  | 
+---------------+-------+ 
1 row in set (0.00 sec) 

2、Relay LogRelay Log的檔案格式、內容和Binlog一樣,唯一區別是從庫上的SQL執行緒執行完當前Relay Log中的事件後,SQL執行緒會自動刪除該Relay Log,從而釋放空間。為保證從庫Crash重啟後,從庫的I/O執行緒和SQL執行緒仍能知道從哪裡開始複製,從庫預設會建立兩個日誌檔案master.info和relay-log.info來儲存複製的進度,這兩個檔案分別記錄了從庫的I/O執行緒當前讀取主庫Binlog的進度和SQL執行緒應用Relay Log的進度。

mysql> show slave status \G; 
*************************** 1. row *************************** 
        Slave_IO_State: Waiting for master to send event 
         Master_Host: 10.24.33.186 --主庫IP 
         Master_User: repl --主庫用於主從複製的使用者賬號 
         Master_Port: 3306 --主庫埠 
        Connect_Retry: 60  
       Master_Log_File: mysql-bin.000005 --從庫I/O執行緒當前讀取主庫Binlog檔名 
     Read_Master_Log_Pos: 4356 --從庫I/O執行緒讀取主庫Binlog的位置 
        Relay_Log_File: strong-relay-bin.000006 --SQL執行緒正在應用的Relay Log 
        Relay_Log_Pos: 320 --Relay Log的位置 
    Relay_Master_Log_File: mysql-bin.000005 --Relay Log對應的Binlog 
       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: 4356 --SQL執行緒正在應用Relay Log的位置對應的Binlog的位置 
       Relay_Log_Space: 1153 
       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 
         Master_UUID: 2a3e3fd9-0587-11e8-bdb8-0800272325a8 
       Master_Info_File: /usr/local/mysql-5.7.21-el7-x86_64/data/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_TLS_Version:  
1 row in set (0.00 sec) 
 
ERROR:  
No query specified 
 
mysql>

MySQL複製方式

Binlog的格式有三種,分別對應了MySQL複製的3種技術。

MySQL複製架構

MySQL複製的常見架構有一主多從複製架構、多級複製架構和雙主複製(Dual Master)架構。

1、一主多從架構在主庫讀請求壓力非常大的場景下,通過配置一主多從複製架構實現讀寫分離,把對實時性要求不是特別高的讀取請求通過負載均衡分佈到多個從庫上,從而降低主庫的讀取壓力,如圖:


2、多級複製架構一主多從架構能解決大部分讀請求壓力特別大的場景的需求,由於MySQL的複製是主庫推送Binlog到從庫,主庫的I/O壓力和網路壓力會隨著從庫的增加而增加(每個從庫都會在主庫上有一個獨立的Binlog Dump執行緒來發送Binlog事件),而多級複製架構解決了一主多從場景下,主庫額外的I/O和網路壓力的場景,如圖:

3、雙主複製/Dual Master架構雙主複製/Dual Master架構特別適合於DBA做維護需要主從切換的場景,通過該架構避免了重複搭建從庫的麻煩,如圖: