首页 技术 正文
技术 2022年11月20日
0 收藏 435 点赞 3,687 浏览 1936 个字

目前社区版本的mysql的审计功能还是比较弱的,基于插件的审计目前存在于Mysql的企业版、Percona和MariaDB上,但是mysql社区版本有提供init-connect选项,基于此我们可以用它来完成审计功能。

init-connect参数说明:

http://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_init_connect

step1:创建用户数据库表

set names utf8
create database auditlog;
create table auditlog.t_audit(
id int not null auto_increment,
thread_id int not null,
login_time timestamp,
localname varchar(50) default null,
matchname varchar(50) default null,
primary key (id)
)ENGINE=InnoDB default charset=utf8 comment '审计用户登录信息';

step2:授权所有的用户拥有对审计表的插入权限

select concat("grant insert on auditlog.t_audit to '",user,"'@'",host,"';") from mysql.user;  #拼结授权语句
……
flush privileges;

注意,以后每添加一个用户都必须授权此表的插入权限,要不会连接不上。

step3:设置init_connect参数

set global init_connect='insert into auditlog.t_audit(id,thread_id,login_time,localname,matchname) values(null,connection_id(),now(),user(),current_user());';

并在配置文件中增加如下语句:init-connect=’insert into auditlog.t_audit(id,thread_id,login_time,localname,matchname) values(null,connection_id(),now(),user(),current_user());’以便下次重启时能生效

验证:

我们登陆后并删除一条记录,查看binlog,我们可以看到此操作的thread_id为7:

mysql基于init-connect+binlog完成审计功能

然后我们来查看此表t_audit表:

[zejin] 3301>select * from auditlog.t_audit;
+----+-----------+---------------------+---------------------------+-------------------------+
| id | thread_id | login_time | localname | matchname |
+----+-----------+---------------------+---------------------------+-------------------------+
| 1 | 5 | 2016-08-10 11:01:07 | user_app@192.168.1.240 | user_app@192.168.1.% |
| 2 | 6 | 2016-08-10 11:02:02 | user_app@192.168.1.236 | user_app@192.168.1.% |
| 3 | 7 | 2016-08-10 11:19:54 | user_yunwei@192.168.1.240 | user_yunwei@192.168.1.% |
+----+-----------+---------------------+---------------------------+-------------------------+
3 rows in set (0.00 sec)

可以看到thread_id为7的用户为user_yunwei,在192.168.1.240机器上操作删除的,完成了对数据的简单审计。 扩展说明:1.init-connect只会在连接时执行,不会对数据库产生大的性能影响2.init-connect是在连接时执行的动作命令,故可以用它来完成其它的功能,如:init_connect=’SET autocommit=0’3.init-connect不会记录拥有super权限的用户记录,为了防止init_connect语句由于语法错误或权限问题而所有用户都登陆不了的情况,保证至少super用户能登陆并修改此值    

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,078
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,553
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,402
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,177
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,814
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898