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

MySQL General Query Log

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.

  1. Enable MySQL General Query Log:

    • Description: The General Query Log records all queries executed on the MySQL server.
    • Enable in my.cnf or my.ini:
      [mysqld]
      general_log = 1
      general_log_file = /path/to/general-query.log
      
    • Restart the MySQL server.
  2. Viewing General Query Log in MySQL:

    • Description: Use various methods to view the contents of the MySQL General Query Log.
    • Command to view the last few lines (Linux):
      tail /path/to/general-query.log
      
    • Command to view the entire log:
      cat /path/to/general-query.log
      
  3. MySQL General Query Log location:

    • Description: The location of the General Query Log depends on the configuration in my.cnf or my.ini.
    • Default location (Linux):
      /var/log/mysql/general-query.log
      
  4. Disable General Query Log in MySQL:

    • Description: Disable the MySQL General Query Log when it's no longer needed.
    • Disable in my.cnf or my.ini:
      [mysqld]
      general_log = 0
      
  5. Rotating and purging General Query Logs:

    • Description: Rotate and purge old General Query Logs to manage disk space.
    • Use the following SQL command to rotate logs:
      FLUSH LOGS;
      
    • To purge old logs, you can use the PURGE BINARY LOGS command:
      PURGE BINARY LOGS TO 'mysql-bin.000003';
      
  6. Filtering queries in MySQL General Query Log:

    • Description: Filter queries in the General Query Log based on specific criteria.
    • Example to filter SELECT queries:
      SELECT * FROM information_schema.processlist WHERE info LIKE 'SELECT%';