複製(Replication):通過複製執行過的語句或者資料集從主伺服器上複製到一個或多個從伺服器上實現多個資料庫伺服器之間資料庫的同步。
MySQL's built-in replication capability is the foundation for building large, highperformance applications on top of MySQL.
MySQL supports two kinds of replication: statement-based replication(基於語句的複製) and row-based replication(基於行的複製).
Statement-based (or "logical") replication has been available since MySQL 3.23, and it's what most people are using in production today
Row-based replication is new in MySQL 5.1.
Both kinds work by recording changes in the master's binary log and replaying the log on the slave
基於語句的複製:記錄改變資料庫的語句,將語句在從伺服器上在執行一遍,效率較高。
基於行的複製:將語句執行後的結果包括行的改變或者添加整個複製到從伺服器中去。
混合方式的複製:由MySQL自動來判斷。
複製過程:
主伺服器上線程:mysql dump
從伺服器兩個線程:I/O thread ,SQL thread。
過程:從伺服器的I/O 線程(主伺服器的遠程用戶端)不斷嘗試串連主伺服器,讀取其二進位日誌,主伺服器收到請求後將檢查自己的Binary Log並根據從伺服器發來的Relay Log的相關資訊來確認自從上次複製之後主伺服器內容是否有更新,如果有,則主伺服器啟動mysql dump線程,將對方所請求的資料返回給從伺服器,從伺服器收到資料後會將資料儲存在Relay Log。SQL thread 會不週期性讀取Relay Log,如果發現有更新,則讀取更新的語句或者行將其儲存在從伺服器上。
MySQL解決的問題:資料備份、負載平衡、高可用、資料分布(異地容災)、升級測試。
下面我們來實現基於MySQL主從複製的架構:
Master:192.168.1.11 MySQL已安裝完畢
[root@station39 ~]# vim /etc/my.cnf
log-bin=master-bin //** update line 50
log-bin-index=master-bin.index //** add line 51
server-id = 1 //** line 59