4.6.9.3 Using mysqlbinlog to Back Up Binary Log Files使用mysqlbinlog备份二进制日志文件

By default, mysqlbinlog reads binary log files and displays their contents in text format. 默认情况下,mysqlbinlog读取二进制日志文件并以文本格式显示其内容。This enables you to examine events within the files more easily and to re-execute them (for example, by using the output as input to mysql). 这使您能够更容易地检查文件中的事件并重新执行它们(例如,通过将输出用作mysql的输入)。mysqlbinlog can read log files directly from the local file system, or, with the --read-from-remote-server option, it can connect to a server and request binary log contents from that server. mysqlbinlog可以直接从本地文件系统读取日志文件,或者,使用--read-from-remote-server选项,它可以连接到服务器并从该服务器请求二进制日志内容。mysqlbinlog writes text output to its standard output, or to the file named as the value of the --result-file=file_name option if that option is given.mysqlbinlog将文本输出写入其标准输出,或者写入名为--result-file=file_name选项值的文件(如果给定该选项)。

mysqlbinlog Backup Capabilities备份功能

mysqlbinlog can read binary log files and write new files containing the same content—that is, in binary format rather than text format. This capability enables you to easily back up a binary log in its original format. mysqlbinlog可以读取二进制日志文件,并写入包含相同内容的新文件,即以二进制格式而不是文本格式。此功能使您能够轻松地以原始格式备份二进制日志。mysqlbinlog can make a static backup, backing up a set of log files and stopping when the end of the last file is reached. mysqlbinlog可以进行静态备份,备份一组日志文件,并在到达最后一个文件的末尾时停止。It can also make a continuous (live) backup, staying connected to the server when it reaches the end of the last log file and continuing to copy new events as they are generated. 它还可以进行连续(“实时”)备份,在服务器到达最后一个日志文件末尾时保持与服务器的连接,并在生成新事件时继续复制。In continuous-backup operation, mysqlbinlog runs until the connection ends (for example, when the server exits) or mysqlbinlog is forcibly terminated. 在连续备份操作中,mysqlbinlog一直运行到连接结束(例如,当服务器退出时)或mysqlbinlog被强制终止。When the connection ends, mysqlbinlog does not wait and retry the connection, unlike a replica server. 当连接结束时,mysqlbinlog不会等待并重试连接,这与副本服务器不同。To continue a live backup after the server has been restarted, you must also restart mysqlbinlog.要在服务器重新启动后继续实时备份,您还必须重新启动mysqlbinlog

Important重要

mysqlbinlog can back up both encrypted and unencrypted binary log files . However, copies of encrypted binary log files that are generated using mysqlbinlog are stored in an unencrypted format.mysqlbinlog可以备份加密和未加密的二进制日志文件。但是,使用mysqlbinlog生成的加密二进制日志文件的副本以未加密的格式存储。

mysqlbinlog Backup Options备份选项

Binary log backup requires that you invoke mysqlbinlog with two options at minimum:二进制日志备份要求您至少使用两个选项调用mysqlbinlog

Along with --read-from-remote-server, it is common to specify other options: --host indicates where the server is running, and you may also need to specify connection options such as --user and --password.除了--read-from-remote-server之外,通常还会指定其他选项:--host表示服务器在哪里运行,您可能还需要指定连接选项,如--user--password

Several other options are useful in conjunction with --raw:其他几个选项与--raw结合使用也很有用:

Static and Live Backups静态和实时备份

To back up a server's binary log files with mysqlbinlog, you must specify file names that actually exist on the server. 要使用mysqlbinlog备份服务器的二进制日志文件,您必须指定服务器上实际存在的文件名。If you do not know the names, connect to the server and use the SHOW BINARY LOGS statement to see the current names. Suppose that the statement produces this output:如果您不知道这些名称,请连接到服务器并使用SHOW BINARY LOGS语句查看当前名称。假设该语句产生以下输出:

mysql> SHOW BINARY LOGS;
+---------------+-----------+-----------+
| Log_name      | File_size | Encrypted |
+---------------+-----------+-----------+
| binlog.000130 |     27459 | No        |
| binlog.000131 |     13719 | No        |
| binlog.000132 |     43268 | No        |
+---------------+-----------+-----------+

With that information, you can use mysqlbinlog to back up the binary log to the current directory as follows (enter each command on a single line):有了这些信息,您可以使用mysqlbinlog将二进制日志备份到当前目录,如下所示(在一行中输入每个命令):

Output File Naming输出文件命名

Without --raw, mysqlbinlog produces text output and the --result-file option, if given, specifies the name of the single file to which all output is written. 如果没有--rawmysqlbinlog将生成文本输出,如果给出--result-file选项,则指定所有输出所写入的单个文件的名称。With --raw, mysqlbinlog writes one binary output file for each log file transferred from the server. 使用--rawmysqlbinlog为从服务器传输的每个日志文件写入一个二进制输出文件。By default, mysqlbinlog writes the files in the current directory with the same names as the original log files. To modify the output file names, use the --result-file option. In conjunction with --raw, the --result-file option value is treated as a prefix that modifies the output file names.默认情况下,mysqlbinlog在当前目录中写入与原始日志文件同名的文件。要修改输出文件名,请使用--result-file选项。与--raw一起,--result-file选项值被视为修改输出文件名的前缀。

Suppose that a server currently has binary log files named binlog.000999 and up. 假设服务器当前有名为binlog.000999及以上的二进制日志文件。If you use mysqlbinlog --raw to back up the files, the --result-file option produces output file names as shown in the following table. 如果使用mysqlbinlog --raw备份文件,--result-file选项将生成输出文件名,如下表所示。You can write the files to a specific directory by beginning the --result-file value with the directory path. 通过以目录路径开头--result文件值,可以将文件写入特定目录。If the --result-file value consists only of a directory name, the value must end with the pathname separator character. Output files are overwritten if they exist.如果--result-file值仅由目录名组成,则该值必须以路径名分隔符结尾。如果输出文件存在,则会被覆盖。

--result-file Option选项Output File Names输出文件名
--result-file=xxbinlog.000999 and up
--result-file=/tmp//tmp/binlog.000999 and up
--result-file=/tmp/x/tmp/xbinlog.000999 and up
Example: mysqldump + mysqlbinlog for Backup and Restore示例:mysqldump+mysqlbinlog用于备份和还原

The following example describes a simple scenario that shows how to use mysqldump and mysqlbinlog together to back up a server's data and binary log, and how to use the backup to restore the server if data loss occurs. 以下示例描述了一个简单的场景,展示了如何同时使用mysqldumpmysqlbinlog来备份服务器的数据和二进制日志,以及如何在发生数据丢失时使用备份来恢复服务器。The example assumes that the server is running on host host_name and its first binary log file is named binlog.000999. Enter each command on a single line.该示例假设服务器在主机host_name上运行,其第一个二进制日志文件名为binlog.00099。在一行中输入每个命令。

Use mysqlbinlog to make a continuous backup of the binary log:使用mysqlbinlog对二进制日志进行连续备份:

mysqlbinlog --read-from-remote-server --host=host_name --raw
  --stop-never binlog.000999

Use mysqldump to create a dump file as a snapshot of the server's data. 使用mysqldump创建转储文件作为服务器数据的快照。Use --all-databases, --events, and --routines to back up all data, and --master-data=2 to include the current binary log coordinates in the dump file.使用--all-databases--events--routines备份所有数据,使用--master-data=2将当前二进制日志坐标包含在转储文件中。

mysqldump --host=host_name --all-databases --events --routines --master-data=2> dump_file

Execute the mysqldump command periodically to create newer snapshots as desired.定期执行mysqldump命令,根据需要创建较新的快照。

If data loss occurs (for example, if the server unexpectedly exits), use the most recent dump file to restore the data:如果发生数据丢失(例如,如果服务器意外退出),请使用最新的转储文件还原数据:

mysql --host=host_name -u root -p < dump_file

Then use the binary log backup to re-execute events that were written after the coordinates listed in the dump file. Suppose that the coordinates in the file look like this:然后使用二进制日志备份重新执行在转储文件中列出的坐标之后写入的事件。假设文件中的坐标如下:

-- CHANGE MASTER TO MASTER_LOG_FILE='binlog.001002', MASTER_LOG_POS=27284;

If the most recent backed-up log file is named binlog.001004, re-execute the log events like this:如果最近备份的日志文件名为binlog.001004,请按如下方式重新执行日志事件:

mysqlbinlog --start-position=27284 binlog.001002 binlog.001003 binlog.001004
  | mysql --host=host_name -u root -p

You might find it easier to copy the backup files (dump file and binary log files) to the server host to make it easier to perform the restore operation, or if MySQL does not allow remote root access.您可能会发现,将备份文件(转储文件和二进制日志文件)复制到服务器主机更容易执行还原操作,或者MySQL不允许远程root访问。

mysqlbinlog Backup Restrictions备份限制

Binary log backups with mysqlbinlog are subject to these restrictions:使用mysqlbinlog的二进制日志备份受以下限制: