Mysql allows you to log slow queries which takes longer to execute. Slow query logging is helpful in analyzing queries which may be making your application slow.
Enable Slow Query Logs in Mysql
- One can enable query logs by modifying the /etc/mysql/my.cnf file.
- Open the file and search for the following lines
#log_slow_queries = /var/log/mysql/mysql-slow.log
#long_query_time = 2
You need to uncomment(remove # in front of the line) the above lines to enable slow query logs and store slow queries in a log file.
Here log_slow_queries defines the path(/var/log/mysql/mysql-slow.log) of the file where slow query logs will be stored and only those query will be logged which takes more than than 2 seconds to execute(long_query_time). You can specify any value you like for long_query_time.
- Once you save all the changes. You need to restart your Mysql server for the changes to take effect. Run the following command to restart mysql server.
Vim1sudo service mysql restart
- Now all your slow queries will be logged by Mysql.
- You can see all your slow queries in /var/log/mysql/mysql-slow.log file.
Hope you find this article helpful. If you have any questions, please ask in comments.
The following two tabs change content below.
Sr. Web Developer at GuestHouser
I am passionate about technology and web development.
Latest posts by Vineet Garg (see all)
- How to enable slow query logs in Mysql - June 7, 2015
- Minify and Combine CSS and JS using Laravel Elixir - May 6, 2015
- Query Logging in Laravel 5 - April 15, 2015