How To Enable Slow Query Log for MySQL

Enable Slow Query Log for MySQL

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 Linux system with administrative privileges and we assume that you already have know how on MySQL.

In order to enable slow query logs for MySQL on your system you would need to do the following.

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.

[mysqld]
log-slow-queries
log-slow-queries= /var/log/mysql/slow-queries.log
long_query_time=1

 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 mysql service. Enter the following command:

 #service mysqld restart

Congratulation’s! You have successfully enable slow query log on MySQL. Thanks for using this tutorial for enable slow query log on MySQL in Linux system. For additional help or useful information, we recommend you to check the official MySQL web site.

You Might Also Like: How To Install Mtop (MySQL Database Server Monitoring) on CentOS 6