07 Jun

How to enable slow query logs in Mysql

Enable slow query logs in Msyql
Learn how you can log slow queries in your Mysql server

Mysql allows you to log slow queries which takes longer to execute. Slow query logging is helpful in analyzing queries which may be making your application slow.

Enable Slow Query Logs in Mysql

  1. One can enable query logs by modifying the /etc/mysql/my.cnf file.
  2. Open the file and search for the following lines
    #log_slow_queries = /var/log/mysql/mysql-slow.log
    #long_query_time = 2

    You need to uncomment(remove # in front of the line) the above lines to enable slow query logs and store slow queries in a log file.
    Here log_slow_queries defines the path(/var/log/mysql/mysql-slow.log) of the file where slow query logs will be stored and only those query will be logged which takes more than than 2 seconds to execute(long_query_time). You can specify any value you like for long_query_time.
  3. Once you save all the changes. You need to restart your Mysql server for the changes to take effect. Run the following command to restart mysql server.
  4. Now all your slow queries will be logged by Mysql.
  5. You can see all your slow queries in  /var/log/mysql/mysql-slow.log  file.

A simple entry in query log file will look something like this:
Screen Shot 2015-06-07 at 6.08.10 PM

Hope you find this article helpful. If you have any questions, please ask in comments. :)

The following two tabs change content below.

Vineet Garg

Sr. Web Developer at GuestHouser
I am passionate about technology and web development.

Latest posts by Vineet Garg (see all)