Mysql audit插件安装
Mysql audit插件可以记录用户及应用对数据库的每一个操作,让我们在需要的时候可以很方便的查看相关审计日志,排查哪个用户在哪个事件端进行了什么操作。在默认情况下会记录任何语句,有语法错误的不会记录
插件安装
插件下载
地址:https://github.com/mcafee/mysql-audit/releases 选择对应版本数据库下载即可
解压安装
# unzip audit-plugin-mysql-5.7-1.1.7-913-linux-x86_64.zip Archive: audit-plugin-mysql-5.7-1.1.7-913-linux-x86_64.zip creating: audit-plugin-mysql-5.7-1.1.7-913/ creating: audit-plugin-mysql-5.7-1.1.7-913/lib/ inflating: audit-plugin-mysql-5.7-1.1.7-913/lib/libaudit_plugin.so inflating: audit-plugin-mysql-5.7-1.1.7-913/COPYING inflating: audit-plugin-mysql-5.7-1.1.7-913/THIRDPARTY.txt inflating: audit-plugin-mysql-5.7-1.1.7-913/README.txt inflating: audit-plugin-mysql-5.7-1.1.7-913/plugin-name.txt creating: audit-plugin-mysql-5.7-1.1.7-913/utils/
查看Mysq插件目录
mysql> show variables like 'plugin_dir'; +---------------+--------------------------+ | Variable_name | Value | +---------------+--------------------------+ | plugin_dir | /usr/lib64/mysql/plugin/ | +---------------+--------------------------+ 1 row in set (0.00 sec)
复制库文件到mysql插件目录下
cd audit-plugin-mysql-5.7-1.1.7-913/lib/libaudit_plugin.so /usr/lib64/mysql/plugin/ chmod +x /usr/lib64/mysql/plugin/libaudit_plugin.so
进入mysql命令行,安装插件
> install plugin audit soname 'libaudit_plugin.so'; Query OK, 0 rows affected (0.06 sec)
查看版本
mysql> show global status like '%audit%'; +------------------------+-----------+ | Variable_name | Value | +------------------------+-----------+ | Audit_protocol_version | 1.0 | | Audit_version | 1.1.7-913 | +------------------------+-----------+
开启Audit功能
> SET GLOBAL audit_json_file=ON; Query OK, 0 rows affected (0.00 sec)
其他参数:
1. audit_json_file 是否开启audit功能。 2. audit_json_log_file 记录文件的路径和名称信息(默认放在mysql数据目录下)。 3. audit_record_cmds audit记录的命令,默认为记录所有命令。可以设置为任意dml、dcl、ddl的组合。如:audit_record_cmds=select,insert,delete,update。还可以在线设置set global audit_record_cmds=NULL。(表示记录所有命令)
audit日志分割
日志使用logrotate工具定时来分割
1、添加配置信息
cat /etc/logrotate.d/audit /var/lib/mysql/mysql-audit.json { create 600 mysql mysql notifempty daily rotate 3000 missingok minsize 200M compress postrotate # just if mysqld is really running if test -x /usr/bin/mysqladmin && \ /usr/bin/mysqladmin ping -uroot -pxxxxxxxxxx &>/dev/null then echo "test" /bin/mysql -e "set global audit_json_file_flush=on" -uroot -pxxxxxxxxxx fi endscript }
2、添加定时任务分割
*/5 * * * * /usr/sbin/logrotate -f /etc/logrotate.d/audit
3、定时讲备份的日志转移到备份目录
cat /opt/move_mysqlaudit_log.sh #!/bin/bash Yesterday=`date +%Y-%m-%d -d "1 days ago"` BACKUP_DIR=/data/backup/mysql_audit [[ ! -e ${BACKUP_DIR}/${Yesterday} ]] && mkdir ${BACKUP_DIR}/${Yesterday} find /var/lib/mysql/ -name mysql-audit.json.*.gz -exec mv {} ${BACKUP_DIR}/${Yesterday} \;