MySQL slow query log

The MySQL slow query log enables you to log all queries that take longer than a specified number of seconds to execute.  This should help identify any queries that are not performing sufficiently.

All that is required is two additional lines in the MySQL configuration file “my.cnf” in the [mysqld] section.

1
2
3
[mysqld]
log-slow-queries = /var/log/mysql-slow.log
long_query_time = 4

The above configuration will log any queries taking longer than 4 seconds to execute in to the specified log file.

MySQL appears to require that the specified log file exists otherwise the logging is disabled and the error below appears in the MySQL log. (Or it may be that it didn’t have permissions to create the file. I haven’t investigated).

1
081013 14:55:37 [ERROR] Could not use /var/log/mysql-slow.log for logging (error 13). Turning logging off for the whole duration of the MySQL server process. To turn it on again: fix the cause, shutdown the MySQL server and restart it.

To create the log file I did the following:

1
2
3
touch /var/log/mysql-slow.log
chown mysql.mysql /var/log/mysql-slow.log
chmod o-r /var/log/mysql-slow.log

The third command is probably unnecessary, but it meant that the permissions matched the mysqld.log in the same directory.

Once this is done MySQL needs to be restarted to start the query logging:

1
service mysql restart

One Response to “MySQL slow query log”

  1. Sweet, thanks!! This fixed this error on my server, however I’m having the following issue as well:

    Plugin ‘InnoDB’ init function returned error.

Leave a Reply