How to Enable and Configure MySQL Query Logging on macOS for Debugging Purposes


2 views

When debugging database applications, enabling proper MySQL logging is crucial. For your MySQL 5.0.45 installation on OS X 10.6.1, let's examine the complete configuration process.

First, check your current logging configuration with:

mysql> SHOW VARIABLES LIKE '%log%';

This reveals whether logging is enabled and where logs are being written. In your case, the general query log shows as ON but isn't producing output, suggesting configuration or permission issues.

Your current my.cnf contains basic logging directives. Let's expand it with additional useful parameters:

[mysqld]
bind-address = 127.0.0.1
log = /var/log/mysqld.log
log-error = /var/log/mysqld.error.log
general_log = 1
general_log_file = /var/log/mysql-query.log
slow_query_log = 1
slow_query_log_file = /var/log/mysql-slow.log
long_query_time = 1
log_queries_not_using_indexes = 1

On macOS, MySQL typically runs as user '_mysql'. Ensure proper permissions:

sudo touch /var/log/mysql-query.log
sudo touch /var/log/mysql-slow.log
sudo chown _mysql /var/log/mysql-*.log
sudo chmod 640 /var/log/mysql-*.log

If the configuration file approach isn't working, try enabling logs dynamically:

mysql> SET GLOBAL general_log = 'ON';
mysql> SET GLOBAL slow_query_log = 'ON';
mysql> SET GLOBAL long_query_time = 0;  -- Log all queries
mysql> SET GLOBAL log_output = 'FILE';

Check where MySQL is actually writing logs:

mysql> SHOW VARIABLES LIKE 'general_log_file';
mysql> SHOW VARIABLES LIKE 'slow_query_log_file';

If logs remain empty after configuration:

  1. Verify MySQL user has write permissions to log directory
  2. Check disk space availability
  3. Confirm the log_bin variable isn't interfering
  4. Review error logs for configuration problems

For production systems, implement log rotation:

sudo nano /etc/newsyslog.d/mysql.conf
# Add:
/var/log/mysql-query.log 640 _mysql _mysql 7 * 24
/var/log/mysql-slow.log 640 _mysql _mysql 7 * 24

Consider using these additional tools:

# MySQL Enterprise Monitor
# pt-query-digest from Percona Toolkit
# mytop for real-time monitoring

When troubleshooting MySQL logging configuration, we need to verify multiple components:

# Check running MySQL process parameters
ps auxww | grep [m]ysqld

# Verify effective logging parameters
$(ps auxww|sed -n '/sed -n/d;/mysqld /{s/.* \$[^ ]*mysqld\$ .*/\\1/;p;}') --verbose --help | grep '^log'

# Check current logging variables
mysql> SHOW VARIABLES LIKE '%log%';

The key configuration file my.cnf should include these essential directives:

[mysqld]
general_log = 1
general_log_file = /var/log/mysql-query.log
log_error = /var/log/mysql-error.log
slow_query_log = 1
slow_query_log_file = /var/log/mysql-slow.log
long_query_time = 2
log_queries_not_using_indexes = 1

Several factors can prevent logging from working:

  • File permissions: MySQL user needs write access
  • SELinux/AppArmor restrictions on Mac OS X
  • Incorrect config file location (try /etc/my.cnf, /etc/mysql/my.cnf, or ~/my.cnf)
  • Missing log file rotation configuration

After configuration changes, always:

# Restart MySQL service
sudo /usr/local/mysql/support-files/mysql.server restart

# Verify logging is active
mysql> SHOW VARIABLES LIKE 'general_log%';
mysql> SHOW VARIABLES LIKE 'slow_query_log%';

# Test with sample queries
mysql> SELECT 1;
mysql> SELECT SLEEP(3);

For production environments, consider these additional settings:

[mysqld]
log_output = FILE
log_slow_admin_statements = 1
log_slow_slave_statements = 1
log_warnings = 2
log_bin = /var/log/mysql/mysql-bin.log
binlog_format = ROW
expire_logs_days = 10
max_binlog_size = 100M