• Archives

  • Categories

Nov 22

How to log slow MySQL queries

I haven’t been generous enough to spend money on higher RAM for my blogs. I started to notice slow downs in the blog recent, particularly caused by a particular plugin that does related posts. I found out by checking on slow queries log. Here’s how to get MySQL to log slow queries:

If you are using Ubuntu of Debian-based operating systems, it’s found in /etc/mysql/my.cnf. You may need to use

sudo nano /etc/mysql/my.cnf

Press CTRL + W, that’s search in nano. Find “slow” for the section for logging slow queries.

# Here you can see queries with especially long duration
#log_slow_queries        = /var/log/mysql/mysql-slow.log
#long_query_time = 2
#log-queries-not-using-indexes

I uncommented “log_slow_queries” and “long_query_time”. I put long_query_time to 8. That requires a bit of experimentation. It depends on your environment.

I run the following command to check the slow queries:

sudo cat /var/log/mysql/mysql-slow.log

After you change the configuration in my.cnf, you need to restart. You can do so using:

sudo /etc/init.d/mysql restart

Hope it helps.

Possibly related:

  1. How to set up VirtualHost in Ubuntu
  2. How to redirect www to no-www in Lighttpd
  3. How to repair MySQL tables
  4. My disk got full

No comments yet. Leave yours here.


WordPress powered and Django inspired.
Love and elephants come after.
RSS: Posts and comments.