13.4.1.1 PURGE BINARY LOGS Statement语句

PURGE { BINARY | MASTER } LOGS {
    TO 'log_name'
  | BEFORE datetime_expr
}

The binary log is a set of files that contain information about data modifications made by the MySQL server. 二进制日志是一组文件,其中包含有关MySQL服务器所做数据修改的信息。The log consists of a set of binary log files, plus an index file (see Section 5.4.4, “The Binary Log”).日志由一组二进制日志文件和一个索引文件组成(请参阅第5.4.4节,“二进制日志”)。

The PURGE BINARY LOGS statement deletes all the binary log files listed in the log index file prior to the specified log file name or date. PURGE BINARY LOGS语句删除指定日志文件名或日期之前日志索引文件中列出的所有二进制日志文件。BINARY and MASTER are synonyms. BINARYMASTER是同义词。Deleted log files also are removed from the list recorded in the index file, so that the given log file becomes the first in the list.已删除的日志文件也将从索引文件中记录的列表中删除,以便给定的日志文件成为列表中的第一个日志文件。

PURGE BINARY LOGS requires the BINLOG_ADMIN privilege. PURGE BINARY LOGS需要BINLOG_ADMIN权限。This statement has no effect if the server was not started with the --log-bin option to enable binary logging.如果服务器未使用--log-bin选项启动以启用二进制日志记录,则此语句无效。

Examples:示例:

PURGE BINARY LOGS TO 'mysql-bin.010';
PURGE BINARY LOGS BEFORE '2019-04-02 22:46:26';

The BEFORE variant's datetime_expr argument should evaluate to a DATETIME value (a value in 'YYYY-MM-DD hh:mm:ss' format).BEFORE变量的datetime_expr参数应计算为DATETIME值('YYYY-MM-DD hh:mm:ss'格式的值)。

This statement is safe to run while replicas are replicating. 复制副本时可以安全运行此语句。You need not stop them. 你不需要阻止他们。If you have an active replica that currently is reading one of the log files you are trying to delete, this statement does not delete the log file that is in use or any log files later than that one, but it deletes any earlier log files. 如果您的活动复制副本当前正在读取您试图删除的某个日志文件,则此语句不会删除正在使用的日志文件或该日志文件之后的任何日志文件,但会删除任何较早的日志文件。A warning message is issued in this situation. 在这种情况下会发出警告消息。However, if a replica is not connected and you happen to purge one of the log files it has yet to read, the replica cannot replicate after it reconnects.但是,如果复制副本未连接,而您恰好清除了其中一个尚未读取的日志文件,则复制副本在重新连接后将无法复制。

To safely purge binary log files, follow this procedure:要安全地清除二进制日志文件,请执行以下步骤:

  1. On each replica, use SHOW REPLICA | SLAVE STATUS to check which log file it is reading.在每个副本上,使用SHOW replica | SLAVE STATUS检查正在读取的日志文件。

  2. Obtain a listing of the binary log files on the source with SHOW BINARY LOGS.使用SHOW BINARY LOGS获取源上二进制日志文件的列表。

  3. Determine the earliest log file among all the replicas. 确定所有副本中最早的日志文件。This is the target file. If all the replicas are up to date, this is the last log file on the list.这是目标文件。如果所有副本都是最新的,则这是列表上的最后一个日志文件。

  4. Make a backup of all the log files you are about to delete. 对要删除的所有日志文件进行备份。(This step is optional, but always advisable.)(此步骤是可选的,但始终是可取的。)

  5. Purge all log files up to but not including the target file.清除目标文件之前但不包括目标文件的所有日志文件。

PURGE BINARY LOGS TO and PURGE BINARY LOGS BEFORE both fail with an error when binary log files listed in the .index file had been removed from the system by some other means (such as using rm on Linux). .index文件中列出的二进制日志文件已通过其他方式(例如在Linux上使用rm)从系统中删除时,PURGE BINARY LOGS TOPURGE BINARY LOGS BEFORE都会失败,并出现错误。(Bug #18199, Bug #18453) To handle such errors, edit the .index file (which is a simple text file) manually to ensure that it lists only the binary log files that are actually present, then run again the PURGE BINARY LOGS statement that failed.要处理此类错误,请手动编辑.index文件(这是一个简单的文本文件),以确保它仅列出实际存在的二进制日志文件,然后再次运行失败的PURGE BINARY LOGS语句。

Binary log files are automatically removed after the server's binary log expiration period. 二进制日志文件在服务器的二进制日志过期后自动删除。Removal of the files can take place at startup and when the binary log is flushed. 可以在启动时以及刷新二进制日志时删除文件。The default binary log expiration period is 30 days. 默认二进制日志过期期限为30天。You can specify an alternative expiration period using the binlog_expire_logs_seconds system variable. 您可以使用binlog_expire_logs_seconds系统变量指定替代的过期期限。If you are using replication, you should specify an expiration period that is no lower than the maximum amount of time your replicas might lag behind the source.如果您使用的是复制,则应指定一个不低于复制副本可能落后于源的最长时间的过期期限。