Search


Friday, December 30, 2011

Enable slow query log in Mysql

How to Enable slow query log in Mysql  ?

Check whether the "slow query log" is enabled  in mysql. Access mysql and try to execute the following command.


mysql> show global variables like '%slow%';
+------------------+-------+
| Variable_name    | Value |
+------------------+-------+
| log_slow_queries | OFF   |
| slow_launch_time | 2     |
+------------------+-------+


The command result shows slow query log is currently disabled in the server.  You have add the following entries in the "/etc/my.cnf"  file in-order to enable "slow query log".  Place the entries below the  section "mysqld"

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

You must create the file manually and change owners this way:

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

 Restart mysql

Check again  whether the "slow query log" is enabled.

mysql> show global variables like '%slow%';
+------------------+-------+
| Variable_name    | Value |
+------------------+-------+
| log_slow_queries | ON    |
| slow_launch_time | 2     |
+------------------+-------+
2 rows in set (0.00 sec)

This steps will log all Mysql queries that took more than 2 seconds to complete the execution.  

No comments:

Post a Comment