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 mysqlbinlog可以直接从本地文件系统读取日志文件,或者,使用--read-from-remote-server
option, it can connect to a server and request binary log contents from that server. --read-from-remote-server
选项,它可以连接到服务器并从该服务器请求二进制日志内容。mysqlbinlog writes text output to its standard output, or to the file named as the value of the mysqlbinlog将文本输出写入其标准输出,或者写入名为--result-file=
option if that option is given.file_name
--result-file=file_name
选项值的文件(如果给定该选项)。
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。
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生成的加密二进制日志文件的副本以未加密的格式存储。
Binary log backup requires that you invoke mysqlbinlog with two options at minimum:二进制日志备份要求您至少使用两个选项调用mysqlbinlog:
The --read-from-remote-server
(or -R
) option tells mysqlbinlog to connect to a server and request its binary log. (This is similar to a replica server connecting to its replication source server.)--read-from-remote-server
(或-R
)选项告诉mysqlbinlog连接到服务器并请求其二进制日志。(这类似于连接到其复制源服务器的副本服务器。)
The --raw
option tells mysqlbinlog to write raw (binary) output, not text output.--raw
选项告诉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
结合使用也很有用:
--stop-never
: Stay connected to the server after reaching the end of the last log file and continue to read new events.:在到达最后一个日志文件末尾后保持与服务器的连接,并继续读取新事件。
--connection-server-id=
: The server ID that mysqlbinlog reports when it connects to a server. id
--connection-server-id=id
:mysqlbinlog在连接到服务器时报告的服务器id。When 当使用--stop-never
is used, the default reported server ID is 1. --stop-never
时,默认报告的服务器ID为1。If this causes a conflict with the ID of a replica server or another mysqlbinlog process, use 如果这导致与副本服务器或另一个mysqlbinlog进程的ID冲突,请使用--connection-server-id
to specify an alternative server ID. --connection-server-id
指定另一个服务器ID。See Section 4.6.9.4, “Specifying the mysqlbinlog Server ID”.请参阅第4.6.9.4节,“指定mysqlbinlog服务器ID”。
--result-file
: A prefix for output file names, as described later.:输出文件名的前缀,如稍后所述。
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将二进制日志备份到当前目录,如下所示(在一行中输入每个命令):
To make a static backup of 要通过binlog.000130
through binlog.000132
, use either of these commands:binlog.000132
对binlog.00013
进行静态备份,请使用以下命令之一:
mysqlbinlog --read-from-remote-server --host=host_name
--raw binlog.000130 binlog.000131 binlog.000132 mysqlbinlog --read-from-remote-server --host=host_name
--raw --to-last-log binlog.000130
The first command specifies every file name explicitly. The second names only the first file and uses 第一个命令明确指定每个文件名。第二个仅命名第一个文件,并使用--to-last-log
to read through the last. --to-last-log
读取最后一个文件。A difference between these commands is that if the server happens to open 这些命令之间的区别在于,如果服务器在binlog.000133
before mysqlbinlog reaches the end of binlog.000132
, the first command does not read it, but the second command does.mysqlbinlog
到达binlog.000132
的末尾之前恰好打开了binlog.00013
,则第一个命令不会读取它,但第二个命令会读取它。
To make a live backup in which mysqlbinlog starts with 要进行实时备份,其中mysqlbinlog以binlog.000130
to copy existing log files, then stays connected to copy new events as the server generates them:binlog.000130
开头以复制现有日志文件,然后保持连接以在服务器生成新事件时复制它们:
mysqlbinlog --read-from-remote-server --host=host_name
--raw
--stop-never binlog.000130
With 使用--stop-never
, it is not necessary to specify --to-last-log
to read to the last log file because that option is implied.--stop-never
,不需要指定--to-last-log
以读取最后一个日志文件,因为该选项是隐含的。
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. --raw
,mysqlbinlog将生成文本输出,如果给出--result-file
选项,则指定所有输出所写入的单个文件的名称。With 使用--raw
, mysqlbinlog writes one binary output file for each log file transferred from the server. --raw
,mysqlbinlog为从服务器传输的每个日志文件写入一个二进制输出文件。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 默认情况下,mysqlbinlog在当前目录中写入与原始日志文件同名的文件。要修改输出文件名,请使用--result-file
option. In conjunction with --raw
, the --result-file
option value is treated as a prefix that modifies the output file names.--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 如果使用mysqlbinlog --raw备份文件,--result-file
option produces output file names as shown in the following table. --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 | |
---|---|
--result-file=x | xbinlog.000999 and up |
--result-file=/tmp/ | /tmp/binlog.000999 and up |
--result-file=/tmp/x | /tmp/xbinlog.000999 and up |
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. 以下示例描述了一个简单的场景,展示了如何同时使用mysqldump和mysqlbinlog来备份服务器的数据和二进制日志,以及如何在发生数据丢失时使用备份来恢复服务器。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 您可能会发现,将备份文件(转储文件和二进制日志文件)复制到服务器主机更容易执行还原操作,或者MySQL不允许远程root
access.root
访问。
Binary log backups with mysqlbinlog are subject to these restrictions:使用mysqlbinlog的二进制日志备份受以下限制:
mysqlbinlog does not automatically reconnect to the MySQL server if the connection is lost (for example, if a server restart occurs or there is a network outage).如果连接丢失(例如,如果服务器重新启动或网络中断),mysqlbinlog不会自动重新连接到MySQL服务器。
The delay for a backup is similar to the delay for a replica server.备份的延迟类似于副本服务器的延迟。