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.cnf
the file or my.ini
file [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 1
or without any value, the query log can be enabled; if it is set to 0 or this item is not configured in my.cnf
the file or my.ini
file , the query log will not be enabled.
: general_log_file
Query the file directory of the log. The author configures the full path of the log here.
: log_output
Indicates the storage method of the log, which can have 3 values, TABLE
which means that the query log is stored in the data table; FILE
that 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_file
if log_output
the 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.log
host_name is the hostname of MySQL.
Before configuring and opening the query log, first check the file information in /data/mysql/log/general_log
the 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
Information about all SQL statements is recorded in the query log.
delete query log
- Find the log directory directly to delete the log record.
rm -rf /data/mysql/log/general_log/general_statement.log
- refresh log
-
The log records before refreshing are as follows:
-
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_log
the directory .
- After executing the refresh command, the log records are as follows:
Turn off query log
It is relatively simple to turn off the query log. You only need my.cnf
to configure the option to 0 under the optionmy.ini
of the file or file , or delete the option .[mysqld]
general_log
general_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.