zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

mysql 同步机制概念

2023-09-11 14:18:40 时间


https://mariadb.com/kb/en/relay-log/#:~:text=The%20relay%20log%20is%20a%20set%20of%20log,containing%20a%20list%20of%20all%20relay%20log%20files.
https://dev.mysql.com/doc/refman/8.0/en/replication-administration-status.html

1.中继日志概念:

The relay log is a set of log files created by a replica during replication.

It's the same format as the binary log, containing a record of events that affect the data or structure; thus, mysqlbinlog can be used to display its contents. It consists of a set of relay log files and an index file containing a list of all relay log files.

Events are read from the primary's binary log and written to the replica's relay log. They are then performed on the replica. Old relay log files are automatically removed once they are no longer needed.


中继日志是复制过程中副本创建的一组日志文件。它的格式与二进制日志相同,包含影响数据或结构的事件记录;
因此,mysqlbinlog可以用来显示其内容。它由一组中继日志文件和一个包含所有中继日志文件列表的索引文件组成。

事件从主服务器的二进制日志中读取,并写入副本的中继日志。然后在复制线程执行这些操作。旧的中继日志文件在不再需要时会自动删除。

 

2.Creating Relay Log Files
New relay log files are created by the replica at the following times:

when the IO thread starts
when the logs are flushed, with FLUSH LOGS or mysqladmin flush-logs.
when the maximum size, determined by the max_relay_log_size system variable, has been reached

创建中继日志文件复制副本在以下时间创建新的中继日志文件:
当IO线程启动时,
刷新日志,使用刷新日志或mysqladmin刷新日志。
当达到由max_relay_log_size系统变量确定的最大尺寸时


3.
Relay Log Names
By default, the relay log will be given a name host_name-relay-bin.nnnnnn, with host_name referring to the server's host name, and #nnnnnn the sequence number.

This will cause problems if the replica's host name changes, returning the error Failed to open the relay log and Could not find target log during relay log initialization. To prevent this, you can specify the relay log file name by setting the relay_log and relay_log_index system variables.

If you need to overcome this issue while replication is already underway,you can stop the replica, prepend the old relay log index file to the new relay log index file, and restart the replica.

For example:

shell> cat NEW_relay_log_name.index >> OLD_relay_log_name.index
shell> mv NEW_relay_log_name.index OLD_relay_log_name.index

中继日志名称默认情况下,中继日志将被命名为host_name-Relay-bin。nnnnnn,主机名指服务器的主机名,
#nnnnnn指序列号。如果复制副本的主机名发生更改,返回错误:无法打开中继日志,并且在中继日志初始化期间找不到目标日志
。为了防止出现这种情况,可以通过设置relay_log和relay_log_index系统变量来指定中继日志文件名。如果
需要在复制正在进行时解决此问题,可以停止复制副本,将旧的中继日志索引文件前置到新的中继日志索引文件,然后重新启动复制副本

4.
Viewing Relay Logs
The SHOW RELAYLOG EVENTS shows events in the relay log, and, since relay log files are the same format as binary log files, they can be read with the mysqlbinlog utility.

Removing Old Relay Logs
Old relay logs are automatically removed once all events have been implemented on the replica, and the relay log file is no longer needed. This behavior can be changed by adjusting the relay_log_purge system variable from its default of 1 to 0, in which case the relay logs will be left on the server. One can also flush the logs with the FLUSH RELAY LOGS commands.

If the relay logs are taking up too much space on the replica, the relay_log_space_limit system variable can be set to limit the size. The IO thread will stop until the SQL thread has cleared the backlog. By default there is no limit.

查看中继日志SHOW RELAYLOG EVENTS在中继日志中显示事件,由于中继日志文件的格式与二进制日志文件相同,因
此可以使用mysqlbinlog实用程序读取它们。

删除旧的中继日志一旦在复制副本上实现了所有事件,旧的中继日志将自动删除,并且不再需要中继日志文件。可以通过将中继日志清除系统变量从默认值1调整为0来更改此行为,
在这种情况下,中继日志将保留在服务器上。还可以使用flush RELAY logs命令刷新日志。
如果中继日志在副本上占用了太多空间,可以设置中继日志空间限制系统变量来限制大小。IO线程将停止,直到SQL线程清除积压replay log 工作。
默认情况下没有限制


5.几个指标:
(Relay_Master_Log_File, Exec_Master_Log_Pos): Coordinates in the source binary log indicating how far the replication SQL (applier) thread has executed events received from that log.

源二进制日志中的坐标,指示复制SQL(applier)线程执行从该日志接收的事件的距离。


(Master_Log_file, Read_Master_Log_Pos): Coordinates in the source binary log indicating how far the replication I/O (receiver) thread has read events from that log.

源二进制日志中的坐标,指示复制I/O(接收器)线程从该日志读取事件的距离。