MySQL log maintenance
我们可以手动将日志文件中的数据刷到磁盘上,这一动作将触发MySQL服务器使用一个新的日志文件。过程如下:
-
If general query logging or slow query logging to a log file is enabled, the server
closes and reopens
the general query log file or slow query log file. -
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 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.
The server creates a new binary log file when you flush the logs. However, it just closes and reopens the general and slow query log files. To cause new files to be created on Unix, rename the current log files before flushing them. At flush time, the server opens new log files with the original names. For example, if the general and slow query log files are named mysql.log and mysql-slow.log, you can use a series of commands like this:
1 | root:(none)> show variables like '%slow%'; |
1 | cd /var/lib/mysql/ |
通过这种方式,我们可以备份/移走旧的日志文件。
如果有错误日志文件,可以使用类似的策略来备份错误日志文件。
还可以通过禁用日志在运行时重命名常规日志或慢查询日志,此方法适用于任何平台,不需要重新启动服务器。
1 | SET GLOBAL general_log = 'OFF'; |
Validate
1 | root:(none)> select sleep(10) as s; |
1 | tail -5 /var/lib/mysql/mysql01-slow.log |
Reference
MySQL 5.7 Reference Manual
Author: Acqua
Link: https://acquaai.github.io/2017/12/15/mysql-log-maintenance/
License: 知识共享署名-非商业性使用 4.0 国际许可协议