mysql 开启数据库审计选项一使用全局设置,使用root在命令登录,在linux和windows上都一样。如下命令:

D:\Install\mysql-8.0.22-winx64\bin>mysql -uroot -p
Enter password: ******
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 330
Server version: 8.0.22 MySQL Community Server - GPL

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> set global general_log = on;
Query OK, 0 rows affected (0.00 sec)

mysql> set global log_output = 'TABLE';
Query OK, 0 rows affected (0.00 sec)

 

这样审计功能就开启了,下面我们来随机执行一些查询语句,然后查看数据库日志。
我们直接使用如下语句查询数据 :

SELECT * FROM mysql.general_log; 

这个可能会报一个错误  错误代码: 1142 ,当我们使用非root账号时,查询这个表没有权限,这个时候需要root用户登录命令授权。
 

mysql> use mysql;
Database changed
mysql> grant all privileges on mysql.general_log to 'User'@'%';
Query OK, 0 rows affected (0.19 sec)

关闭这个功能使用以下命令:

set global general_log = off;

另外说一个这个日志输出,一般常用的有两个选项可选,(TABLE,FILE)也可同时设置两个值如下:

 set global log_output='TABLE,FILE'

最后赋上查询数据的结果:

sql

使用完毕后记得关闭该功能额。

链接