MySQL Tutorial
MySQL Installation and Configuration
MySQL Database Operations
Database Design
MySQL Data Types
MySQL Storage Engines
MySQL Basic Operations of Tables
MySQL Constraints
MySQL Operators
MySQL Function
MySQL Manipulate Table Data
MySQL View
MySQL Indexes
MySQL Stored Procedure
MySQL Trigger
MySQL Transactions
MySQL Character Set
MySQL User Management
MySQL Database Backup and Recovery
MySQL Log
MySQL Performance Optimization
The MySQL General Query Log is a log file where the MySQL server records established client connections and statements received from clients. It provides a useful means to monitor and audit the queries executed on your MySQL server.
Here's a brief tutorial on how to configure and use the MySQL General Query Log.
Configuring the General Query Log
The General Query Log is not enabled by default. You can enable it by setting the general_log
variable to ON
and specifying a file or table to store the log data in the MySQL configuration file (my.cnf or my.ini).
Here's an example of how to configure the General Query Log to log to a file:
[mysqld] general_log = ON general_log_file = /var/log/mysql/mysql-general.log
In this example, MySQL will write general query logs to /var/log/mysql/mysql-general.log
. You can replace this path with the desired location on your system.
You can also log to a table in the MySQL database by setting log_output
to TABLE
:
[mysqld] general_log = ON log_output = TABLE
In this configuration, MySQL will log to a table named general_log
in the mysql
database.
Viewing the General Query Log
To view the general query log when logging to a file, you can use any text editor or command-line tools like 'cat', 'less', or 'tail':
tail /var/log/mysql/mysql-general.log
This command will show the last few lines of the general query log.
If you're logging to a table, you can use a SELECT statement:
SELECT * FROM mysql.general_log;
Interpreting the General Query Log
Here's an example of what a general query log entry might look like when logging to a file:
220509 8:45:57 7 Connect root@localhost on 7 Query SELECT DATABASE() 7 Query show tables 7 Quit
In this example, you can see the date and time of the event (220509 8:45:57
), the thread ID that executed the statement (7
), the command type (Connect
, Query
, Quit
), and the argument (e.g., the query).
Managing the General Query Log
The General Query Log can grow quite large on a busy server, so it's important to implement log rotation to manage its size. Many systems use the 'logrotate' utility for this purpose.
Additionally, logging every query can cause a performance overhead, particularly on a busy server or with complex queries. Therefore, it's usually a good idea to turn on the general query log only when you're debugging a specific issue.
Note: Modifying the MySQL configuration file and viewing the log file typically require administrative privileges. Be careful when modifying configuration files and always back up any files before you modify them.
Enable MySQL General Query Log:
[mysqld] general_log = 1 general_log_file = /path/to/general-query.log
Viewing General Query Log in MySQL:
tail /path/to/general-query.log
cat /path/to/general-query.log
MySQL General Query Log location:
/var/log/mysql/general-query.log
Disable General Query Log in MySQL:
[mysqld] general_log = 0
Rotating and purging General Query Logs:
FLUSH LOGS;
PURGE BINARY LOGS
command:PURGE BINARY LOGS TO 'mysql-bin.000003';
Filtering queries in MySQL General Query Log:
SELECT * FROM information_schema.processlist WHERE info LIKE 'SELECT%';