下面方式是在线开启,重启后会失效:
1.mysql社区版没有审计插件,先获取server_audit.so文件,我是先在一台测试服务器上安装了一个mariadb数据库,然后搜索find / -name server_audit.so文件,scp复制出来的,也可以直接去mariadb官网下载一个二进制包找;
2.获取到server_audit.so包后,把文件cp到mysql插件的路径下:
cp /tmp/server_audit.so /usr/local/mysql/lib/plugin/
3.登录mysql安装插件
mysql> INSTALL PLUGIN server_audit SONAME \'server_audit.so\';
Query OK, 0 rows affected (0.00 sec)
4.检查审计功能是否开启,没有开启则开启
mysql> show variables like \'%audit%\';
+-------------------------------+-----------------------+
| Variable_name | Value |
+-------------------------------+-----------------------+
| server_audit_events | |
| server_audit_excl_users | |
| server_audit_file_path | server_audit.log |
| server_audit_file_rotate_now | OFF |
| server_audit_file_rotate_size | 1000000 |
| server_audit_file_rotations | 9 |
| server_audit_incl_users | |
| server_audit_loc_info | |
| server_audit_logging | OFF |
| server_audit_mode | 1 |
| server_audit_output_type | file |
| server_audit_query_log_limit | 1024 |
| server_audit_syslog_facility | LOG_USER |
| server_audit_syslog_ident | mysql-server_auditing |
| server_audit_syslog_info | |
| server_audit_syslog_priority | LOG_INFO |
+-------------------------------+-----------------------+
16 rows in set (0.00 sec)
5.开启审计功能
mysql> set global server_audit_logging=on;
Query OK, 0 rows affected (0.00 sec)
6.用 show variables like \'%audit%\';查看是否已经开启,开启后ON代表开启,OFF代表没有开启
mysql> show variables like \'%audit%\';
+-------------------------------+-----------------------+
| Variable_name | Value |
+-------------------------------+-----------------------+
| server_audit_events | |
| server_audit_excl_users | |
| server_audit_file_path | server_audit.log |
| server_audit_file_rotate_now | OFF |
| server_audit_file_rotate_size | 1000000 |
| server_audit_file_rotations | 9 |
| server_audit_incl_users | |
| server_audit_loc_info | |
| server_audit_logging | ON |
| server_audit_mode | 1 |
| server_audit_output_type | file |
| server_audit_query_log_limit | 1024 |
| server_audit_syslog_facility | LOG_USER |
| server_audit_syslog_ident | mysql-server_auditing |
| server_audit_syslog_info | |
| server_audit_syslog_priority | LOG_INFO |
+-------------------------------+-----------------------+
16 rows in set (0.00 sec)
7.查看审计效果
[root@smiletest mysql]# tail -f server_audit.log
20190903 09:24:18,smiletest,root,localhost,2,0,FAILED_CONNECT,,,1045
20190903 09:24:23,smiletest,root,localhost,3,0,CONNECT,,,0
20190903 09:24:23,smiletest,root,localhost,3,3,QUERY,,\'select @@version_comment limit 1\',0
20190903 09:24:25,smiletest,root,localhost,3,4,QUERY,,\'SHOW VARIABLES LIKE \\'%audit%\\'\',0
20190903 09:24:45,smiletest,root,localhost,3,5,QUERY,,\'show databases\',0
20190903 09:25:12,smiletest,root,localhost,3,0,DISCONNECT,,,0
8.参数说明,都可以通过 set global的方式修改
server_audit_output_type:指定日志输出类型,可为SYSLOG或FILE
server_audit_logging:启动或关闭审计
server_audit_events:指定记录事件的类型,可以用逗号分隔的多个值(connect,query,table),如果开启了查询缓存(query cache),查询直接从查询缓存返回数据,将没有table记录
server_audit_file_path:如server_audit_output_type为FILE,使用该变量设置存储日志的文件,可以指定目录,默认存放在数据目录的server_audit.log文件中
server_audit_file_rotate_size:限制日志文件的大小
server_audit_incl_users:指定哪些用户的活动将记录,connect将不受此变量影响,该变量比server_audit_excl_users优先级高
server_audit_excl_users:该列表的用户行为将不记录,connect将不受该设置影响
案例
只审计test账户其它账户不审计
grant all on *.* to test@\'172.16.1.%\' identified by \'test@123\';
flush privileges;
INSTALL PLUGIN server_audit SONAME \'server_audit.so\';
set global server_audit_logging=on;
set global server_audit_file_rotate_size=10000000; 10M
set global server_audit_incl_users=\'test\';
show variables like \'%audit%\';
下面方式是中断业务开启,再次重启后不会失效:
1.mysql社区版没有审计插件,先获取server_audit.so文件,我是先在一台测试服务器上安装了一个mariadb数据库,然后搜索find / -name server_audit.so文件,scp复制出来的,也可以直接去mariadb官网下载一个二进制包找;
2.获取到server_audit.so包后,把文件cp到mysql插件的路径下:
cp /tmp/server_audit.so /usr/local/mysql/lib/plugin/
3.编辑my.cnf配置
vim /etc/my.cnf
#开启审计功能
server_audit_logging=on
#指定审计日志文件存放路径,这里的路径要注意了,这个文件路径必须经是Mysql权限里
server_audit_file_path =/data/mysql/server_audit.log
server_audit_file_rotate_size=10000000
#防止server_audit 插件被卸载,需要在配置文件中添加
server_audit=FORCE_PLUS_PERMANENT
4.重启mysql服务
/etc/init.d/mysqld restart
5.查看审计功能是否开启
mysql> show variables like \'%audit%\';