mysql 用init-connect+binlog实现用户操作追踪做access的ip的log记录

在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 | [email protected] | [email protected] |

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

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 | [email protected] | [email protected] |

| 2 | 91 | 2011-03-11 19:28:33 | [email protected] | [email protected] |

| 3 | 2 | 2011-03-11 19:31:49 | [email protected] | [email protected] |

| 4 | 2 | 2000-10-10 10:10:10 | [email protected] | [email protected] |

| 5 | 21 | 2000-10-10 11:11:11 | [email protected] | [email protected]% |

| 6 | 69 | 2011-03-12 21:35:43 | [email protected] | [email protected] |

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

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来追踪到是谁,什么时间,做了什么操作。

时间: 2024-08-03 12:06:34

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

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

转自:http://blog.sina.com.cn/s/blog_605f5b4f01013xkv.html mysql 用init-connect+binlog实现用户操作追踪 做access 的ip的log 记录 在MYSQL中,每个连接都会先执行init-connect,进行连接的初始化.我们可以在这里获取用户的登录名称和thread的ID值.然后配合binlog,就可以追踪到每个操作语句的操作时间,操作人等.实现审计. 实验过程:1:创建登录日志库,登录日志表 CREATE DATAB

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

前言:测试环境莫名其妙有几条重要数据被删除了,由于在binlog里面只看到是公用账号删除的,无法查询是那个谁在那个时间段登录的,就考虑怎么记录每一个MYSQL账号的登录信息,在MYSQL中,每个连接都会先执行init-connect,进行连接的初始化,我们可以在这里获取用户的登录名称和thread的ID值.然后配合binlog,就可以追踪到每个操作语句的操作时间,操作人等.实现审计. 1,在mysql服务器db中建立单独的记录访问信息的库set names utf8;create databas

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

用户操作与权限管理 MySQL用户操作 创建用户 方法一: CREATE USER语句创建 CREATE USER "用户名"@"IP地址" IDENTIFIED BY "密码"; 方法二: INSERT语句创建 INSERT INTO mysql.user(user,host, password,ssl_cipher,x509_issuer,x509_subject) VALUES('用户名','IP地址',password('密码'),'',

Mysql 记录用户操作

Mysql 连接通过init_connect来初始化,官网说明: 服务器为每个连接的客户端执行的字符串.该字符串由一个或多个SQL语句组成,用分号字符分隔. 例如,默认情况下每个客户端会话都启用自动提交模式.对于较旧的服务器(在MySQL 5.5.8之前), 没有全局自动提交系统变量来指定默认情况下应禁用自动提交,但作为解决方法,init_connect可用于实现相同的效果: SET GLOBAL init_connect ='SET autocommit = 0'; 1.创建数据库及表 创建数

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

centos7下面操作mysql添加,授权,删除用户 添加用户 以root用户登录数据库,运行以下命令: create user test identified by '123456789'; 上面创建了用户test,密码是123456789.我们在mysql.user表里面可以看到新增的用户信息 +------+----------------+-------------------------------------------+ | user | host | password | +--

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

下方将个人常用的MySql操作语句(Win7下)总结如下: 1. 启动与关闭数据库 “管理员”权限, MySql安装目录下bin目录//:  1.1 启动 @>net start mysql  1.2 关闭 @>net stop mysql;  1.3 问题:服务名无效 这种情况是mysql服务没有安装,这时需要在同个目录下安装服务: @>mysqld -install 相应地,卸载命令是: @>mysqld -remove 2. 连接数据库 “管理员”权限, MySql安装目录下

【Mysql】常用指令之——用户操作(创建,授权,修改,删除)

Mysql中的用户 user 每一个user都对应了不同的用户地址和权限 创建Mysql用户共有三种方式1.create user 2.grant 3.操作mysql.user表 1.CREATE USER 'username'@'host' IDENTIFIED BY 'password'; 例子: CREATE USER 'aa'@'localhost' IDENTIFIED BY '123456'; CREATE USER 'aa'@'192.168.1.101_' IDENDIFIED

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

版权声明:笔记整理者亡命小卒热爱自由,崇尚分享.但是本笔记源自www.jtthink.com(程序员在囧途)沈逸老师的<web级mysql颠覆实战课程 >.如需转载请尊重老师劳动,保留沈逸老师署名以及课程来源地址. 现在我们接着上节课,完成第二个功能,不管成功不成功都记录一个日志. 一.回顾上节课内容,我们在user_log表中添加一个字段 user_id.  在上一节课的存储过程新增一行代码,如下: BEGIN set @gid=0; set @user_name=''; set @_res

mysql用户操作和权限管理

用户操作与权限管理 MySQL用户操作 创建用户 方法一: CREATE USER语句创建 CREATE USER "用户名"@"IP地址" IDENTIFIED BY "密码"; 方法二: INSERT语句创建 INSERT INTO mysql.user(user,host, password,ssl_cipher,x509_issuer,x509_subject) VALUES(‘用户名’,’IP地址’,password(‘密码’),’’,