English

Show MySQL Queries

This tutorial will guide you on how to show MySQL queries in a log file, in order to make any executed query available for further analysis.

Once you’ve logged into your MySQL instance, run the following command to gather information about the location of your log file:

mysql> SHOW VARIABLES LIKE 'general_log%';
+------------------+-----------------------------+
| Variable_name    | Value                       |
+------------------+-----------------------------+
| general_log      | OFF                         |
| general_log_file | /var/lib/mysql/centos7.log  |
+------------------+-----------------------------+

The above command shows that the query logging is OFF through the general_log variable, also that the location of the log file is /var/lib/mysql/centos7.log.

Run the query below to switch the query logging on:

mysql> SET GLOBAL general_log = 'ON';

Then use your favourite method to view the log file whilst your database processes the MySQL queries:

[root@centos7 ~]# tail -f /var/lib/mysql/centos7.log
 SELECT `user`.* FROM `user` WHERE `user`.`status` = 1 ORDER BY `user`.`id_user` DESC LIMIT 50
 SELECT  `user`.* FROM `user` INNER JOIN `project` ON `user`.`id_user` = `project`.`id_user_owner`
 ...

Please note that this approach consumes a huge amount of disk space and reduce the database performance, so it is recommended to turn it off when your analysis is done:

mysql> SET GLOBAL general_log = 'OFF';

Hope it helps.
Cya!

bgasparotto

Recent Posts

Python function decorator

This guide will show you how to create a Python function decorator with a few…

2 years ago

Got permission denied while trying to connect to the Docker daemon socket

This guide will show you how to fix the error Got permission denied while trying…

2 years ago

Python virtual environment on Intellij IDEA

This guide will show you how to create a Python virtual environment on Intellij IDEA…

2 years ago

Find and kill processes on Linux and Mac by port number

This tutorial will quickly show you how to to find and kill processes on Linux,…

2 years ago

Python: Relocation R_X86_64_PC32 against symbol can not be used when making a shared object Error

This guide shows a possible solution for Python error Relocation R_X86_64_PC32 against symbol can not…

2 years ago

Kubernetes useful commands

I condensed below a cheat sheet of Kubernetes useful commands. I will keep updating this…

2 years ago