MySQL General Log
Luke Hebert
- 2 minutes read - 327 wordsThe MySQL general log records every statement and connection event that reaches the server. It is one of the fastest ways to figure out what a client is actually sending when something looks off, and it takes only a few commands to get running.
Enabling the General Log
By default the general log is off. To turn it on for the current session and persist the setting, run:
SET GLOBAL general_log = 'OFF';
ALTER TABLE mysql.general_log ENGINE = MyISAM;
SET GLOBAL general_log = 'ON';The ALTER TABLE step ensures the mysql.general_log table uses the MyISAM engine, which supports concurrent reads while the log is being written. Without that, you may hit lock contention when querying the table.
Querying the Log
Once the log is active, you can inspect recent entries directly:
SELECT * FROM mysql.general_log LIMIT 2;Each row contains the event timestamp, the connection ID, the command type (Query, Connect, Quit, etc.), and the full statement text.
Practical Tips
- Scope it down. The general log can grow quickly on a busy server. Use
LIMITand filter byevent_timeorthread_idto keep queries fast. - Turn it off when done. Run
SET GLOBAL general_log = 'OFF';once you have the data you need so you are not writing every statement to disk indefinitely. - Check the file log too. If you prefer file-based logging, set
general_log_fileto a path under your data directory and confirmlog_outputincludesFILE.
The general log captures every statement, including those that contain sensitive data such as passwords in SET PASSWORD or application credentials in query parameters. Treat the log file with the same care you would treat a credentials file.
Disabling the General Log
When you are finished troubleshooting, disable it to stop the disk I/O overhead:
SET GLOBAL general_log = 'OFF';If you want the change to survive a restart, also update the configuration file (my.cnf or my.ini) and set general_log = 0 under the [mysqld] section.