Log all queries in MySQL

Better Stack Team
Updated on November 23, 2022

In MySQL, it is possible to log all queries that were executed. You can view them as a table or file.

Output query history into a table

To output executed queries into a table, run the following MySQL commands:

SET GLOBAL log_output = 'TABLE';
SET GLOBAL general_log = 'ON';
Copied!

Then, take a look at mysql.general_log table, where you see the query history:

SELECT * FROM mysql.general_log
Copied!

Output query history into a file

To output query history into a file instead of a table, run the following MySQL commands:

SET GLOBAL log_output = "FILE";
SET GLOBAL general_log_file = "/path/to/your/logfile.log";
SET GLOBAL general_log = 'ON';
Copied!

Don’t forget to replace "/path/to/your/logfile.log" with the actual log file you wish to use. Then you can just open the file to see query history. To see the last 10 line, run the following command:

tail /path/to/your/logfile.log
Copied!
Got an article suggestion? Let us know
Explore more
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

We are hiring.

Software is our way of making the world a tiny bit better. We build tools for the makers of tomorrow.

Explore all positions →

Reliability is the
ultimate feature

Delightful observability tools that turn your logs & monitoring into a secret weapon for shipping better software faster.

Explore Better Stack