复制之所以有效,是因为写入二进制日志的事件会从源读取,然后在副本上处理。根据事件类型,这些事件以不同的格式记录在二进制日志中。使用的不同复制格式对应于事件在源的二进制日志中记录时使用的二进制日志格式。二进制日志格式与复制过程中使用的术语之间的对应关系如下:Replication works because events written to the binary log are read from the source and then processed on the replica. The events are recorded within the binary log in different formats according to the type of event. The different replication formats used correspond to the binary logging format used when the events were recorded in the source's binary log. The correlation between binary logging formats and the terms used during replication are:
使用基于语句的二进制日志时,源将 SQL 语句写入二进制日志。将源复制到副本的过程是通过在副本上执行这些 SQL 语句来完成的。这称为 基于语句的复制(可以缩写为 SBR),对应于 MySQL 的基于语句的二进制日志格式。When using statement-based binary logging, the source writes SQL statements to the binary log. Replication of the source to the replica works by executing the SQL statements on the replica. This is called statement-based replication (which can be abbreviated as SBR), which corresponds to the MySQL statement-based binary logging format.
使用基于行的日志记录时,源会将 事件 写入二进制日志,这些事件指示单个表行的更改方式。源到副本的复制是通过将表示表行更改的事件复制到副本来实现的。这称为 基于行的复制(可缩写为 RBR)。When using row-based logging, the source writes events to the binary log that indicate how individual table rows are changed. Replication of the source to the replica works by copying the events representing the changes to the table rows to the replica. This is called row-based replication (which can be abbreviated as RBR).
基于行的日志记录是默认方法。Row-based logging is the default method.
您还可以配置 MySQL 使用基于语句和基于行的混合日志记录,这取决于哪种方式最适合记录更改。这称为混合格式日志记录。使用混合格式日志记录时,默认使用基于语句的日志。根据某些语句以及正在使用的存储引擎,在特定情况下日志会自动切换为基于行。使用混合格式进行复制被称为基于混合的复制或混合格式复制。有关更多信息,请参见7.4.4.3 节,“混合二进制日志格式”。You can also configure MySQL to use a mix of both statement-based and row-based logging, depending on which is most appropriate for the change to be logged. This is called mixed-format logging. When using mixed-format logging, a statement-based log is used by default. Depending on certain statements, and also the storage engine being used, the log is automatically switched to row-based in particular cases. Replication using the mixed format is referred to as mixed-based replication or mixed-format replication. For more information, see Section 7.4.4.3, “Mixed Binary Logging Format”.
NDB 集群。 MySQL NDB Cluster 8.4 的默认二进制日志格式是 ROW。NDB 集群复制使用基于行的复制;NDB 存储引擎与基于语句的复制不兼容。更多信息请参见 第 25.7.2 节,“NDB 集群复制的一般要求”。NDB Cluster.
The default binary logging format in MySQL NDB Cluster
8.4 is ROW. NDB Cluster
Replication uses row-based replication; that the
NDB storage engine is incompatible
with statement-based replication. See
Section 25.7.2, “General Requirements for NDB Cluster Replication”, for more
information.
当使用MIXED格式时,二进制日志格式部分由所使用的存储引擎和所执行的语句决定。有关混合格式日志记录及支持不同日志格式的规则的更多信息,请参见第 7.4.4.3 节,“混合二进制日志格式”。When using MIXED format, the binary logging
format is determined in part by the storage engine being used and
the statement being executed. For more information on mixed-format
logging and the rules governing the support of different logging
formats, see Section 7.4.4.3, “Mixed Binary Logging Format”.
正在运行的 MySQL 服务器中的日志记录格式由设置 binlog_format 服务器系统变量来控制。该变量可以以会话或全局范围设置。新设置何时以及如何生效的规则与其他 MySQL 服务器系统变量相同。为当前会话设置该变量仅在该会话结束前有效,并且该更改对其他会话不可见。全局设置该变量对更改后连接的客户端生效,但对当前任何客户端会话,包括更改该变量设置的那个会话,则不生效。要使全局系统变量设置永久生效,以便在服务器重启后仍然适用,必须在选项文件中进行设置。有关更多信息,请参见 第 15.7.6.1 节,“用于变量赋值的 SET 语法”。The logging format in a running MySQL server is controlled by
setting the binlog_format server
system variable. This variable can be set with session or global
scope. The rules governing when and how the new setting takes
effect are the same as for other MySQL server system variables.
Setting the variable for the current session lasts only until the
end of that session, and the change is not visible to other
sessions. Setting the variable globally takes effect for clients
that connect after the change, but not for any current client
sessions, including the session where the variable setting was
changed. To make the global system variable setting permanent so
that it applies across server restarts, you must set it in an
option file. For more information, see
Section 15.7.6.1, “SET Syntax for Variable Assignment”.
在某些情况下,您不能在运行时更改二进制日志格式,或者更改会导致复制失败。请参见 第 7.4.4.2 节,“设置二进制日志格式”。There are conditions under which you cannot change the binary logging format at runtime or doing so causes replication to fail. See Section 7.4.4.2, “Setting The Binary Log Format”.
更改全局 binlog_format 值需要具有设置全局系统变量的权限。更改会话 binlog_format 值需要具有设置受限会话系统变量的权限。请参见 第 7.1.9.1 节,“系统变量权限”。Changing the global binlog_format
value requires privileges sufficient to set global system
variables. Changing the session
binlog_format value requires
privileges sufficient to set restricted session system variables.
See Section 7.1.9.1, “System Variable Privileges”.
在 MySQL 8.0 中,修改二进制日志格式(binlog_format 系统变量)已被弃用;在 MySQL 的未来版本中,可以预期 binlog_format 将被完全移除,而基于行的格式将成为 MySQL 唯一使用的日志格式。Changing the binary logging format
(binlog_format system variable)
was deprecated in MySQL 8.0; in a future version of MySQL, you
can expect binlog_format to be removed
altogether, and the row-based format to become the only logging
format used by MySQL.
基于语句和基于行的复制格式存在不同的问题和限制。有关它们各自优缺点的比较,请参见 第 19.2.1.1 节,“基于语句和基于行复制的优缺点”。The statement-based and row-based replication formats have different issues and limitations. For a comparison of their relative advantages and disadvantages, see Section 19.2.1.1, “Advantages and Disadvantages of Statement-Based and Row-Based Replication”.
使用基于语句的复制时,您可能会遇到复制存储例程或触发器的问题。您可以通过改用基于行的复制来避免这些问题。有关更多信息,请参见 第 27.7 节,“存储程序二进制日志记录”。With statement-based replication, you may encounter issues with replicating stored routines or triggers. You can avoid these issues by using row-based replication instead. For more information, see Section 27.7, “Stored Program Binary Logging”.