Skip to content

日志审计表

xiaoboluo768 edited this page Jun 7, 2020 · 2 revisions
  • 加载了audit log审计插件之后,MySQL Server就会创建audit_log_filter, audit_log_user表
  • 参考链接:
  • 安装插件
# MySQL 5.7.13版本之前的安装方法
root@localhost : (none) 08:58:55> INSTALL PLUGIN audit_log SONAME 'audit_log.so';
Query OK, 0 rows affected (0.00 sec)

# MySQL 5.7.13及其之后版本的安装方法(MySQL 5.7.13版本之后,安装审计日志插件不会随同创建表和规则信息,所以需要使用元数据SQL导入的方式安装),sql文件在你的$basedir/share目录下
## 先查看一下该SQL脚本的内容,可以看到该表是创建在mysql 系统字典库下的,创建了2帐表(默认情况下这两张表在5.7.21版本之前为MyISAM引擎,5.7.21开始为InnoDB引擎,当然这里你可以自行修改建表语句。另外,如果插件已经安装好了,可以自行使用alter table xx engine=innodb;语句修改引擎),安装了插件,创建了5个UDF函数
[root@localhost ~]# cat /usr/local/mysql/share/audit_log_filter_linux_install.sql
# Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.

USE mysql;

CREATE TABLE IF NOT EXISTS audit_log_filter(NAME VARCHAR(64) BINARY NOT NULL PRIMARY KEY, FILTER JSON NOT NULL) engine=InnoDB;
CREATE TABLE IF NOT EXISTS audit_log_user(USER VARCHAR(16) BINARY NOT NULL, HOST VARCHAR(60) BINARY NOT NULL, FILTERNAME VARCHAR(64) BINARY NOT NULL, PRIMARY KEY (USER, HOST), FOREIGN KEY (FILTERNAME) REFERENCES mysql.audit_log_filter(NAME)) engine=InnoDB;

INSTALL PLUGIN audit_log SONAME 'audit_log.so';

CREATE FUNCTION audit_log_filter_set_filter RETURNS STRING SONAME 'audit_log.so';
CREATE FUNCTION audit_log_filter_remove_filter RETURNS STRING SONAME 'audit_log.so';
CREATE FUNCTION audit_log_filter_set_user RETURNS STRING SONAME 'audit_log.so';
CREATE FUNCTION audit_log_filter_remove_user RETURNS STRING SONAME 'audit_log.so';
CREATE FUNCTION audit_log_filter_flush RETURNS STRING SONAME 'audit_log.so';

SELECT audit_log_filter_flush() AS 'Result';

## 导入该SQL脚本文件
[root@localhost ~]# mysql -u root -pletsg0 < /usr/local/mysql/share/audit_log_filter_linux_install.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
Result
OK

## 查看是否安装成功
root@localhost : mysql 09:48:59> SELECT PLUGIN_NAME, PLUGIN_STATUS FROM INFORMATION_SCHEMA.PLUGINS WHERE PLUGIN_NAME LIKE 'audit%';
+-------------+---------------+
| PLUGIN_NAME | PLUGIN_STATUS |
+-------------+---------------+
| audit_log | ACTIVE |
+-------------+---------------+
1 row in set (0.00 sec)

# 默认情况下,基于规则的过滤器不为任何用户提供审计功能(与5.7.13版本之前不同),如果需要使用基于规则的过滤器,可以使用如下语句创建一个基于规则的过滤器,并且把这个过滤器应用在所有用户上(%代表所有用户)
## 创建一个基于规则的过滤器
root@localhost : mysql 09:52:36> SELECT audit_log_filter_set_filter('log_all', '{ "filter": { "log": true } }');
+-------------------------------------------------------------------------+
| audit_log_filter_set_filter('log_all', '{ "filter": { "log": true } }') |
+-------------------------------------------------------------------------+
| OK |
+-------------------------------------------------------------------------+
1 row in set (0.00 sec)

# 把这个基于规则的过滤器应用在所有用户上(如果没有单独为某个用户配置过滤器,则会使用这个默认用户的过滤器)
root@localhost : mysql 09:55:01> SELECT audit_log_filter_set_user('%', 'log_all');
+-------------------------------------------+
| audit_log_filter_set_user('%', 'log_all') |
+-------------------------------------------+
| OK |
+-------------------------------------------+
1 row in set (0.00 sec)
  • 安装审计插件之后会新增如下一些系统变量和状态变量
# 系统参数变量
root@localhost : mysql 09:32:03> show variables like '%audit%';
+-----------------------------+--------------+
| Variable_name | Value |
+-----------------------------+--------------+
| audit_log_buffer_size | 1048576 |
| audit_log_connection_policy | ALL |
| audit_log_current_session | OFF |
| audit_log_exclude_accounts | |
| audit_log_file | audit.log |
| audit_log_filter_id | 0 |
| audit_log_flush | OFF |
| audit_log_format | NEW |
| audit_log_include_accounts | |
| audit_log_policy | ALL |
| audit_log_rotate_on_size | 0 |
| audit_log_statement_policy | ALL |
| audit_log_strategy | ASYNCHRONOUS |
+-----------------------------+--------------+
13 rows in set (0.01 sec)

# 系统状态变量
root@localhost : mysql 09:34:20> show status like '%audit%';
+-------------------------------+-------+
| Variable_name | Value |
+-------------------------------+-------+
| audit_log_current_size | 13982 |
| audit_log_event_max_drop_size | 0 |
| audit_log_events | 1 |
| audit_log_events_buffered | 0 |
| audit_log_events_filtered | 0 |
| audit_log_events_lost | 0 |
| audit_log_events_written | 1 |
| audit_log_total_size | 13982 |
| audit_log_write_waits | 0 |
+-------------------------------+-------+
9 rows in set (0.00 sec)
  • 如果需要卸载审计日志插件,可以使用如下语句动态删除
DROP TABLE IF EXISTS mysql.audit_log_filter;
DROP TABLE IF EXISTS mysql.audit_log_user;
UNINSTALL PLUGIN audit_log;
DROP FUNCTION audit_log_filter_set_filter;
DROP FUNCTION audit_log_filter_remove_filter;
DROP FUNCTION audit_log_filter_set_user;
DROP FUNCTION audit_log_filter_remove_user;
DROP FUNCTION audit_log_filter_flush;
# 以下UDF MySQL 5.7.x版本中不存在这个函数,在8.0中支持
DROP FUNCTION audit_log_encryption_password_get; 
DROP FUNCTION audit_log_encryption_password_set; 
DROP FUNCTION audit_log_read;
DROP FUNCTION audit_log_read_bookmark; 
  • 审计日志记录内容示例
 <AUDIT_RECORD>
  <TIMESTAMP>2018-06-23T14:51:13 UTC</TIMESTAMP>
  <RECORD_ID>20421_2018-06-23T14:50:46</RECORD_ID>
  <NAME>Query</NAME>
  <CONNECTION_ID>3</CONNECTION_ID>
  <STATUS>0</STATUS>
  <STATUS_CODE>0</STATUS_CODE>
  <USER>root[root] @ localhost []</USER>
  <OS_LOGIN/>
  <HOST>localhost</HOST>
  <IP/>
  <COMMAND_CLASS>show_status</COMMAND_CLASS>
  <SQLTEXT>show status like '%audit%'</SQLTEXT>
 </AUDIT_RECORD>
  • PS:审计日志插件安装之后,即使禁用,也会有一定开销,所以,除非你确定你要使用,否则请不要安装
  • 使用启动选项audit_log=off|on来控制插件的启用与禁用,默认启用
  • 为了防止审计日志插件在运行过程中被使用SQL语句卸载,可以在配置文件中使用选项 audit-log=FORCE_PLUS_PERMANENT 来禁止动态删除
  • 审计日志内容默认记录在datadir下的audit.log日志文件中
  • 由于该插件是企业版功能,加上该插件使用起来较为复杂,有需要的请自行研究,详见链接:https://dev.mysql.com/doc/refman/5.7/en/audit-log-filtering.html

上一篇:Client 端帮助文档 |下一篇:audit_log_filter表

Clone this wiki locally