Detailed explanation of query log configuration in Mysql

query log

The query log in MySQL is saved in a text file, which can record all data operations in MySQL.


Enable query log

By default, MySQL does not enable the query log. If you need to enable the query log, you need to configure it under my.cnfthe file or my.inifile [mysqld]option . For example, configure to enable MySQL query log:


[mysqld]
general_log = 1
general_log_file = /data/mysql/log/general_log/general_statement.log
log_output = FILE

Various configuration descriptions are as follows:

· general_log: indicates whether to enable the query log. If this item is set to 1or without any value, the query log can be enabled; if it is set to 0 or this item is not configured in my.cnfthe file or my.inifile , the query log will not be enabled.

: general_log_fileQuery the file directory of the log. The author configures the full path of the log here.

: log_outputIndicates the storage method of the log, which can have 3 values, TABLEwhich means that the query log is stored in the data table; FILEthat the query log is saved in a file; NONE means that the log information is not saved in the data table and file.

Note : When the query log is enabled, general_log_fileif log_outputthe value of the specified option and option is not displayed, MySQL will save the query log to the directory specified by the DATADIR option (that is, the data directory in the database). The default file name is, among them, host_name.loghost_name is the hostname of MySQL.

Before configuring and opening the query log, first check the file information in /data/mysql/log/general_logthe directory .

[root@binghe150 ~]# 
[root@binghe150 ~]# ll /data/mysql/log/general_log/
total 0

When the MySQL query log is not configured, /data/mysql/log/general_log/no files exist in the directory.

After the query log configuration is complete, you need to restart the MySQL service to take effect.

[root@binghe150 ~]# service mysqld restart
Shutting down MySQL..... SUCCESS! 
Starting MySQL........ SUCCESS!

You can also specify to enable MySQL query logs on the MySQL command line.

mysql> SET GLOBAL general_log = 1;
Query OK, 0 rows affected (0.01 sec)
mysql> SET GLOBAL general_log_file = '/data/mysql/log/general_log/general_statement.log'; 
Query OK, 0 rows affected (0.00 sec)
mysql> SET GLOBAL log_output = 'file';
Query OK, 0 rows affected (0.00 sec)

At this point, check the files in /data/mysql/log/general_log/the directory .

[root@binghe150 ~]# ll /data/mysql/log/general_log/
total 4
-rw-r----- 1 mysql mysql 547 Jan 17 11:39 general_statement.log

When the query log configuration is enabled, MySQL will automatically create the log file specified by the general_log_file option.

view query log

If the log_output option is configured to save the query log to a file, the format of the log file is plain text, and you can directly view the contents of the log file.


cat /data/mysql/log/general_log/general_statement.log

mysql query logging
Information about all SQL statements is recorded in the query log.

delete query log

  1. Find the log directory directly to delete the log record.
rm -rf /data/mysql/log/general_log/general_statement.log
  1. refresh log
  • The log records before refreshing are as follows:
    insert image description here

  • Refresh query log

    • You can execute the following command on the MySQL command line to refresh logs.
    mysql> FLUSH LOGS;
    Query OK, 0 rows affected (0.02 sec)
    
    • You can also execute the following command on the server command line to refresh the log.
    [root@binghe150 ~]# mysqladmin -uroot -p flush-logs
    Enter password:
    

The log is refreshed successfully, check the files in /data/mysql/log/general_logthe directory .

  • After executing the refresh command, the log records are as follows:
    insert image description here

Turn off query log

It is relatively simple to turn off the query log. You only need my.cnfto configure the option to 0 under the optionmy.ini of the file or file , or delete the option .[mysqld]general_loggeneral_log

[mysqld]
general_log = 0

After the configuration is complete, restart MySQL to take effect.

You can also execute the following command on the MySQL command line to close the query log.

mysql> SET GLOBAL general_log = 0;
Query OK, 0 rows affected (0.00 sec)

After closing the query log and deleting the query log, and then performing the operation of refreshing the log, MySQL will no longer recreate the query log file.

Guess you like

Origin blog.csdn.net/weixin_36754290/article/details/129201603