When debugging database issues, it's incredibly useful to see every query MySQL is executing. The general query log shows you all queries in real-time. Here's how to turn it on.
Run these three commands in your MySQL client:
SET GLOBAL log_output = "FILE";
SET GLOBAL general_log_file = "/tmp/mysql_queries.log";
SET GLOBAL general_log = 'ON';
Now all queries are being logged to /tmp/mysql_queries.log.
On Windows, you must escape the backslashes:
SET GLOBAL log_output = "FILE";
SET GLOBAL general_log_file = "c:\\temp\\log\\mysql_queries.log";
SET GLOBAL general_log = 'ON';
Notice the double backslashes: c:\\temp\\log\\
The general log captures:
On Linux/Mac:
# Watch log in real-time
tail -f /tmp/mysql_queries.log
# View last 100 lines
tail -n 100 /tmp/mysql_queries.log
On Windows:
# In PowerShell
Get-Content c:\temp\log\mysql_queries.log -Wait -Tail 50
# Or just open in text editor
notepad c:\temp\log\mysql_queries.log
The general log creates large files quickly. Turn it off when you're done debugging:
SET GLOBAL general_log = 'OFF';
To enable logging every time MySQL starts, edit your MySQL configuration file:
# Usually located at /etc/mysql/my.cnf or /etc/my.cnf
[mysqld]
general_log = 1
general_log_file = /var/log/mysql/query.log
# Usually located at C:\ProgramData\MySQL\MySQL Server 8.0\my.ini
[mysqld]
general_log = 1
general_log_file = "c:\\mysql\\logs\\query.log"
After editing, restart MySQL:
# Linux
sudo service mysql restart
# Windows
net stop mysql80
net start mysql80
See if logging is enabled and where the file is:
SHOW VARIABLES LIKE 'general_log%';
Example output:
+------------------+-----------------------------+
| Variable_name | Value |
+------------------+-----------------------------+
| general_log | ON |
| general_log_file | c:\temp\log\queries.log |
+------------------+-----------------------------+
You can log to a file or to a table:
SET GLOBAL log_output = "FILE";
SET GLOBAL log_output = "TABLE";
SET GLOBAL general_log = 'ON';
-- Queries are logged to mysql.general_log table
SELECT * FROM mysql.general_log ORDER BY event_time DESC LIMIT 10;
SET GLOBAL log_output = "FILE,TABLE";
Important: The general log has a performance impact because it logs every query. Only use it for debugging, never in production.
If you only want to see slow queries (not all queries), use the slow query log instead:
SET GLOBAL slow_query_log = 'ON';
SET GLOBAL slow_query_log_file = '/tmp/slow_queries.log';
SET GLOBAL long_query_time = 1; -- Log queries over 1 second
Since the log can get large, use grep or findstr to filter:
# Find queries on a specific table
grep "FROM users" /tmp/mysql_queries.log
# Find INSERT statements
grep "INSERT INTO" /tmp/mysql_queries.log
# Find queries from a specific connection
grep "Connect.*192.168" /tmp/mysql_queries.log
# Find queries on a specific table
Select-String "FROM users" c:\temp\log\mysql_queries.log
# Find INSERT statements
Select-String "INSERT INTO" c:\temp\log\mysql_queries.log
To prevent the log from growing forever:
SET GLOBAL general_log = 'OFF';
-- Now rename or delete the log file
SET GLOBAL general_log = 'ON'; -- Creates new file
Create /etc/logrotate.d/mysql:
/var/log/mysql/query.log {
daily
rotate 7
missingok
compress
delaycompress
notifempty
create 640 mysql mysql
postrotate
mysql -e "SET GLOBAL general_log = 'OFF'; SET GLOBAL general_log = 'ON';"
endscript
}
MySQL can't write to the log file location:
# Linux - ensure mysql user can write
sudo chown mysql:mysql /var/log/mysql/
sudo chmod 755 /var/log/mysql/
# Windows - ensure MySQL service has write permissions to the folder
The directory doesn't exist:
# Create the directory first
# Linux
sudo mkdir -p /var/log/mysql/
# Windows
md c:\temp\log\
# Enable logging
SET GLOBAL log_output = "FILE";
SET GLOBAL general_log_file = "/path/to/query.log";
SET GLOBAL general_log = 'ON';
# Check status
SHOW VARIABLES LIKE 'general_log%';
# Disable logging
SET GLOBAL general_log = 'OFF';
# View log (Linux)
tail -f /path/to/query.log
# View log (Windows)
Get-Content c:\path\to\query.log -Wait -Tail 50
Please donate if helpful