8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

正文为中文,英文原文默认隐藏。点击任意中文段落即可展开 / 收起对应英文原文。

打开官方英文原文 ↗

译文由机器翻译生成,仅供理解参考;有疑问请以官方英文原文为准。

MySQL 8.4 参考手册 / 9 备份与恢复 / 9.5 时间点(增量)恢复 / 9.5.1 使用二进制日志进行时间点恢复
中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 100%

9.5.1 使用二进制日志的时间点恢复9.5.1 Point-in-Time Recovery Using Binary Log

本节解释了使用二进制日志执行时间点恢复的一般概念。下一节,第 9.5.2 节,“使用事件位置的时间点恢复”,通过一个示例详细说明了该操作。This section explains the general idea of using the binary log to perform a point-in-time-recovery. The next section, Section 9.5.2, “Point-in-Time Recovery Using Event Positions”, explains the operation in details with an example.

Note

本节及下一节的许多示例使用 mysql 客户端来处理由 mysqlbinlog 生成的二进制日志输出。如果您的二进制日志包含 \0(空)字符,则该输出无法被 mysql 解析,除非您使用 --binary-mode 选项调用它。Many of the examples in this and the next section use the mysql client to process binary log output produced by mysqlbinlog. If your binary log contains \0 (null) characters, that output cannot be parsed by mysql unless you invoke it with the --binary-mode option.

用于时间点恢复的信息来源是完全备份操作后生成的一组二进制日志文件。因此,为了允许服务器恢复到某个时间点,必须在服务器上启用二进制日志记录,这在 MySQL 8.4 中是默认设置(参见 第 7.4.4 节,“二进制日志”)。The source of information for point-in-time recovery is the set of binary log files generated subsequent to the full backup operation. Therefore, to allow a server to be restored to a point-in-time, binary logging must be enabled on it, which is the default setting for MySQL 8.4 (see Section 7.4.4, “The Binary Log”).

要从二进制日志恢复数据,您必须知道当前二进制日志文件的名称和位置。默认情况下,服务器在数据目录中创建二进制日志文件,但可以使用 --log-bin 选项指定路径,将文件放在不同的位置。要查看所有二进制日志文件的列表,请使用如下语句:To restore data from the binary log, you must know the name and location of the current binary log files. By default, the server creates binary log files in the data directory, but a path name can be specified with the --log-bin option to place the files in a different location. To see a listing of all binary log files, use this statement:

mysql> SHOW BINARY LOGS;

要确定当前二进制日志文件的名称,请执行以下语句:To determine the name of the current binary log file, issue the following statement:

mysql> SHOW BINARY LOG STATUS;

mysqlbinlog 工具将二进制日志文件中的事件从二进制格式转换为文本,以便可以查看或应用。mysqlbinlog 具有根据事件时间或日志中事件位置选择二进制日志部分的选项。请参阅 第 6.6.9 节,“mysqlbinlog — 用于处理二进制日志文件的工具”。The mysqlbinlog utility converts the events in the binary log files from binary format to text so that they can be viewed or applied. mysqlbinlog has options for selecting sections of the binary log based on event times or position of events within the log. See Section 6.6.9, “mysqlbinlog — Utility for Processing Binary Log Files”.

应用二进制日志中的事件会导致它们所表示的数据修改被重新执行。这使得可以恢复特定时间范围内的数据更改。要应用二进制日志中的事件,请使用 mysqlbinlog 输出,并使用 mysql 客户端处理:Applying events from the binary log causes the data modifications they represent to be reexecuted. This enables recovery of data changes for a given span of time. To apply events from the binary log, process mysqlbinlog output using the mysql client:

$> mysqlbinlog binlog_files | mysql -u root -p

如果二进制日志文件已被加密,mysqlbinlog 不能像前面的例子那样直接读取它们,但可以使用 --read-from-remote-server (-R) 选项从服务器读取它们。例如:If binary log files have been encrypted, mysqlbinlog cannot read them directly as in the previous example, but can read them from the server using the --read-from-remote-server (-R) option. For example:

$> mysqlbinlog --read-from-remote-server --host=host_name --port=3306  --user=root --password --ssl-mode=required  binlog_files | mysql -u root -p

这里使用了选项 --ssl-mode=required 来确保二进制日志文件的数据在传输过程中受到保护,因为它以未加密的格式发送到 mysqlbinlog。Here, the option --ssl-mode=required has been used to ensure that the data from the binary log files is protected in transit, because it is sent to mysqlbinlog in an unencrypted format.

Important

VERIFY_CA 和 VERIFY_IDENTITY 比 REQUIRED 更适合作为 SSL 模式的选择,因为它们有助于防止中间人攻击。要实现其中一个设置,你必须首先确保服务器的 CA 证书在你环境中使用它的所有客户端上都能可靠获取,否则会导致可用性问题。参见 加密连接的命令选项。VERIFY_CA and VERIFY_IDENTITY are better choices than REQUIRED for the SSL mode, because they help prevent man-in-the-middle attacks. To implement one of these settings, you must first ensure that the CA certificate for the server is reliably available to all the clients that use it in your environment, otherwise availability issues will result. See Command Options for Encrypted Connections.

查看日志内容在您需要确定事件时间或位置以选择在执行事件之前的部分日志内容时非常有用。要查看日志中的事件,可以将 mysqlbinlog 输出发送到分页程序中:Viewing log contents can be useful when you need to determine event times or positions to select partial log contents prior to executing events. To view events from the log, send mysqlbinlog output into a paging program:

$> mysqlbinlog binlog_files | more

或者,将输出保存到文件中,然后在文本编辑器中查看该文件:Alternatively, save the output in a file and view the file in a text editor:

$> mysqlbinlog binlog_files > tmpfile
$> ... edit tmpfile ...

编辑文件后,按如下方式应用内容:After editing the file, apply the contents as follows:

$> mysql -u root -p < tmpfile

如果你有多个二进制日志需要在 MySQL 服务器上应用,请使用单个连接来应用你想处理的所有二进制日志文件的内容。下面是一种方法:If you have more than one binary log to apply on the MySQL server, use a single connection to apply the contents of all binary log files that you want to process. Here is one way to do so:

$> mysqlbinlog binlog.000001 binlog.000002 | mysql -u root -p

另一种方法是将整个日志写入一个文件,然后处理该文件:Another approach is to write the whole log to a single file and then process the file:

$> mysqlbinlog binlog.000001 >  /tmp/statements.sql
$> mysqlbinlog binlog.000002 >> /tmp/statements.sql
$> mysql -u root -p -e "source /tmp/statements.sql"