MySQL5.7开启查询日志和审计日志
Mysql审计日志
1、下载mariadb-5.5.56-linux-x86_64.tar.gz解压获取server_audit.so插件
下载链接:https://downloads.mariadb.org/mariadb-galera/5.5.56/#file_type=source
2、登录MySQL,执行命令获取MySQL的plugin目录
mysql> SHOW GLOBAL VARIABLES LIKE 'plugin_dir';
+---------------+-------------------------------+
| Variable_name | Value |
+---------------+-------------------------------+
| plugin_dir | /usr/lib64/mysql/plugin/
+---------------+-------------------------------+
1 row in set (0.00 sec)
3、将server_audit.so上传到 /usr/lib64/mysql/plugin/下
4、在命令下安装server_audit.so
1 mysql> INSTALL PLUGIN server_audit SONAME 'server_audit.so';
5、查看变量开启设置情况,默认貌似都是关闭的
mysql> show variables like '%audit%';
+-------------------------------+---------------------------------------------------+
| Variable_name | Value |
+-------------------------------+---------------------------------------------------+
| server_audit_events | CONNECT,QUERY,TABLE,QUERY_DDL,QUERY_DML,QUERY_DCL |
| server_audit_excl_users | |
| server_audit_file_path | /data/mysql/auditlogs/ |
| server_audit_file_rotate_now | ON |
| server_audit_file_rotate_size | 200000000 |
| server_audit_file_rotations | 200 |
| 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 |
+-------------------------------+---------------------------------------------------+
6、编辑my.cnf,添加配置
1 server_audit_events='CONNECT,QUERY,TABLE,QUERY_DDL,QUERY_DML,QUERY_DCL' 2 备注:指定哪些操作被记录到日志文件中 3 server_audit_logging=on 4 server_audit_file_path =/data/mysql/auditlogs/ 5 备注:审计日志存放路径,该路径下会生成一个server_audit.log文件,就会记录相关操作记录了 6 server_audit_file_rotate_size=200000000 7 server_audit_file_rotations=200 8 server_audit_file_rotate_now=ON
注意:server_audit_file_path =/data/mysql/auditlogs/的目录需要开启权限 chmod 777 /data/mysql/auditlogs/ ,并且要关闭SElinux,否则无法生成日志。
7、重启服务,service mysqld restart
登录MySQL后发现,在MySQL环境下执行的任何命令都被记录到/data/mysql/auditlogs/server_audit.log,如果日志文件达到指定的大小,会自动切割
mysql> show variables like '%audit%';
+-------------------------------+---------------------------------------------------+
| Variable_name | Value |
+-------------------------------+---------------------------------------------------+
| server_audit_events | CONNECT,QUERY,TABLE,QUERY_DDL,QUERY_DML,QUERY_DCL |
| server_audit_excl_users | |
| server_audit_file_path | /data/mysql/auditlogs/ |
| server_audit_file_rotate_now | ON |
| server_audit_file_rotate_size | 200000000 |
| server_audit_file_rotations | 200 |
| 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 |
+-------------------------------+---------------------------------------------------+
8、参数说明:
详细请参考:https://mariadb.com/kb/en/mariadb/server_audit-system-variables/
1 server_audit_output_type:指定日志输出类型,可为SYSLOG或FILE 2 server_audit_logging:启动或关闭审计 3 server_audit_events:指定记录事件的类型,可以用逗号分隔的多个值(connect,query,table),如果开启了查询缓存(query cache),查询直接从查询缓存返回数据,将没有table记录 4 server_audit_file_path:如server_audit_output_type为FILE,使用该变量设置存储日志的文件,可以指定目录,默认存放在数据目录的server_audit.log文件中 5 server_audit_file_rotate_size:限制日志文件的大小 6 server_audit_file_rotations:指定日志文件的数量,如果为0日志将从不轮转 7 server_audit_file_rotate_now:强制日志文件轮转 8 server_audit_incl_users:指定哪些用户的活动将记录,connect将不受此变量影响,该变量比server_audit_excl_users优先级高 9 server_audit_syslog_facility:默认为LOG_USER,指定facility 10 server_audit_syslog_ident:设置ident,作为每个syslog记录的一部分 11 server_audit_syslog_info:指定的info字符串将添加到syslog记录 12 server_audit_syslog_priority:定义记录日志的syslogd priority 13 server_audit_excl_users:该列表的用户行为将不记录,connect将不受该设置影响 14 server_audit_mode:标识版本,用于开发测试
9、卸载server_audit
1 mysql> UNINSTALL PLUGIN server_audit; 2 mysql> show variables like '%audit%'; 3 Empty set (0.00 sec)
防止server_audit 插件被卸载,需要在配置文件中添加:
1 [mysqld] 2 server_audit=FORCE_PLUS_PERMANENT
重启MySQL生效
值得注意的是,应该在server_audit插件被安装好,并且已经运行之后添加这些配置,否则过早在配置文件添加这个选项,会导致MySQL发生启动错误!
1 mysql> UNINSTALL PLUGIN server_audit; 2 ERROR 1702 (HY000): Plugin 'server_audit' is force_plus_permanent and can not be unloaded
MySQL查询日志配置
在my.cnf 文件中配置
1 log-output=file 2 general-log=1 3 general-log-file=/path/to/query.log