Enable logging for MySql queries

Logging mysql queries is turned off by default. But general logging of mysql service which includes errors and warning are turned on.

Sometimes though, you want to know what query your application is actually performing. This is useful while debugging applications that for instance use ORM(Object Relational Mapping) frameworks which basically map objects to relational tables so that you don't have to write raw SQL syntax in your app. The ORM frameworks common with Ruby On Rails, Django and other frameworks translates your native language code into sql syntax. With Rails you can actually see the sql syntax in the production.log or development.log files under log directory.

But the alternative is to turn on logging from the database itself. You can do that in the following steps;

Step 1:
Open mysql configuration file my.cnf.
/etc/mysql/mysql.conf.d/mysqld.cnf

Step 2:
Search for generallogfile and generallog option and comment them out.
general_log_file = /var/log/mysql/mysql.log
general
log = 1

Step 3: restart mysql
service mysql restart

Step 4: Monitor mysql server log using tail
tail -f /var/log/mysql/mysql.log

Also put together a screencast for this. Totally a newbie to screencasting. I did this using the OBS studio after trying out numerous options. Hope to add more videos to future posts.

David Okwii

David Okwii is a Systems Engineer who currently works with Uganda's country code top-level domain registry.

Kampala Uganda http://www.davidokwii.com

Subscribe to oquidave@geek:~ #

Get the latest posts delivered right to your inbox.

or subscribe via RSS with Feedly!