Home > MySQL > Enable MySQL Query Log File

Enable MySQL Query Log File

October 26th, 2008 Leave a comment Go to comments

MySQL has built-in functionality that allows you to log SQL queries to a file , You can enable the full SQL queries logs to a file or only slow running queries log.  It is easy for us to troubleshoot/ debug the sql statement if SQL queries log enable , The slow query log  is  used to find queries that take a long time to execute and are therefore candidates for optimization.

To enable you just need to add some lines to your my.cnf file, and restart. Add the following:

  • To enable slow Query Log only

log-slow-queries = /var/log/mysql/mysql-slow.log
long_query_time = 1

After enabling slow query, mysqld writes a statement to the slow query log file and it consists of all SQL statements that took more than long_query_time seconds to execute. The time to acquire the initial table locks is not counted as execution time. mysqld only log after SQL statements has been executed and after all locks have been released, so log order might be different from execution order. The minimum and default values of long_query_time are 1 and 10, respectively.

  • To enable full Log Query

log=/var/log/mysqldquery.log

The above will log all queries to the log file.

Note :: Don’t forgot to restart mysql service after making changes in my.cnf file.

Regards

  1. jarein shone
    December 2nd, 2011 at 08:32 | #1

    If I remove following two lines and restart mysql, will it stop writing to mysql-slow.log?

    log-slow-queries = /var/log/mysql/mysql-slow.log
    long_query_time = 1

  1. April 29th, 2009 at 21:09 | #1
  2. April 12th, 2010 at 07:10 | #2