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_logto turn the feature on or off' - '
general_log_fileto choose the file path when logging to a file' - '
log_outputto chooseFILE,TABLE, or both'
A typical configuration file snippet looks like this:
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:
To turn it back 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:
If the output shows general_log = ON, MySQL is actively recording statements.
You can then inspect the file with normal shell tools:
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:
Then query it like any other table:
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:
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, usuallyFILEorTABLE. - Verify the active settings with
SHOW VARIABLES. - Disable the log when you are done because full query logging has real performance and storage costs.

