What's the safest way to truncate the MySQL slow query log (under Linux primarily, but Windows would be handy to know) while MySQL is running?
在MySQL运行时,最安全的截断MySQL慢查询日志的方法是什么?
By safe I mean:
我说的“安全”是指:
- Mustn't cause any permissions problems
- 不得引起任何权限问题
- Mustn't jump back to its original size next time its appended to
- 下次当它被附加到原来的尺寸时,不能跳回原来的尺寸
3 个解决方案
#1
21
To truncate a file regardless if it is open by a running application do:
无论正在运行的应用程序是否打开文件,都要截断文件:
> /var/logs/your.log
at your shell prompt.
在shell提示符。
#2
16
From here:
从这里开始:
You can move the open file, then tell mysqld to flush logs, which will open a new log.
您可以移动打开的文件,然后告诉mysqld刷新日志,这会打开一个新的日志。
shell> cd mysql-data-directory
shell> mv mysql.log mysql.old
shell> mv mysql-slow.log mysql-slow.old
shell> mysqladmin flush-logs
#3
5
If this is going to be a ongoing concern, you should take a look at logrotate as it can do this for you.
如果这将是一个持续关注的问题,您应该看看logrotate,因为它可以为您实现这一点。
I did not know about echo > file.log
, though it makes sense. I've always used cat /dev/null > file.log
.
我不知道echo >文件。对数,虽然有道理。我总是使用cat /dev/null > file.log。
Also, it should be noted that it's very important not to delete a log file that is being written to because the program that has it open will continue to write to the file. Very difficult to figure out why all your hard drive space is gone!
此外,应该注意的是,不删除正在写入的日志文件非常重要,因为打开日志文件的程序将继续写入文件。很难弄明白为什么你所有的硬盘空间都消失了!
#1
21
To truncate a file regardless if it is open by a running application do:
无论正在运行的应用程序是否打开文件,都要截断文件:
> /var/logs/your.log
at your shell prompt.
在shell提示符。
#2
16
From here:
从这里开始:
You can move the open file, then tell mysqld to flush logs, which will open a new log.
您可以移动打开的文件,然后告诉mysqld刷新日志,这会打开一个新的日志。
shell> cd mysql-data-directory
shell> mv mysql.log mysql.old
shell> mv mysql-slow.log mysql-slow.old
shell> mysqladmin flush-logs
#3
5
If this is going to be a ongoing concern, you should take a look at logrotate as it can do this for you.
如果这将是一个持续关注的问题,您应该看看logrotate,因为它可以为您实现这一点。
I did not know about echo > file.log
, though it makes sense. I've always used cat /dev/null > file.log
.
我不知道echo >文件。对数,虽然有道理。我总是使用cat /dev/null > file.log。
Also, it should be noted that it's very important not to delete a log file that is being written to because the program that has it open will continue to write to the file. Very difficult to figure out why all your hard drive space is gone!
此外,应该注意的是,不删除正在写入的日志文件非常重要,因为打开日志文件的程序将继续写入文件。很难弄明白为什么你所有的硬盘空间都消失了!