在MYSQL中,每个连接都会先执行init-connect,进行连接的初始化。我们可以在这里获取用户的登录名称和thread的ID值。然后配合binlog,就可以追踪到每个操作语句的操作时间,操作人等。实现审计。

实验过程:
1:创建登录日志库,登录日志表

CREATE DATABASE `accesslog`;

USE `accesslog`;

CREATE TABLE `accesslog`

(

`id` int(11) NOT NULL AUTO_INCREMENT,

`thread_id` int(11) DEFAULT NULL, #线程ID,这个值很重要

`log_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, #登录时间

`localname` varchar(30) DEFAULT NULL, #登录名称带IP

`matchname` varchar(30) DEFAULT NULL, #登录用户,user的全称

PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

2:在配置文件中配置init-connect参数。登录时插入日志表。如果这个参数是个错误的SQL语句,登录就会失败。
Linux 下的配置文件为 my.cnf,windows下为my.ini

init-connect='insert into accesslog.accesslog values(null,connection_id(),now(),user(),current_user());'

log-bin

重启service mysqld 以使其配置文件生效

3:创建普通用户,不能有super权限。init-connect对具有super权限的用户不起作用。同时此用户必须要有INSERT权限,如果没有,登录后的任何操作都会导致MYSQL登录失败。

grant insert,select,update on *.* to 'user1'@'localhost'; #带INSERT权限

grant select,update on *.* to 'user2'@'localhost'; #不带INSERT权限

4:SESSION1登录,并查看日志

D:\mysql6\bin>mysql -uuser1 -p

Enter password:

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 65

Server version: 5.1.45-community-log MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select * FROM accesslog.accesslog;

+----+-----------+---------------------+-----------------+-----------------+

| id | thread_id | log_time | localname | matchname |

+----+-----------+---------------------+-----------------+-----------------+

| 1 | 65 | 2011-03-11 19:18:25 | user1@localhost | user1@localhost |

+----+-----------+---------------------+-----------------+-----------------+

1 row in set (0.00 sec)

mysql> show processlist;# 当前运行的threadId

+----+-------+----------------+------+---------+------+-------+------------------+

| Id | User | Host | db | Command | Time | State | Info |

+----+-------+----------------+------+---------+------+-------+------------------+

| 65 | user1 | localhost:1339 | NULL | Query | 0 | NULL | show processlist |

+----+-------+----------------+------+---------+------+-------+------------------+

1 row in set (0.00 sec)

mysql>

5:再用user2登录

D:\mysql6\bin>mysql -uuser2 -p

Enter password:

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 76

Server version: 5.1.45-community-log

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select * FROM accesslog.accesslog;

ERROR 2006 (HY000): MySQL server has gone away

No connection. Trying to reconnect...

Connection id: 77

Current database: *** NONE ***

ERROR 2013 (HY000): Lost connection to MySQL server during query

mysql> select * FROM accesslog.accesslog;

ERROR 2006 (HY000): MySQL server has gone away

No connection. Trying to reconnect...

Connection id: 78

Current database: *** NONE ***

看下错误日志
如果没有对log-bin指定log文件,默认在 /var/lib/mysql目录下以mysqld-bin.00000X等作为名称。而 mysqld-bin.index则记录了所有的log的文件名称
使用时则使用mysqlbinlog /var/lib/mysql|grep "*****"等来追踪database的操作。

110311 19:23:47 [Warning] Aborted connection 77 to db: 'unconnected' user: 'user2' host: 'localhost' (init_connect command failed)

110311 19:23:47 [Warning] INSERT command denied to user 'user2'@'localhost' for table 'accesslog'

110311 19:23:53 [Warning] Aborted connection 78 to db: 'unconnected' user: 'user2' host: 'localhost' (init_connect command failed)

110311 19:23:53 [Warning] INSERT command denied to user 'user2'@'localhost' for table 'accesslog'

6:下面以USER1登录,并做一个INSERT操作,查看日志文件。

mysql> insert into t3 values(10,10,'2011-10-10 00:00:00');

Query OK, 1 row affected (0.00 sec)

mysql> show processlist;

+----+-------+----------------+-----------+---------+------+-------+------------------+

| Id | User | Host | db | Command | Time | State | Info |

+----+-------+----------------+-----------+---------+------+-------+------------------+

| 69 | user1 | localhost:1439 | accesslog | Query | 0 | NULL | show processlist |

+----+-------+----------------+-----------+---------+------+-------+------------------+

1 row in set (0.00 sec)

mysql> select * from accesslog.accesslog;

+----+-----------+---------------------+-----------------+-----------------+

| id | thread_id | log_time | localname | matchname |

+----+-----------+---------------------+-----------------+-----------------+

| 1 | 65 | 2011-03-11 19:18:25 | user1@localhost | user1@localhost |

| 2 | 91 | 2011-03-11 19:28:33 | user1@localhost | user1@localhost |

| 3 | 2 | 2011-03-11 19:31:49 | user1@localhost | user1@localhost |

| 4 | 2 | 2000-10-10 10:10:10 | user1@localhost | user1@localhost |

| 5 | 21 | 2000-10-10 11:11:11 | root@localhost | root@% |

| 6 | 69 | 2011-03-12 21:35:43 | user1@localhost | user1@localhost |

+----+-----------+---------------------+-----------------+-----------------+

6 rows in set (0.01 sec)

查看日志文件的内容

# at 340

#110312 21:36:01 server id 1 end_log_pos 453 Query thread_id=69 exec_time=0 error_code=0

use text;

SET TIMESTAMP=1299936961;

insert into t3 values(10,10,'2011-10-10 00:00:00')

;

# at 453

thread_id=69

在日志表里记录的和日志文件里面记录的相同。可以通过这个thread_id来追踪到是谁,什么时间,做了什么操作。

注:

1.mysql在linux下可以安装audit_log.so插件来实现审计,在windows下不可以,所以用此方法代替。

2.此方法只适用于binlog存在的条件下,如果系统默认保留14天binlog日志则14天以前的用户信息无法查询。

3.对于用户赋权时不是on *.*的用户需要加grant insert on accesslog.accesslog to user@host。

4.当mysql重启后accesslog表中thread_id可能会有重复情况,此时需要结合log_time判断具体登录用户信息。

5.注释为本人自己总结,如有不对欢迎指出。

mysql 用init-connect+binlog实现用户操作追踪做access的ip的log记录的更多相关文章

  1. 0816关于MySQL的审计 init-connect+binlog实现用户操作追踪

    转自:http://blog.sina.com.cn/s/blog_605f5b4f01013xkv.html mysql 用init-connect+binlog实现用户操作追踪 做access 的 ...

  2. 在MySQL中使用init-connect与binlog来实现用户操作追踪记录

    在MySQL中使用init-connect与binlog来实现用户操作追踪记录 分类: MySQL 前言: 测试环境莫名其妙有几条重要数据被删除了,由于在binlog里面只看到是公用账号删除的,无法查 ...

  3. MySQL数据库(6)_用户操作与权限管理、视图、存储过程、触发器、基本函数

    用户操作与权限管理 MySQL用户操作 创建用户 方法一: CREATE USER语句创建 CREATE USER "用户名"@"IP地址" IDENTIFIE ...

  4. linux mysql授权远程连接,创建用户等

    1.进入mysql 2.此命令是为密码为 root .IP(%)任意的 root 用户授权.(*.* 表示数据库.表,to后为root用户:%:模糊查询,所有 IP 都可以,可指定其他主机 IP:by ...

  5. mysql添加,授权,删除用户以及连接数据库Can't connect to MySQL server on '192.168.31.106' (113)错误排查

    centos7下面操作mysql添加,授权,删除用户 添加用户 以root用户登录数据库,运行以下命令: create user test identified by '; 上面创建了用户test,密 ...

  6. mysql颠覆实战笔记(三)-- 用户登录(二):保存用户操作日志的方法

    版权声明:笔记整理者亡命小卒热爱自由,崇尚分享.但是本笔记源自www.jtthink.com(程序员在囧途)沈逸老师的<web级mysql颠覆实战课程 >.如需转载请尊重老师劳动,保留沈逸 ...

  7. MySql常用操作语句(1:启动、连接数据库及用户操作)

    下方将个人常用的MySql操作语句(Win7下)总结如下: 1. 启动与关闭数据库 “管理员”权限, MySql安装目录下bin目录//:  1.1 启动 @>net start mysql   ...

  8. mysql 用户操作和授权

    1.查看mysql的版本 mysql -V 2.用户操作 # 创建用户 create user 'username'@'ip地址' identified by '密码'; # 用户重命名 rename ...

  9. Mysql 创建权限较小的用户(只对特定数据库有操作权限)

    项目开发过程中,因为root的权限太大,可能对其他数据库造成修改.故创建一权限较小的用户,使其只能对特定的数据库操作,以保证数据安全. 主要语句如下: grant all on bos19.* to ...

随机推荐

  1. k8s 使用 traefik 将clusterIP的 svc 暴露服务的方法

    0. 前置条件 安装好k8s.. traefik的简介图 1. 安装 traefik 方法: 创建一个目录 并且存放部分文件等. mkdir /traefik cd /traefik git clon ...

  2. developer roadmap

    developer roadmap https://balsamiq.com/ https://balsamiq.com/givingback/free/ https://balsamiq.cloud ...

  3. swagger error: Conflicting schemaIds: Duplicate schemaIds detected for types A and B

    使用Web API并使用swashbuckle生成swagger文档,我在两个不同的命名空间中定义了两个具有相同名称的不同类.当我在浏览器中打开swagger页面时,它说: Conflicting s ...

  4. JDK8新特性,给接口添加一个默认实现

    在JDK8中,允许给接口本身添加一个默认的实现.用“default”进行修饰.如下实例 package interfacetest; public interface TestInterface { ...

  5. BZOJ2568 比特集合(树状数组)

    考虑维护f[k][x]表示“最低k位所表示的数不大于x”的数的个数.那么查询时答案就为f[k][2k-1]-f[k][2k-1-1]. 同时记录每个数在集合中出现多少次.这样的话插入.删除已经解决了, ...

  6. bzoj 4448 [Scoi2015]情报传递 (树链剖分+主席树)

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=4448 题面: Description 奈特公司是一个巨大的情报公司,它有着庞大的情报网络 ...

  7. 【刷题】BZOJ 1124 [POI2008]枪战Maf

    Description 有n个人,每个人手里有一把手枪.一开始所有人都选定一个人瞄准(有可能瞄准自己).然后他们按某个顺序开枪,且任意时刻只有一个人开枪.因此,对于不同的开枪顺序,最后死的人也不同. ...

  8. 架构师成长之路1.2-多功能系统信息统计工具dstat

    点击返回架构师成长之路 架构师成长之路1.2-多功能系统信息统计工具dstat dstat命令是一个用来替换vmstat.iostat.netstat.nfsstat和ifstat这些命令的工具,是一 ...

  9. 《剑指offer》— JavaScript(30)连续子数组的最大和

    连续子数组的最大和 题目描述 HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学.今天测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好 ...

  10. python---django请求-响应的生命周期(FBV和CBV含义)

    Django请求的生命周期是指:当用户在访问该url路径是,在服务器Django后台都发生了什么. 客户端发送Http请求给服务端,Http请求是一堆字符串,其内容是: 访问:http://crm.o ...