How to show the last queries executed on MySQL?

Better Stack Team
Updated on November 23, 2022

In MySQL, it is possible to show the last 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';

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

 
SELECT * FROM mysql.general_log

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';

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