Mysql – Rotate MySQL log files

logMySQL

I want to daily rotate MySQL log files on Windows. I want to keep the files, I don't want to use tables instead.

There are 3 log files: general.log slow-queries.log and error.log

I can do it easily with a scheduled task. I saw that it recommended stopping the logs before I copy them to the backup directory.

  • One problem is that 'error.log' cannot be stopped.
  • Another problem is that stopping the logs may cause some loss of events, and I want to avoid it.

Is there a safer way (or built-in way) for logs rotation? (on a daily base or according to the file size)?

Best Answer

FLUSH LOGS gracefully does most of what you want. It stops writing to a log, renames the file and starts a new one. After that, you can deal with copying/moving/archiving/etc the files.

FLUSH LOGS can be run at any time without damaging anything or losing any log entries.

Caveat: There are many different log files; FLUSH may not work the same way on each, especially in older versions of MySQL.

For those three logs, in most systems, the general log is the biggest, followed by slow, then error. FLUSH also rotates the "binlog", but, again, that is "harmless".