简体   繁体   中英

Truncate Slow Query Log in MySQL

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?

By safe I mean:

  • Mustn't cause any permissions problems
  • Mustn't jump back to its original size next time its appended to

To truncate a file regardless if it is open by a running application do:

> /var/logs/your.log

at your shell prompt.

From here :

You can move the open file, then tell mysqld to flush logs, which will open a new log.

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

If this is going to be a ongoing concern, you should take a look at logrotate as it can do this for you.

I did not know about echo > file.log , though it makes sense. I've always used 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!

Keep in mind that just deleting (rm) or moving (mv) the file will not free up space until mysql restart.

On ubuntu and MariaDB i used this to empty the disk space used from mysql-slow.log without restarting.

echo > /var/lib/mysql/mysql-slow.log

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM