How to view queries executed in MySQL?
Hi all, This is a small post which I would like to share my finding with you. In one of my software project, There was a small peace of software which frequently dealing with database. (I got that clue from MySQL workbench dashboard) I was so curious about that because I wanted to know what does that do and what does it read/write from/to the database.
Finally I found a solution which helps me to find out that ! The solution was MySQL log. First you will have to enable the log. If you are using a version below 5.1.12, you will have to go to configuration level (my.cnf). But fortunately, If you are using MySQL >= 5.1.12 you can do that on the fly with MySQL global variables and does not need to restart the server as well.
This is how I did (in MySQL >= 5.1.12),
There are two options which can be done.
- Writing log to a table
- Writing log to a file.
By default, MySQL logging is disabled, because of the performance reasons. So we have to enable it first.
Writing log to a table
Execute following lines in MySQL console
SET GLOBAL log_output = 'TABLE';
SET GLOBAL general_log = 'ON';
Thats it!, Now MySQL will start to log all queries into a table called mysql.general_log table. So you can search the log like below
SELECT * FROM mysql.general_log;
Writing log to a file
Execute following lines in MySQL console
SET GLOBAL log_output = 'FILE';
SET GLOBAL general_log_file = '/specific/path/to/mylog.log';
SET GLOBAL general_log = 'ON';
Now mysql will start to log all queries into the log file you configured.
How to turn off logging.
Execute following line in mysql console
SET GLOBAL general_log = 'OFF';
Then MySQL will immediately stops writing log
Note
If you restarts the MySQL server, the configurations you made above will be discarded and reloads the default values. You can always see current values of global variables with following queries
SHOW GLOBAL VARIABLES LIKE 'general_log';
SHOW GLOBAL VARIABLES LIKE 'log_output';
SHOW GLOBAL VARIABLES LIKE 'general_log_file';
Comments
Post a Comment