doc: 阅读mysql relay log文档

This commit is contained in:
wu xiangkai
2025-10-21 15:41:26 +08:00
parent 4cdefcd2bb
commit cca7672cbf

View File

@@ -15,6 +15,8 @@
- [Disadvantages of statement-based replication](#disadvantages-of-statement-based-replication) - [Disadvantages of statement-based replication](#disadvantages-of-statement-based-replication)
- [Advantages of row-based replication](#advantages-of-row-based-replication) - [Advantages of row-based replication](#advantages-of-row-based-replication)
- [disadvantages of row-based replication](#disadvantages-of-row-based-replication) - [disadvantages of row-based replication](#disadvantages-of-row-based-replication)
- [Replay Log and Replication Metadata Repositories](#replay-log-and-replication-metadata-repositories)
- [The Relay Log](#the-relay-log)
# Replication # Replication
Replication允许数据从一个database server被复制到一个或多个mysql database serversreplicas。replication默认是异步的replicas并无需永久连接即可接收来自source的更新。基于configuration可以针对所有的databases、选定的databases、甚至database中指定的tables进行replicate。 Replication允许数据从一个database server被复制到一个或多个mysql database serversreplicas。replication默认是异步的replicas并无需永久连接即可接收来自source的更新。基于configuration可以针对所有的databases、选定的databases、甚至database中指定的tables进行replicate。
@@ -321,3 +323,33 @@ mysql server中的logging format通过`binlog_format` system variable来进行
- 相比于statement-based replicationrow-based replication通常会向log中ieur更多数据特别是当statement操作大量行数据是 - 相比于statement-based replicationrow-based replication通常会向log中ieur更多数据特别是当statement操作大量行数据是
- 在replica并无法查看从source接收并执行的statements。可以通过`mysqlbinlog``--base64-output=DECODE-ROWS``--verbose`选项来查看数据变更 - 在replica并无法查看从source接收并执行的statements。可以通过`mysqlbinlog``--base64-output=DECODE-ROWS``--verbose`选项来查看数据变更
### Replay Log and Replication Metadata Repositories
replica server会创建一系列仓库其中存储在replication过程中使用的信息
- `relay log`: 该日志被replication I/O线程写入包含从source server的binary log读取的事务。relay log中记录的事务将会被replication SQL thread应用到replica
- `connection metadata repository`: 包含replication receiver thread连接到source server并从binary log获取事务时需要的信息。connection metadata repository会被写入到`mysql.slave_master_info`
- `applier metadata repository`: 包含replication applier thread从relay log读取并应用事务时所需要的信息。applier metadata repository会被写入到`mysql.slave_relay_log_info`表中
#### The Relay Log
relay log和binary log类似由一些`numbered files`构成,文件中包含`描述数据库变更的事件`。relay log还包含一个index file其中记录了所有被使用的relay log files的名称。默认情况下relay log files位于data directory中
relay log files拥有和binary log相同的格式也可以通过mysqlbinlog进行读取。如果使用了binary log transaction compression那么写入relay log的事务payloads也会按照和binary log相同的方式被压缩。
对于默认的replication channelrelay log file命名形式如下`host_name-relay-bin.nnnnnn`:
- `host_name`为replica server host的名称
- `nnnnnn`是序列号序列号从000001开始
对于非默认的replication channels默认的名称为`host_name-relay-bin-channel`
- `channel`为replciation channel的名称
replica会使用index file来追踪当前在使用的relay log files。默认relay log index file的名称为`host_name-relay-bin.index`
当如下条件下replica server将会创建新的relay log file
- 每次replication I/O thread开启时
- 当logs被刷新时例如当执行`FLUSH LOGS`命令时)
- 当当前relay log file的大小太大时大小上限可以通过如下方式决定
- 如果`max_relay_log_size`的大小大于0那么其就是relay log file的最大大小
- 如果`max_relay_log_size`为0那么relay log file的最大大小由`max_binlog_size`决定
replication sql thread在其执行完文件中所有events之后都不再需要该文件被执行完的relay log file都会被自动删除。目前没有显式的机制来删除relay logsrelay log的删除由replciation sql thread来处理。但是`FLUSH LOGS`可以针对relay logs进行rotation。