復(fù)制是將主數(shù)據(jù)庫(kù)的DDL和DML操作通過(guò)二進(jìn)制日志傳到從庫(kù)上,然后再?gòu)膸?kù)重做,從而使得從庫(kù)和主庫(kù)保持?jǐn)?shù)據(jù)的同步。MySQL可以從一臺(tái)主庫(kù)同時(shí)向多臺(tái)從庫(kù)進(jìn)行復(fù)制,從庫(kù)同時(shí)也可以作為其他從庫(kù)的主庫(kù),實(shí)現(xiàn)鏈?zhǔn)綇?fù)制。
MySQL復(fù)制的優(yōu)點(diǎn):
- 主庫(kù)故障,可以快速切換至從庫(kù)提供服務(wù);
- 在從庫(kù)執(zhí)行查詢操作,降低主庫(kù)的訪問(wèn)壓力;
- 在從庫(kù)執(zhí)行備份,避免備份期間對(duì)主庫(kù)影響;
MySQL復(fù)制原理
1、MySQL主庫(kù)在事務(wù)提交時(shí)會(huì)把數(shù)據(jù)變更作為事件Events記錄在Binlog中,主庫(kù)上的sync_binlog參數(shù)控制Binlog日志刷新到磁盤(pán);
2、主庫(kù)推送Binlog中的事件到從庫(kù)的Relay Log,之后從庫(kù)根據(jù)Relay Log進(jìn)行重做,通過(guò)邏輯復(fù)制來(lái)達(dá)到主從庫(kù)的數(shù)據(jù)一致;
MySQL通過(guò)3個(gè)線程來(lái)完成主從庫(kù)間的數(shù)據(jù)復(fù)制:其中Binlog Dump線程運(yùn)行在主庫(kù)上,I/O線程和SQL線程運(yùn)行在從庫(kù)上。當(dāng)在從庫(kù)啟動(dòng)復(fù)制(Start Slave)時(shí),首先創(chuàng)建I/O線程連接主庫(kù),主庫(kù)隨后創(chuàng)建Binlog Dump線程讀取數(shù)據(jù)庫(kù)事件并發(fā)送給I/O線程,I/O線程獲取到事件數(shù)據(jù)后更新到從庫(kù)的Relay Log中,之后從庫(kù)上的SQL線程讀取Relay Log中更新的數(shù)據(jù)庫(kù)事件并應(yīng)用,
如下圖所示:
![](/d/20211018/e9343e1f1e4c121ac15a9ec10d2687d6.gif)
查看主庫(kù):
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 --由此可見(jiàn),以“推送”的方式同步
Info: NULL
4 rows in set (0.00 sec)
ERROR:
No query specified
查看備庫(kù):
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
由此可見(jiàn),MySQL復(fù)制是異步的,從庫(kù)和主庫(kù)存在一定的延時(shí)。
復(fù)制相關(guān)的日志
1、BinlogBinlog會(huì)記錄mysql中所有的數(shù)據(jù)修改操作,可以通過(guò)如下方式查看Binlog的格式,對(duì)應(yīng)有三種,分別為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的文件格式、內(nèi)容和Binlog一樣,唯一區(qū)別是從庫(kù)上的SQL線程執(zhí)行完當(dāng)前Relay Log中的事件后,SQL線程會(huì)自動(dòng)刪除該Relay Log,從而釋放空間。為保證從庫(kù)Crash重啟后,從庫(kù)的I/O線程和SQL線程仍能知道從哪里開(kāi)始復(fù)制,從庫(kù)默認(rèn)會(huì)創(chuàng)建兩個(gè)日志文件master.info和relay-log.info來(lái)保存復(fù)制的進(jìn)度,這兩個(gè)文件分別記錄了從庫(kù)的I/O線程當(dāng)前讀取主庫(kù)Binlog的進(jìn)度和SQL線程應(yīng)用Relay Log的進(jìn)度。
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.24.33.186 --主庫(kù)IP
Master_User: repl --主庫(kù)用于主從復(fù)制的用戶賬號(hào)
Master_Port: 3306 --主庫(kù)端口
Connect_Retry: 60
Master_Log_File: mysql-bin.000005 --從庫(kù)I/O線程當(dāng)前讀取主庫(kù)Binlog文件名
Read_Master_Log_Pos: 4356 --從庫(kù)I/O線程讀取主庫(kù)Binlog的位置
Relay_Log_File: strong-relay-bin.000006 --SQL線程正在應(yīng)用的Relay Log
Relay_Log_Pos: 320 --Relay Log的位置
Relay_Master_Log_File: mysql-bin.000005 --Relay Log對(duì)應(yīng)的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線程正在應(yīng)用Relay Log的位置對(duì)應(yīng)的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復(fù)制方式
Binlog的格式有三種,分別對(duì)應(yīng)了MySQL復(fù)制的3種技術(shù)。
MySQL復(fù)制架構(gòu)
MySQL復(fù)制的常見(jiàn)架構(gòu)有一主多從復(fù)制架構(gòu)、多級(jí)復(fù)制架構(gòu)和雙主復(fù)制(Dual Master)架構(gòu)。
1、一主多從架構(gòu)在主庫(kù)讀請(qǐng)求壓力非常大的場(chǎng)景下,通過(guò)配置一主多從復(fù)制架構(gòu)實(shí)現(xiàn)讀寫(xiě)分離,把對(duì)實(shí)時(shí)性要求不是特別高的讀取請(qǐng)求通過(guò)負(fù)載均衡分布到多個(gè)從庫(kù)上,從而降低主庫(kù)的讀取壓力,如圖:
![](/d/20211018/08db5eed9fef75058830e3ebeb50b962.gif)
2、多級(jí)復(fù)制架構(gòu)一主多從架構(gòu)能解決大部分讀請(qǐng)求壓力特別大的場(chǎng)景的需求,由于MySQL的復(fù)制是主庫(kù)推送Binlog到從庫(kù),主庫(kù)的I/O壓力和網(wǎng)絡(luò)壓力會(huì)隨著從庫(kù)的增加而增加(每個(gè)從庫(kù)都會(huì)在主庫(kù)上有一個(gè)獨(dú)立的Binlog Dump線程來(lái)發(fā)送Binlog事件),而多級(jí)復(fù)制架構(gòu)解決了一主多從場(chǎng)景下,主庫(kù)額外的I/O和網(wǎng)絡(luò)壓力的場(chǎng)景,如圖:
3、雙主復(fù)制/Dual Master架構(gòu)雙主復(fù)制/Dual Master架構(gòu)特別適合于DBA做維護(hù)需要主從切換的場(chǎng)景,通過(guò)該架構(gòu)避免了重復(fù)搭建從庫(kù)的麻煩,如圖:
![](/d/20211018/3192352a3442e4b9695cd6c4f24b9481.gif)
您可能感興趣的文章:- 分析MySQL復(fù)制以及調(diào)優(yōu)原理和方法
- Linux下MySQL數(shù)據(jù)庫(kù)的主從同步復(fù)制配置
- 詳解Docker方式實(shí)現(xiàn)MySql 主從復(fù)制(實(shí)踐篇)
- Mysql中復(fù)制詳細(xì)解析
- MySQL高可用解決方案MMM(mysql多主復(fù)制管理器)
- MySQL5.7.18主從復(fù)制搭建(一主一從)教程詳解
- Mysql5.7.18的安裝與主從復(fù)制圖文詳解
- 詳解MySQL實(shí)現(xiàn)主從復(fù)制過(guò)程
- 利用pt-heartbeat監(jiān)控MySQL的復(fù)制延遲詳解
- 詳解MySQL主從復(fù)制讀寫(xiě)分離搭建
- 詳解如何利用docker快速構(gòu)建MySQL主從復(fù)制環(huán)境
- 簡(jiǎn)單談?wù)凪ySQL的半同步復(fù)制