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.
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”.
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.
On other systems, you must install a short script yourself that you start from cron (or its equivalent) for handling log files.
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
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
LOGS statement (see
Section 22.214.171.124, “PURGE BINARY LOGS Statement”).
To force MySQL to start using new log files, flush the logs. Log
flushing occurs when you execute a
LOGS statement or a mysqladmin
flush-logs, mysqladmin refresh,
--source-data command. See
Section 126.96.36.199, “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
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.
If the server was started with the
--log-erroroption to cause the error log to be written to a file, the server closes and reopens the log file.
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
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:
SIGHUPsignal flushes all the logs. However,
SIGHUPhas additional effects other than log flushing that might be undesirable.
SIGUSR1causes the server to flush the error log, general query log, and slow query log. If you are interested in flushing only those logs,
SIGUSR1can be used as a more “lightweight” signal that does not have the
SIGHUPeffects that are unrelated to logs.
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
err.log, you can use a series of commands
like this from the command line:
mv mysql.log mysql.log.old
mv mysql-slow.log mysql-slow.log.old
mv err.log err.log.old
On Windows, use rename rather than mv.
At this point, you can make a backup of
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.
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 is owned by
root and not writable by
mysqld. In this case, log-flushing operations
fail to create a new log file.
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
mv /var/log/mysqld.log /var/log/mysqld.log.old
install -omysql -gmysql -m0644 /dev/null /var/log/mysqld.log