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 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. 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 on 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 through the step-by-step enable slow query log for MySQL.

Enable Slow Query Log for MySQL

Step 1. Edit the /etc/my.cnf file with your favorite text editor.

nano /etc/my.cnf

Step 2. 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

Step 3. 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

Step 4. After that, restart the MySQL service. Enter the following command:

service mysqld restart

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 a best deal!