8.4 中文镜像

MySQL 8.4 参考手册 · 中文镜像

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

打开官方英文原文 ↗

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

中文点击中文段落可显示 / 隐藏英文原文本页译文覆盖 100%

7.4.6 服务器日志维护7.4.6 Server Log Maintenance

如第 7.4 节,“MySQL 服务器日志”所述,MySQL 服务器可以创建几种不同的日志文件,以帮助您查看正在进行的活动。然而,您必须定期清理这些文件,以确保日志不会占用过多的磁盘空间。As described in Section 7.4, “MySQL Server Logs”, MySQL Server can create several different log files to help you see what activity is taking place. However, you must clean up these files regularly to ensure that the logs do not take up too much disk space.

在启用日志记录的情况下使用 MySQL 时,您可能需要不时备份并删除旧的日志文件,并告诉 MySQL 开始记录到新的文件中。请参见第 9.2 节,“数据库备份方法”。When using MySQL with logging enabled, you may want to back up and remove old log files from time to time and tell MySQL to start logging to new files. See Section 9.2, “Database Backup Methods”.

在 Linux(Red Hat)安装中,您可以使用 mysql-log-rotate 脚本进行日志维护。如果您是从 RPM 软件包安装 MySQL 的,这个脚本应该会自动安装。如果您在使用二进制日志进行复制,请小心使用这个脚本。在确定所有副本已经处理了日志内容之前,不应删除二进制日志。On a Linux (Red Hat) installation, you can use the mysql-log-rotate script for log maintenance. If you installed MySQL from an RPM distribution, this script should have been installed automatically. Be careful with this script if you are using the binary log for replication. You should not remove binary logs until you are certain that their contents have been processed by all replicas.

在其他系统上,您必须自己安装一个简短的脚本,并通过 cron(或其等效工具)启动该脚本来处理日志文件。On other systems, you must install a short script yourself that you start from cron (or its equivalent) for handling log files.

二进制日志文件会在服务器的二进制日志过期周期后自动删除。文件的删除可以在启动时或二进制日志刷新时进行。默认的二进制日志过期周期为 30 天。要指定其他过期周期,请使用 binlog_expire_logs_seconds 系统变量。如果您正在使用复制功能,应指定不低于副本可能落后于源的最长时间的过期周期。要按需删除二进制日志,请使用 PURGE BINARY LOGS 语句(参见 第 15.4.1.1 节,“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. To specify an alternative expiration period, use the binlog_expire_logs_seconds system variable. 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. To remove binary logs on demand, use the PURGE BINARY LOGS statement (see Section 15.4.1.1, “PURGE BINARY LOGS Statement”).

要强制 MySQL 开始使用新的日志文件,请刷新日志。执行 FLUSH LOGS 语句,或执行 mysqladmin flush-logs、mysqladmin refresh、mysqldump --flush-logs,或 mysqldump --source-data 命令时,会发生日志刷新。参见 第 15.7.8.3 节,“FLUSH 语句”、第 6.5.2 节,“mysqladmin — MySQL 服务器管理程序” 和 第 6.5.4 节,“mysqldump — 数据库备份程序”。此外,当当前二进制日志文件大小达到 max_binlog_size 系统变量的值时,服务器会自动刷新二进制日志。To force MySQL to start using new log files, flush the logs. Log flushing occurs when you execute a FLUSH LOGS statement or a mysqladmin flush-logs, mysqladmin refresh, mysqldump --flush-logs, or mysqldump --source-data command. See Section 15.7.8.3, “FLUSH Statement”, Section 6.5.2, “mysqladmin — A MySQL Server Administration Program”, and Section 6.5.4, “mysqldump — A Database Backup Program”. In addition, the server flushes the binary log automatically when current binary log file size reaches the value of the max_binlog_size system variable.

FLUSH LOGS 支持可选的修饰符以启用对单个日志的选择性刷新(例如,FLUSH BINARY LOGS)。参见 第 15.7.8.3 节,“FLUSH 语句”。FLUSH LOGS supports optional modifiers to enable selective flushing of individual logs (for example, FLUSH BINARY LOGS). See Section 15.7.8.3, “FLUSH Statement”.

日志刷新操作具有以下效果:A log-flushing operation has the following effects:

  • 如果启用了二进制日志记录,服务器会关闭当前的二进制日志文件,并使用下一个序列号打开一个新的日志文件。If binary logging is enabled, the server closes the current binary log file and opens a new log file with the next sequence number.

  • 如果启用了通用查询日志或慢查询日志记录到日志文件,服务器会关闭并重新打开日志文件。If general query logging or slow query logging to a log file is enabled, the server closes and reopens the log file.

  • 如果服务器是使用 --log-error 选项启动,以将错误日志写入文件,服务器会关闭然后重新打开日志文件。If the server was started with the --log-error option to cause the error log to be written to a file, the server closes and reopens the log file.

执行日志刷新语句或命令需要使用具有 RELOAD 权限的账户连接到服务器。在 Unix 和类 Unix 系统上,刷新日志的另一种方法是向服务器发送信号,这可以由 root 或拥有服务器进程的账户完成。(参见 第 6.10 节,“MySQL 中的 Unix 信号处理”。)信号使得可以在无需连接到服务器的情况下执行日志刷新:Execution of log-flushing statements or commands requires connecting to the server using an account that has the RELOAD privilege. On Unix and Unix-like systems, another way to flush the logs is to send a signal to the server, which can be done by root or the account that owns the server process. (See Section 6.10, “Unix Signal Handling in MySQL”.) Signals enable log flushing to be performed without having to connect to the server:

  • SIGHUP 信号会刷新所有日志。然而,SIGHUP 除了刷新日志之外,还有其他可能不希望发生的影响。A SIGHUP signal flushes all the logs. However, SIGHUP has additional effects other than log flushing that might be undesirable.

  • SIGUSR1 会导致服务器刷新错误日志、通用查询日志和慢查询日志。如果你只想刷新这些日志,可以使用 SIGUSR1 作为一种更“轻量级”的信号,它不会产生与日志无关的 SIGHUP 影响。SIGUSR1 causes the server to flush the error log, general query log, and slow query log. If you are interested in flushing only those logs, SIGUSR1 can be used as a more “lightweight” signal that does not have the SIGHUP effects that are unrelated to logs.

如前所述,刷新二进制日志会创建一个新的二进制日志文件,而刷新通用查询日志、慢查询日志或错误日志只是关闭并重新打开日志文件。对于后者的日志,在 Unix 上要创建新的日志文件,首先需要在刷新之前重命名当前日志文件。在刷新时,服务器会使用原来的名称打开新的日志文件。例如,如果通用查询日志、慢查询日志和错误日志文件分别命名为 mysql.log、mysql-slow.log 和 err.log,你可以在命令行中使用类似的一系列命令:As mentioned previously, flushing the binary log creates a new binary log file, whereas flushing the general query log, slow query log, or error log just closes and reopens the log file. For the latter logs, to cause a new log file to be created on Unix, rename the current log file first before flushing it. At flush time, the server opens the new log file with the original name. For example, if the general query log, slow query log, and error log files are named mysql.log, mysql-slow.log, and err.log, you can use a series of commands like this from the command line:

cd mysql-data-directory
mv mysql.log mysql.log.old
mv mysql-slow.log mysql-slow.log.old
mv err.log err.log.old
mysqladmin flush-logs

在 Windows 上,使用 rename 而不是 mv。On Windows, use rename rather than mv.

此时,你可以备份 mysql.log.old、mysql-slow.log.old 和 err.log.old,然后将它们从磁盘中删除。At this point, you can make a backup of mysql.log.old, mysql-slow.log.old, and err.log.old, then remove them from disk.

要在运行时重命名通用查询日志或慢查询日志,首先连接到服务器并禁用日志:To rename the general query log or slow query log at runtime, first connect to the server and disable the log:

SET GLOBAL general_log = 'OFF';
SET GLOBAL slow_query_log = 'OFF';

在日志被禁用后,可在外部重命名日志文件(例如,从命令行)。然后再次启用日志:With the logs disabled, rename the log files externally (for example, from the command line). Then enable the logs again:

SET GLOBAL general_log = 'ON';
SET GLOBAL slow_query_log = 'ON';

这种方法适用于任何平台,并且不需要重启服务器。This method works on any platform and does not require a server restart.

Note

为了让服务器在您外部重命名文件后重新创建指定的日志文件,文件位置必须可被服务器写入。这并不总是可能的。例如,在 Linux 上,服务器可能将错误日志写入 /var/log/mysqld.log,其中 /var/log 归 root 所有,而 mysqld 无法写入。在这种情况下,日志刷新操作无法创建新日志文件。For the server to recreate a given log file after you have renamed the file externally, the file location must be writable by the server. This may not always be the case. For example, on Linux, the server might write the error log as /var/log/mysqld.log, where /var/log is owned by root and not writable by mysqld. In this case, log-flushing operations fail to create a new log file.

为处理这种情况,您必须在重命名原始日志文件后手动创建具有适当所有权的新日志文件。例如,作为 root 执行以下命令:To handle this situation, you must manually create the new log file with the proper ownership after renaming the original log file. For example, execute these commands as root:

mv /var/log/mysqld.log /var/log/mysqld.log.old
install -omysql -gmysql -m0644 /dev/null /var/log/mysqld.log