MySQL数据库审计插件,最经济的方案莫过于安装audit插件
一、插件包下载
根据自己的数据库的版本,选择合适的插件包版本。
二、审计日志的路径准备
创建挂载点:
mkdir /mysql_audit_logs
挂载审计的存储,我使用的是NAS
mount NAS地址 /mysql_audit_logs
创建审计日志路径:
mkdir -p /mysql_audit_logs/audit_logs/
chown -R mysql.mysql /mysql_audit_logs/audit_logs/
三、文件放置
确定数据库的插件路径
root@mysqldb: [(none)]> show variables like '%plugin%';
+-----------------------------------------------+------------------------------------+
| Variable_name | Value |
+-----------------------------------------------+------------------------------------+
| audit_uninstall_plugin | OFF |
| default_authentication_plugin | mysql_native_password |
| plugin_dir | /data/mysql/mysqldb/lib/plugin/ |
| replication_optimize_for_static_plugin_config | OFF |
+-----------------------------------------------+------------------------------------+
拷贝文件至数据库
cd /data/soft
rz audit-plugin-mysql-5.7-1.1.10-980-linux-x86_64.zip
unzip audit-plugin-mysql-5.7-1.1.10-980-linux-x86_64.zip
cd audit-plugin-mysql-5.7-1.1.10-980/lib/libaudit_plugin.so /data/mysql/mysqldb/lib/plugin/
四、数据库安装插件
安装
INSTALL PLUGIN AUDIT SONAME 'libaudit_plugin.so';
检查
root@mysqldb: [(none)]> show plugins;
+----------------------------+----------+--------------------+--------------------+---------+
| Name | Status | Type | Library | License |
+----------------------------+----------+--------------------+--------------------+---------+
| AUDIT | ACTIVE | AUDIT | libaudit_plugin.so | GPL |
+----------------------------+----------+--------------------+--------------------+---------+
五、审计配置
自定义审计日志路径和文件名
set global audit_json_log_file='/mysql_audit_logs/audit_logs/mysql-audit.json';
自定义审计内容
set global audit_password_masking_cmds = 'insert,delete,update,create,drop,alter,grant,truncate,select';
开启审计日志
SET GLOBAL audit_json_file=ON;
关闭审计日志
SET GLOBAL audit_json_file=OFF;
六、永久配置审计
将[
#mysql-audit
audit_json_log_file = /mysql_audit_logs/audit_logs/mysql-audit.json
audit_json_file = on
plugin-load=AUDIT=libaudit_plugin.so
audit_record_cmds = 'insert,delete,update,create,drop,alter,grant,truncate,select'
]
追加写入/etc/my.cnf
七、后续管理
审计日志每日切割文件
通过shell脚本实现
vi audit_log_cut.sh
i#!/bin/bash
source /etc/profile
yesterday=`date -d yesterday +%Y%m%d`
dir_news="/mysql_audit_logs/audit_logs"
files="mysql-audit.json"
files_new=$files"_bak_"$yesterday
mysql -uroot -p'' -e "SET GLOBAL audit_json_file=OFF;"
mv $dir_news/$files $dir_news/$files_new
mysql -uroot -p'' -e "SET GLOBAL audit_json_file=ON;"
历史审计日志压缩&删除
通过shell脚本实现
vi zip_audit_logs.sh
i#!/bin/bash
# 日志存储目录
audit_logs="/mysql_audit_logs/audit_logs/"
# 获取文件
files=$(ls ${audit_logs})
# 压缩日志
for file in $files
do
if [[ $file =~ [0-9]{8} ]] && [[ ! $file == *.zip ]] && [[ $file == *bak* ]]; then
# 获取文件名日期
#file_date="${file##*_}"
file_date="${BASH_REMATCH}"
# 获取当前日期
current_date=$(date +%Y%m%d)
# 将日期转换为秒数
file_date_seconds=$(date -d "$file_date" +%s)
current_seconds=$(date -d "$current_date" +%s)
# 计算时间差(以天为单位)
time_diff=$(( (current_seconds - file_date_seconds) / 86400 ))
if [ $time_diff -gt 3 ]; then
echo "压缩日志 $file"
zip -m ${audit_logs}${file}.zip ${audit_logs}$file
fi
fi
done
# 删除大于90天的压缩日志
for file in $files
do
if [[ $file =~ [0-9]{8} ]] && [[ $file == *.zip ]] && [[ $file == *bak* ]]; then
date_part="${BASH_REMATCH}"
# 获取当前日期
current_date=$(date +%Y%m%d)
# 将日期转换为秒数
file_date_seconds=$(date -d "$date_part" +%s)
current_seconds=$(date -d "$current_date" +%s)
# 计算时间差(以天为单位)
time_diff=$(( (current_seconds - file_date_seconds) / 86400 ))
if [ $time_diff -gt 90 ]; then
echo "删除日志 $file"
rm -rf ${audit_logs}${file}
fi
fi
done
并将以上2个shell脚本加入定时任务