How to enable the slow query log in MySQL?

Slow MySQL queries can significantly lower down the performance of your web application and eat up more server resources. MySQL server slow query log enables you to find those queries which take a long time to execute. Linux VPS and Dedicated server customers can edit my.cnf file to enable slow query log. This article will explain the steps to edit your my.cnf file and enable slow query log.

  1. Login to your server via SSH.

  2. To edit the MySQL settings with nano editor issue the following command. You can also use other editors like: Vi, Pico, Emacs etc.

    nano /etc/my.cnf
  3. To enable mysql slow query log, add following lines under [mysqld] section.

    long_query_time=3  |  This statement tells MySQL server to log any query that takes longer that 3 seconds.
    log_slow_queries=/var/log/mysql/log-slow-queries.log | This statement tells MySQL server where to log the slow queries.
    log_queries_not_using_indexes=YES | This is an optional statement that logs queries that don’t use an index.

    After adding above lines, enter Ctrl + O to save the changes and then Ctrl + X to exit.

  4. Create MySQL log directory and slow log file using following commands.

    mkdir /var/log/mysql
    touch /var/log/mysql/log-slow-queries.log
    chown mysql.mysql -R /var/log/mysql
  5. When you’ve made the changes to my.cnf file and created the log file you need to restart mysql server with following command.

    service mysqld restart

  • 0 Users Found This Useful

Was this answer helpful?

Related Articles

Fix :: Unable to login at phpMyadmin after revoking MySQL root user

Problem Statement I am not able to login into phpMyadmin after revoking root user. Solution...

How to Administrate MySQL Server with mysqladmin commands?

By using mysqladmin commands, you can perform basic MySQL tasks very easily, such as...

Fix - PHPMyAdmin Error You should upgrade to MySQL 5.5.0 or later

Problem statement Recently we upgraded phpMyAdmin version from 4.0.10.12 to 4.5.3.1 in our...

How to Fix MySQL Error "The server quit without updating PID file"?

MySQL server plays a vital role for your Linux Server. It runs all of your website's databases,...

How to Fix MySQL Error "Plugin 'InnoDB' registration as a STORAGE ENGINE failed"?

Problem Statement MySQL service is stopped. Error Message 150206 4:42:12 [ERROR] Plugin...