17.2.1 Replication Implementation Details 复制实施细节

17.2.1 Replication Implementation Details 复制实施细节

MySQL 复制能力实现使用3个threads,一个在master上,另外2个在slave上:

Binlog dump thread, master创建一个thread 来发送binary log 内容到一个slave,当slave连接的时候。

这个线程可以在SHOW PROCESSLIST 数据中看到:

binary log dump thread 需要一个lock 在master 的binary log 用于读取要发送给slave的evnet.

当event被读取后,lock 释放, 甚至在event被发送给slave前。

Slave I/O thread, 当START SLAVE 语句在slave server上被执行,slave创建一个I/O thread,

slave连接到master,告诉master 发送它的binary log里的更新的内容

slave I/O 线程 读取 master的binlog dump thread 发送和复制它们到本地的文件,组成slave的relay log.

Slave SQL thread,slave 创建一个SQL thread 读取relay log ,是由slave I/O thread 写入。

在前面的描述中, 有三个线程 每个master/slave 连接。

一个master 有多个slave 创建一个binary log dump thread 为每个连接的slave,

每个slave 有它自己的I/O 和SQL threads.

一个slave 使用2个thread 来分别的从master 读取更新和,并执行它们成为单独的任务。

因此,如果读取语句的任务不会慢下来 如果执行语句是慢的。

例如, 如果slave server 有一段时间没有运行,它的I/O thread 可以很快的获取所有的binary log 内容从master,

当slave启动的时候, 即使SQL thread 远远的落后。

如果在SQL thread 已经执行所有获取的语句前停止, I/O thread 至少获取了一切,因此一份

安全的语句的拷贝被存储在slave的relay logs里,准备下次slave启动的时候执行。

SHOW PROCESSLIST 语句提供的信息告诉你 在master和slave上对于复制发生了什么。

下面的例子阐释三个thread 如何显示在输出的列表里:

在master server上,通过SHOW PROCESSLIST;

mysql> SHOW PROCESSLISTG
***************** 1. row *****************
Id: 2
User: root
Host: localhost:32931
db: NULL
Command: Binlog Dump
Time: 94
State: Has sent all binlog to slave; waiting for binlog to
be updated
Info: NULL

在这里 thread 2 是一个Binlog Dump 复制线程, 服务于slave连接。

状态信息表明所有的update 已经发送到slave,master 是在等待更多的更新发生。

如果你在master上没有看到Binlog Dump threads 在一个master上, 这意味着,复制没有运行,

也就是说没有slave连接。

在slave server上, SHOW PROCESSLIST显示如下:

mysql> SHOW PROCESSLISTG
***************** 1. row *****************
Id: 10
User: system user
Host:
db: NULL
Command: Connect
Time: 11
State: Waiting for master to send event
Info: NULL
***************** 2. row *****************
Id: 11
User: system user
Host:
db: NULL
Command: Connect
Time: 11
State: Has read all relay log; waiting for the slave I/O
thread to update it
Info: NULL

状态信息表明 thread 10 是I/O thread 是和Master 连接的, thread 11 是SQL thread

处理更新存储在relay logs里的内容.

原文地址:https://www.cnblogs.com/hzcya1995/p/13351316.html