Enable MySQL query logging in Ubuntu

In this post, I explain about how to enable MySQL query logging for all queries.

To enable query logging, historically you needed to edit my.cnf file under /etc/mysql path. However, the path and the file name is not valid anymore. In the newer versions, you need to edit mysqld.cnf that is located at /etc/mysql/mysql.conf.d.

To do so you need to open the file:

$ sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf

And uncomment the following two lines:

general_log_file = /tmp/mysql.log
general_log = 1

In the above example I select /tmp/mysql.log as the filename and the path. This is not something fixed, you can still change them based on your preference.

The last step is to save the file and restart the MySQL service.

$ sudo /etc/init.d/mysqld restart

Keep in mind that query logging should be only enabled for troubleshooting and diagnosis purposes and not on production because it slows down MySQL performance and causes numerous issues.

Leave a Reply