How to enable the MySQL Slow Query log in ubuntu

enable the MySQL Slow Query log in ubuntu

When developing your own internet application it's necessary that pages load quickly. The most important factors that confirm page response times is the time taken to SQL queries, however does one recognize that SQL queries are at fault?

The answer is MySQL's slow query log. By facultative this feature, a log file is formed which will tell you every time a query takes too long to execute. you'll be able to then use this data to either optimize your info or rewrite the SQL.

For this guide, I'll assume you have already installed MySQL or MariaDB. This article is supporting Ubuntu however the identical approach works on different UNIX operating systems.

Step 1: Modify the Slow question log

First, confirm your MySQL configuration file "my.cnf" is located - on Ubuntu it's 
/etc/mysql/my.cnf whereas on different distributions it's typically /etc/my.cnf

Edit the MySQL configuration file and add the subsequent lines at the end:

 slow_query_log_file = /var/log/mysql/slow.log  
 long_query_time = 1  

Save your changes and restart MySQL

 service mysql restart  

Step 2: examining the slow question log

Start looking at the slow log:

 tail -f /var/log/mysql/slow.log  

At startup, you may see output like this:

 /usr/sbin/mysqld, Version: 5.7.12-0ubuntu1-log ((Ubuntu)). started with:  
 Tcp port: 3306 OS socket: /var/run/mysqld/mysqld.sock  
 Time Id Command Argument  

Now, mistreatment your browser begin interacting together with your internet application and note the queries that are shown. shortly you will notice one or 2 queries that are systematically shown.

No comments

'; (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = '//' + disqus_shortname + ''; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })();