默认情况下,通用查询日志功能是关闭的。可以通过以下命令查看通用查询日志是否开启,命令如下:
mysql> SHOW VARIABLES LIKE '%general%';
+------------------+----------------------------------+
| Variable_name | Value |
+------------------+----------------------------------+
| general_log | OFF |
| general_log_file | /usr/local/mysql/data/sztech.log |
+------------------+----------------------------------+
2 rows in set (0.00 sec)
打开通用查询日志功能
mysql> SET GLOBAL general_log=ON;
Query OK, 0 rows affected (0.01 sec)
mysql> SHOW VARIABLES LIKE '%general%';
+------------------+----------------------------------+
| Variable_name | Value |
+------------------+----------------------------------+
| general_log | ON |
| general_log_file | /usr/local/mysql/data/sztech.log |
+------------------+----------------------------------+
2 rows in set (0.01 sec)
mysql> use wangjia;
Database changed
mysql> create table test ( id int(2) not null auto_increment, name varchar(10) not null,sex char(5) not null, primary key (id));
Query OK, 0 rows affected (0.22 sec)
mysql> desc test;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(2) | NO | PRI | NULL | auto_increment |
| name | varchar(10) | NO | | NULL | |
| sex | char(5) | NO | | NULL | |
+-------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
mysql> insert into test values(1,'TOM','man');
Query OK, 1 row affected (0.01 sec)
mysql> insert into test values(2,'lisa','woman');
Query OK, 1 row affected (0.02 sec)
mysql> select * from test;
+----+------+-------+
| id | name | sex |
+----+------+-------+
| 1 | TOM | man |
| 2 | lisa | woman |
+----+------+-------+
2 rows in set (0.00 sec)
执行成功后,打开通用查询日志,这里日志名称为 sztech.log,下面是通用查询日志中的部分内容。
[root@sztech ~]# cd /usr/local/mysql/data
[root@sztech data]# more sztech.log
/usr/local/mysql/bin/mysqld, Version: 8.0.11 (MySQL Community Server - GPL). started with:
Tcp port: 3306 Unix socket: /usr/local/mysql/mysql.sock
Time Id Command Argument
2023-03-20T06:58:33.100434Z 9 Query SHOW VARIABLES LIKE '%general%'
2023-03-23T00:22:33.300745Z 13 Connect root@localhost on using Socket
2023-03-23T00:22:33.302629Z 13 Query select @@version_comment limit 1
2023-03-23T00:22:35.949440Z 13 Query SHOW binary logs
2023-03-23T00:22:50.805402Z 13 Query SHOW master status
2023-03-23T00:36:10.165082Z 13 Query create database wangjia
2023-03-23T00:36:24.421360Z 13 Query SELECT DATABASE()
2023-03-23T00:36:24.422176Z 13 Init DB wangjia
2023-03-23T00:36:24.423616Z 13 Query show databases
2023-03-23T00:36:24.436669Z 13 Query show tables
2023-03-23T00:36:36.474743Z 13 Query create table test ( id int(2) not null auto_increment, name varchar(10) not null,sex char(5) not null, prim
ary key (id))
2023-03-23T00:36:48.569634Z 13 Query desc test
2023-03-23T00:37:02.378065Z 13 Query insert into test values(1,'TOM','man')
2023-03-23T00:37:12.457736Z 13 Query insert into test values(2,'lisa','woman')
2023-03-23T00:37:20.090594Z 13 Query select * from test
[root@sztech data]#
可以看出,该日志非常清晰地记录了客户端的所有行为。
关闭通用日志
通用查询日志启动后,可以通过两种方法停止该日志。一种是将 MySQL 配置文件中的相关配置注释掉,然后重启服务器,来停止通用查询日志。具体内容如下:
[mysqld]
#log=dir\filename
上述方法需要重启 MySQL 服务器,这在某些场景,比如有业务量访问的情况下是不允许的,这时可以通过另一种方法来动态地控制通用查询日志的开启和关闭。
设置 MySQL 的环境变量 general_log 为关闭状态可以停止该日志,示例如下:mysql> SET GLOBAL general_log=off;Query OK, 0 rows affected (0.00 sec)
mysql> SHOW VARIABLES LIKE '%general_log%' \G;
*************************** 1. row ***************************
Variable_name: general_log
Value: OFF
*************************** 2. row ***************************
Variable_name: general_log_file
Value: /usr/local/mysql/data/sztech.log
2 rows in set (0.01 sec)