转自:http://blog.sina.com.cn/s/blog_605f5b4f01013xkv.html

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

在MYSQL中,每个连接都会先执行init-connect,进行连接的初始化。我们可以在这里获取用户的登录名称和thread的ID值。然后配合binlog,就可以追踪到每个操作语句的操作时间,操作人等。实现审计。
实验过程:
1:创建登录日志库,登录日志表
  1. CREATE DATABASE `accesslog`;
  2. USE `accesslog`;
  3. CREATE TABLE `accesslog`
  4. (
  5. `id` int(11) NOT NULL AUTO_INCREMENT,
  6. `thread_id` int(11) DEFAULT NULL, #线程ID,这个值很重要
  7. `log_time` timestamp NOT NULL DEF AULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, #登录时间
  8. `localname` varchar(30) DEFAULT NULL, #登录名称带IP
  9. `matchname` varchar(30) DEFAULT NULL, #登录用户,user的全称
  10. PRIMARY KEY (`id`)
  11. ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
2:在配置文件中配置init-connect参数。登录时插入日志表。如果这个参数是个错误的SQL语句,登录就会失败。
Linux 下的配置文件为 my.cnf,windows下位my.ini

  1. init-connect='insert into accesslog.accesslog values(null,connection_id(),now(),user(),current_user());' -- 注意修改对应SQL
重启service mysqld 以使其配置文件生效

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

  1. grant insert,select,update on *.* to 'user1'@'localhost'; #带INSERT权限
  2. grant select,update on *.* to 'user2'@'localhost'; #不带INSERT权限
4:SESSION1登录,并查看日志

  1. mysql> select * FROM accesslog.accesslog;
  2. +----+-----------+---------------------+-----------------+-----------------+
  3. | id | thread_id | log_time | localname | matchname |
  4. +----+-----------+---------------------+-----------------+-----------------+
  5. | 1 | 65 | 2011-03-11 19:18:25 | user1@localhost | user1@localhost |
  6. +----+-----------+---------------------+-----------------+-----------------+
  7. 1 row in set (0.00 sec)
  8. mysql> show processlist;# 当前运行的threadId
  9. +----+-------+----------------+------+---------+------+-------+------------------+
  10. | Id | User | Host | db | Command | Time | State | Info |
  11. +----+-------+----------------+------+---------+------+-------+------------------+
  12. | 65 | user1 | localhost:1339 | NULL | Query | 0 | NULL | show processlist |
  13. +----+-------+----------------+------+---------+------+-------+------------------+
  14. 1 row in set (0.00 sec)
  15. mysql>
5:再用user2登录

  1. D:\mysql6\bin>mysql -uuser2 -p
  2. Enter password:
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 76
  5. Server version: 5.1.45-community-log
  6. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  7. mysql> select * FROM accesslog.accesslog;
  8. ERROR 2006 (HY000): MySQL server has gone away
  9. No connection. Trying to reconnect...
  10. Connection id: 77
  11. Current database: *** NONE ***
  12. ERROR 2013 (HY000): Lost connection to MySQL server during query
  13. mysql> select * FROM accesslog.accesslog;
  14. ERROR 2006 (HY000): MySQL server has gone away
  15. No connection. Trying to reconnect...
  16. Connection id: 78
  17. Current database: *** NONE ***
看下错误日志
如果没有对log-bin指定log文件,默认在 /var/lib/mysql目录下以mysqld-bin.00000X等作为名称。而 mysqld-bin.index则记录了所有的log的文件名称
使用时则使用mysqlbinlog /var/lib/mysql|grep -B "表名"等来追踪database的操作。

  1. 110311 19:23:47 [Warning] Aborted connection 77 to db: 'unconnected' user: 'user2' host: 'localhost' (init_connect command failed)
  2. 110311 19:23:47 [Warning] INSERT command denied to user 'user2'@'localhost' for table 'accesslog'
  3. 110311 19:23:53 [Warning] Aborted connection 78 to db: 'unconnected' user: 'user2' host: 'localhost' (init_connect command failed)
  4. 110311 19:23:53 [Warning] INSERT command denied to user 'user2'@'localhost' for table 'accesslog'
6:下面以USER1登录,并做一个INSERT操作,查看日志文件。

  1. mysql> insert into t3 values(10,10,'2011-10-10 00:00:00');
  2. Query OK, 1 row affected (0.00 sec)
  3. mysql> show processlist;
  4. +----+-------+----------------+-----------+---------+------+-------+------------------+
  5. | Id | User | Host | db | Command | Time | State | Info |
  6. +----+-------+----------------+-----------+---------+------+-------+------------------+
  7. | 69 | user1 | localhost:1439 | accesslog | Query | 0 | NULL | show processlist |
  8. +----+-------+----------------+-----------+---------+------+-------+------------------+
  9. 1 row in set (0.00 sec)
  10. mysql> select * from accesslog.accesslog;
  11. +----+-----------+---------------------+-----------------+-----------------+
  12. | id | thread_id | log_time | localname | matchname |
  13. +----+-----------+---------------------+-----------------+-----------------+
  14. | 1 | 65 | 2011-03-11 19:18:25 | user1@localhost | user1@localhost |
  15. | 2 | 91 | 2011-03-11 19:28:33 | user1@localhost | user1@localhost |
  16. | 3 | 2 | 2011-03-11 19:31:49 | user1@localhost | user1@localhost |
  17. | 4 | 2 | 2000-10-10 10:10:10 | user1@localhost | user1@localhost |
  18. | 5 | 21 | 2000-10-10 11:11:11 | root@localhost | root@% |
  19. | 6 | 69 | 2011-03-12 21:35:43 | user1@localhost | user1@localhost |
  20. +----+-----------+---------------------+-----------------+-----------------+
  21. 6 rows in set (0.01 sec)
查看日志文件的内容
  1. # at 340
  2. #110312 21:36:01 server id 1 end_log_pos 453 Query thread_id=69 exec_time=0 error_code=0
  3. use text;
  4. SET TIMESTAMP=1299936961;
  5. insert into t3 values(10,10,'2011-10-10 00:00:00')
  6. ;
  7. # at 453
thread_id=69
在日志表里记录的和日志文件里面记录的相同。可以通过这个thread_id来追踪到是谁,什么时间,做了什么操作。

3. Q&A

Q:使用init-connect会影响服务器性能吗?

A:理论上,只会在用户每次连接时往数据库里插入一条记录,不会对数据库产生很大影响。除非连接频率非常高(当然,这个时候需要注意的就是如何进行连接复用和控制,而非是不是要用这种方法的问题了)

Q:access-log表如何维护?

A: 由于是一个log系统,推荐使用archive存储引擎,有利于数据厄压缩存放。如果数据库连接数量很大的话,建议一定时间做一次数据导出,然后清表。

Q:表有其他用途么?

A:有!access-log表当然不只用于审计,当然也可以用于对于数据库连接的情况进行数据分析,例如每日连接数分布图等等,只有想不到没有做不到。

Q:会有遗漏的记录吗?

A:会的,init-connect 是不会在super用户登录时执行的。所以access-log里不会有数据库超级用户的记录,这也是为什么我们不主张多个超级用户,并且多人使用的原因

-- 这个跟权限有关

可以学习 https://mp.weixin.qq.com/s/EZ71SU21tOa35VQayH8Hfw该文章,

通过SQL和流量监控查询数据变化

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

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

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

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

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

  3. MySQL 库、表、记录、相关操作(1)

    库.表.记录.相关操作(1) 数据库配置 # 通过配置文件统一配置的目的:统一管理 服务端(mysqld) .客户端(client) # 配置了 mysqld(服务端) 的编码为utf8,那么再创建的 ...

  4. mysql 之审计 init-connect+binlog完成审计功能

    mysql基于init-connect+binlog完成审计功能 目前社区版本的mysql的审计功能还是比较弱的,基于插件的审计目前存在于Mysql的企业版.Percona和MariaDB上,但是my ...

  5. Inception服务的安装以及使用Python 3 实现MySQL的审计

    Inception服务的安装以及使用Python实现MySQL的审计 Bison是Inception服务所依赖的包之一,但是某些Linux版本已安装的Bison,或者是通过yum安装的Bison,通常 ...

  6. 如何解决远程连接mysql出现Can’t connect to MySQL server on (111 “Connection refused”)的问题

    如何解决远程连接mysql出现Can’t connect to MySQL server on (111 “Connection refused”)的问题 开放Mysql的远程连接 在服务器上登录my ...

  7. 连接Mysql提示Can’t connect to local MySQL server through socket的解决方法

    mysql,mysqldump,Mysqladmin,php连接mysql服务常会提示下面错误: ERROR 2002 (HY000): Can't connect to local MySQL se ...

  8. Mac下安装Mysql出现 Can’t connect to local MySQL server through socket '/tmp/mysql.sock'

    在Mac下安装mysql出现 Can't connect to local MySQL server through socket '/tmp/mysql.sock' 错误,解决如下: $ unset ...

  9. 【转】pam_mysql - MySQL error (Can't connect to local MySQL server through socket

    转自:http://350201.blog.51cto.com/340201/1034672 参照 http://wjw7702.blog.51cto.com/5210820/936244博 主做的p ...

随机推荐

  1. 修改input:file样式

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  2. @Transaction 无效

    上班的时候碰到这个问题,看了一些博客写的,都试了一遍解决方案,发现结果还是不行, 最后突然发现我的配置顺序和网上的有些许不同,就改了下,发现成功了,特此打桩纪念一下. 一.先说一下基本用法: 1. @ ...

  3. Struts2 中 result type=”json” 的参数解释

    转自:http://wangquanhpu.iteye.com/blog/1461750 1, ignoreHierarchy 参数:表示是否忽略等级,也就是继承关系,比如:TestAction 继承 ...

  4. 大数据攻城狮之Hadoop伪分布式篇

    对于初学大数据的萌新来说,初次接触Hadoop伪分布式搭建的同学可能是一脸萌笔的,那么这一次小编就手把手的教大家在centos7下搭建Hadoop伪分布式. 底层环境: VMware Workstat ...

  5. Python 42 mysql用户管理 、pymysql模块

    一:mysql用户管理 什么是mysql用户管理 mysql是一个tcp服务器,应用于操作服务器上的文件数据,接收用户端发送的指令,接收指令时需要考虑到安全问题, ATM购物车中的用户认证和mysql ...

  6. BZOJ 2101 DP+优化

    思路: http://www.cnblogs.com/exponent/archive/2011/08/14/2137849.html f[i,i+len]=sum[i,i+len]-min(f[i+ ...

  7. python 线程池和锁

    一.死锁现象与递归锁 锁:Lock线程安全,多线程操作时,内部会让所有线程排队处理.如:list/dict/Queue        线程不安全 + 人 => 排队处理. import thre ...

  8. HTTPS的中那些加密算法

    密码学在计算机科学中使用非常广泛,HTTPS就是建立在密码学基础之上的一种安全的通信协议.HTTPS早在1994年由网景公司首次提出,而如今在众多互联网厂商的推广之下HTTPS已经被广泛使用在各种大小 ...

  9. Android 权限管理(持续整理)

    1. Android 6.0之后,APP可以直接安装,运行时再询问用户授予相关权限,此时系统弹出一个对话框,(这个对话框不能由开发者定制) 同时用户也可以在手机的“设置”中对于某个App进行权限管理 ...

  10. 远程连接Oracle设置

    1:打开net manager:开始->程序->oracle->配置和移植工具->Net Manager 2:添加服务器名->名子自定义,一般根据项目自定义,自己知道就行 ...