MySQL
database logging
query logging
troubleshooting
SQL debugging

Log all queries in mysql

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

If you want MySQL to record every statement it receives, you are looking for the general query log. It logs connects, disconnects, and SQL statements, which makes it useful for debugging, auditing short-lived incidents, or verifying what an application is actually sending to the server.

The tradeoff is cost. Logging every query adds I/O overhead and can produce very large log files, so it is usually something you enable temporarily rather than leave on forever in a busy production environment.

Use the General Query Log

MySQL has several log types, but only one is meant for "log everything": the general query log.

The main server settings are:

  • 'general_log to turn the feature on or off'
  • 'general_log_file to choose the file path when logging to a file'
  • 'log_output to choose FILE, TABLE, or both'

A typical configuration file snippet looks like this:

ini
1[mysqld]
2general_log = ON
3log_output = FILE
4general_log_file = /var/log/mysql/general.log

After changing the config file, restart MySQL for the setting to persist across restarts.

Enabling It Without Restarting

For temporary troubleshooting, you can enable the general log at runtime:

sql
SET GLOBAL log_output = 'FILE';
SET GLOBAL general_log_file = '/var/log/mysql/general.log';
SET GLOBAL general_log = 'ON';

To turn it back off:

sql
SET GLOBAL general_log = 'OFF';

This is often the safest workflow in production because you can enable the log only for the debugging window you care about.

Verifying the Setting

Check the current values directly from MySQL:

sql
SHOW VARIABLES LIKE 'general_log';
SHOW VARIABLES LIKE 'general_log_file';
SHOW VARIABLES LIKE 'log_output';

If the output shows general_log = ON, MySQL is actively recording statements.

You can then inspect the file with normal shell tools:

bash
tail -f /var/log/mysql/general.log

That makes it easy to correlate application actions with the exact SQL MySQL received.

Logging to a Table Instead of a File

MySQL can also write the general log into the mysql.general_log table:

sql
SET GLOBAL log_output = 'TABLE';
SET GLOBAL general_log = 'ON';

Then query it like any other table:

sql
1SELECT event_time, user_host, argument
2FROM mysql.general_log
3ORDER BY event_time DESC
4LIMIT 20;

This can be convenient for ad hoc inspection from SQL clients, but table logging also has overhead and is not always ideal for sustained heavy traffic.

When the Slow Query Log Is Better

A common mistake is enabling the general log when the real goal is performance tuning. If you only care about expensive queries, the slow query log is often the better tool:

ini
1[mysqld]
2slow_query_log = ON
3slow_query_log_file = /var/log/mysql/slow.log
4long_query_time = 1

The general log answers "what did the server receive?" The slow log answers "which statements were expensive?" Use the right tool for the question.

Security and Operational Considerations

Query logs may contain sensitive information such as literal values, email addresses, API tokens, or personally identifiable data. Protect the log files with appropriate permissions and retention policies.

Also plan for file growth. A busy application can generate huge general logs in a short time. Rotate or truncate logs carefully, and make sure the filesystem has enough space before enabling the feature.

If you only need a quick snapshot, enable logging, reproduce the issue, inspect the output, and disable it again.

Common Pitfalls

The most common mistake is leaving the general query log enabled indefinitely on a busy system. That can create unnecessary disk I/O, consume storage quickly, and sometimes distort the very performance problem you are trying to diagnose.

Another issue is confusing the general query log with the binary log. The binary log is mainly for replication and recovery, not for easy human-readable inspection of every incoming statement.

People also forget to check log_output. If it is set differently than expected, you may look for a file that never receives data or query a log table that stays empty.

Finally, remember that runtime changes made with SET GLOBAL are usually not persistent across restarts unless you also update the configuration file.

Summary

  • To log all queries in MySQL, enable the general query log.
  • Use SET GLOBAL general_log = 'ON' for temporary runtime troubleshooting.
  • Control the destination with log_output, usually FILE or TABLE.
  • Verify the active settings with SHOW VARIABLES.
  • Disable the log when you are done because full query logging has real performance and storage costs.

Course illustration
Course illustration

All Rights Reserved.