需要:audit-plugin-mysql-5.7-1.1.9-974-linux-x86_64,
不让放链接,可以在必应里查一下这个,在github中下载。

前言1:MySQL的版本问题,个人水平有限,在8.0版本无法安装上审计插件,网上有一个关于8.0版本的教程,不过人家并未公布修改后的插件。
安装审计插件
1、上传audit-plugin到opt/mysql目录下,并解压
unzip audit-plugin-mysql-5.7-1.1.9-974-linux-x86_64.zip
2、将lib 目录下的文件移动到 mysql的plugin中
查看--mysql-plugin位置,登录mysql 输入下面指令
SHOW VARIABLES LIKE '%plugin%'

将libaudit_plugin.so移动到上面的地址
mv libaudit_plugin.so /usr/lib64/mysql/plugin/
3、赋予权限
chmod 777 /usr/lib64/mysql/plugin/libaudit_plugin.so
4、登录mysql,尝试执行安装【一般都会报错】
install plugin audit soname 'libaudit_plugin.so';

5、进入utils目录下
cd /opt/mysql/audit-plugin-mysql-5.7-1.1.9-974/utils/
赋权限
chmod 777 offset-extract.sh
查看mysqld指令位置
which mysqld

执行指令
./offset-extract.sh /usr/sbin/mysqld

复制生成的内容,选取数字的一段复制

6、修改my.cnf
查看位置whereis my.cnf
编辑 vim my.cnf
在[mysqld]之后加入
plugin-load=AUDIT=libaudit_plugin.so
audit_offsets=7832, 7880, 3640, 4800, 456, 360, 0, 32, 64, 160, 544, 7996, 4368, 3648, 3656, 3660, 6080, 2072, 8, 7064, 7104, 7088, 13480, 148, 672, 0
audit_json_file=1

保存退出。
重启MySQL、查询状态。
systemctl restart mysqld
systemctl status mysqld
登录MySQL
show plugins

完成安装。
查看审计日志,并将数据插入一张表
找到mysql-audit.json
一般在 /var/lib/mysql下
cd /var/lib/mysql
tail -f mysql-audit.json

这些json字符串就是日志。
之后,可以写脚本,也可以写其他程序来定时调取这些内容,将他们插入到数据的表中。
提供一个Java读取的方法
String filePath = "/var/lib/mysql/mysql-audit.json";
BufferedReader f = new BufferedReader(new FileReader(filePath));
String line;
while ((line = f.readLine()) != null) {
//fastJson
JSON.parseObject(line);---->处理JSON字符串
//之后就可以进行插入数据的操作了,可以对一些字段是否为空进行一个判断
}
网友评论