Pacific Blue Software Logo

How to Enable MySQL Query Logging

How to enable MySQL query logging?

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.

Quick Enable (Session Only)

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.

Windows Path (Important!)

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\\

What Gets Logged

The general log captures:

Viewing the Log

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

Turn It Off

The general log creates large files quickly. Turn it off when you're done debugging:

SET GLOBAL general_log = 'OFF';

Make It Permanent

To enable logging every time MySQL starts, edit your MySQL configuration file:

Linux/Mac - my.cnf

# Usually located at /etc/mysql/my.cnf or /etc/my.cnf
[mysqld]
general_log = 1
general_log_file = /var/log/mysql/query.log

Windows - my.ini

# 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

Checking Current Status

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     |
+------------------+-----------------------------+

Log Output Options

You can log to a file or to a table:

File Output (Recommended)

SET GLOBAL log_output = "FILE";

Table Output

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;

Both File and Table

SET GLOBAL log_output = "FILE,TABLE";

Performance Warning

Important: The general log has a performance impact because it logs every query. Only use it for debugging, never in production.

Alternative: Slow Query Log

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

Filtering the Log

Since the log can get large, use grep or findstr to filter:

Linux/Mac

# 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

Windows PowerShell

# 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

Log Rotation

To prevent the log from growing forever:

Manual Rotation

SET GLOBAL general_log = 'OFF';
-- Now rename or delete the log file
SET GLOBAL general_log = 'ON';  -- Creates new file

Automatic Rotation (Linux)

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
}

Common Issues

Permission Denied

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

Log File Not Created

The directory doesn't exist:

# Create the directory first
# Linux
sudo mkdir -p /var/log/mysql/

# Windows
md c:\temp\log\

Quick Reference

# 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

Enable MySQL General Query Log for Debugging


Articles for Developers
Database Articles
How to Raise Exceptions in MySQL Triggers

If you found this useful, then please consider making a donation.

paypal
QR Code for donation Please donate if helpful