MySQL:Replication 实现原理

之前介绍了主从搭建,具体参考 MySQL:主从复制(Replication)搭建 ,接下来简单介绍 MySQL 主从复制原理。

MySQL 主从复制基于 binary log,这个文件类似于 Oracle 的 redo 和 PostgreSQL WAL 日志文件,用来记录数据库的变化( update, insert, delete 等),但 SELECT 一般不记录到日志里。

MySQL 主从复制的三个线程

  1. Binlog dump thread
    这个线程位于主节点上,用来发送变化的 binary log 到备节点。

  2. Slave I/O thread
    这个线程位于备节点上, 作用是与主节点保持连接,并且接收主节点发送过来的变化的 binary log 到本地。

  3. Slave SQL thread
    这个线程位于备节点,作用是读取备节点的 relay log 并且在备节点上重做日志里的事件。

主节点查看: Binlog dump thread

1
2
3
4
5
6
7
8
9
10
11
root@localhost:francs>show processlistG  
* 2. row *
Id: 1493
User: rep1
Host: db2:20976
db: NULL
Command: Binlog Dump
Time: 1491
State: Master has sent all binlog to slave; waiting for binlog to be updated
Info: NULL
2 rows in set (0.00 sec)

备注:主节点状态信息具体查看:Replication Master Thread States

备节点查看: Slave I/O thread 和 Slave SQL thread

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
root@localhost:francs>show slave statusG  
* 1. row *
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.2.37
Master_User: rep1
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: bin-log.000038
Read_Master_Log_Pos: 604
Relay_Log_File: db2-relay-bin.000054
Relay_Log_Pos: 281
Relay_Master_Log_File: bin-log.000038
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: 604
Relay_Log_Space: 855
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: 0c130d47-22bb-11e4-aaaa-000c2986ac80
Master_Info_File: /database/mysql/data/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: 0
1 row in set (0.00 sec)

备注: 查看 Slave_IO_Running: Yes 和 Slave_SQL_Running: Yes 两项, yes 表示线程正常。Slave_IO_State 表示 I/O THREAD 状态,详细信息参考 Replication Slave I/O Thread States, Slave_SQL_Running_State 表示 SQL thread 状态,详细信息参考 Replication Slave SQL Thread States

备节点的 I/O, SQL 两个线程也通过 show processlist 查看

root@localhost:francs>show processlistG  
*  2. row *  
 Id:  7  
 User: system user  
 Host:    
 db: NULL  
Command:  Connect  
 Time:  2055  
 State:  Waiting  for master to send event  
 Info: NULL  
*  3. row *  
 Id:  8  
 User: system user  
 Host:    
 db: NULL  
Command:  Connect  
 Time:  2038  
 State:  Slave has read all relay log; waiting for the slave I/O thread to update it  
 Info: NULL  
3 rows in  set  (0.00 sec)

四 参考

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/239610.html

(0)
上一篇 2022年2月12日
下一篇 2022年2月12日

相关推荐

发表回复

登录后才能评论