How To Enable Slow Query Log for MySQL

Enable Slow Query Log for MySQL

In this tutorial, we will show you how to enable a slow query log for MySQL on Linux systems. MySQL has built-in functionality that allows you to log SQL queries to a file, You can enable the full SQL query 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. We assume that you already have MySQL installed on the Linux system with administrative privileges and we assume that you already have a small amount of knowledge of MySQL.

This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo‘ to the commands to get root privileges. I will show you the step-by-step enable slow query log for MySQL.


  • A server running one of the following operating systems: CentOS Linux.
  • It’s recommended that you use a fresh OS install to prevent any potential issues.
  • SSH access to the server (or just open Terminal if you’re on a desktop).
  • A non-root sudo user or access to the root user. We recommend acting as a non-root sudo user, however, as you can harm your system if you’re not careful when acting as the root.

Enable Slow Query Log for MySQL

Step 1. First, let’s start by ensuring your system is up-to-date.

sudo dnf clean all
sudo dnf update

Step 2. Configure MySQL server.

Now we edit the /etc/my.cnf file with your favorite text editor.

nano /etc/my.cnf

Once you have your my.cnf file open, add the following line under the “[mysqld]” section.

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

Save and close the file, then create the file slow-queries.log. You can have the file in any spot you wish, as long as you define the path in your my.cnf.

touch /var/log/mysql/slow-queries.log
chown mysql.mysql /var/log/mysql/slow-queries.log

Finally, restart the MySQL service. Enter the following command:

sudo systemctl restart mysql

Step 3. Verify Slow Log Query.

Once successfully configured. Check for the “slow_query_log” parameter (it should be “ON”):

mysql> show variables like '%slow%';
| Variable_name       | Value                          |
| log_slow_queries    | ON                             |
| slow_launch_time    | 2                              |
| slow_query_log      | ON                             |
| slow_query_log_file | /var/lib/mysql/mysqld-slow.log |
4 rows in set (0.00 sec)

Next, we check for the parameter “long_query_time”, it should have the time as 5 seconds:

mysql> show variables like '%long%';
| Variable_name      | Value    |
| long_query_time    | 5.000000 |
| max_long_data_size | 1048576  |
2 rows in set (0.00 sec)

For additional resources on installing and managing MySQL, read the post below:

Congratulations! You have successfully enabled a slow query log on MySQL. Thanks for using this tutorial to enable a slow query log on MySQL in the Linux system. For additional help or useful information, we recommend you check the official MySQL website.

MySQL Configuration Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “MySQL Configuration”, starting from $10 (Paypal payment). Please contact us to get the best deal!


r00t is a seasoned Linux system administrator with a wealth of experience in the field. Known for his contributions to idroot.us, r00t has authored numerous tutorials and guides, helping users navigate the complexities of Linux systems. His expertise spans across various Linux distributions, including Ubuntu, CentOS, and Debian. r00t's work is characterized by his ability to simplify complex concepts, making Linux more accessible to users of all skill levels. His dedication to the Linux community and his commitment to sharing knowledge makes him a respected figure in the field.
Back to top button