Logging all MySQL queries on Mac OSX

We’ve been doing some debugging of a 3rd party application recently where we had some possible performance concerns under a certain load and/or size of data in the database. What we wanted to know was exactly which queries (and how many) where hitting the database on a specific page request.

The simplest method was to turn on logging of all queries on the local MySQL database, this is turned off by default as like most debugging techniques it adds overhead. To do this I needed to make a small change in the config file and restart MySQL. However it seems that a Mac installation of MySQL doesn’t create this config file by default which is a little confusing.

Here are the steps taken to turn on logging of all queries:

Create a “my.cnf” file (if it doesn’t already exist)
sudo touch /etc/my.cnf

Add an entry to my.cnf telling MySQL where to write the log entries
[mysqld]
log=/var/log/mysqld.log

Create the log file
sudo touch /var/log/mysqld.log

Restart MySQL

To view the logs simply open mysqld.log in your favourite editor, or you can tail it in the terminal:

tail -f /var/log/mysqld.log

This is a really handy way to see all the database traffic from your application. We also use MySQL Adminstrator to view some real-time stats in terms of the total number of queries being executed.

Remember once you’ve finished your debugging to turn logging off in my.cnf, I just comment it out (with a #) and restart MySQL:

#[mysqld]
#log=/var/log/mysqld.log

Note: An easy way to check if you do have a my.cnf file is to open up MySQL Administrator and click the “Options” tab. If you get an alert saying “Configuration File not Found” then you know you need to go and create one!

Once you have a my.cnf file you have access to the wide array of config options all from MySQL Administrator:

Post a Comment or Leave a Trackback

2 Comments

  1. December 18, 2008 at 10:16 pm | Permalink

    according to http://dev.mysql.com/doc/refman/6.0/en/query-log.html
    “As of MySQL 6.0.8, –log and -l are deprecated”

    had to edit the my.cnf like this:

    [mysqld]
    general_log=1
    general_log_file=”/var/log/mysqld.log”

  2. April 5, 2010 at 9:01 pm | Permalink

    It’s also worth noting that you should check the permissions on mysqld.log. I’m running Mac OS X 10.6 (Snow Leopard) and had to manually change the owner of the file to _mysqld: `sudo chown _mysqld /var/log/mysqld.log`

    Cheers!

One Trackback

  1. [...] found this link only after I found out what I needed to [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*