ITPub博客

首页 > 数据库 > MySQL > mysql利用init-connect和binlog审计功能

mysql利用init-connect和binlog审计功能

原创 MySQL 作者:jacobxian 时间:2015-01-20 16:48:20 0 删除 编辑
测试脚本:
CREATE TABLE log_xyz.accesslog (`id` int(11) primary key auto_increment,conn_id int, `time` timestamp, `localname` varchar(30), `matchname` varchar(30));


grant all privileges on log_xyz.* to 'xyz'@'%' identified by '******';




下面参数设置,必须授予非操作用户插入log_xyz.accesslog的权限,否则,会导致报错,无法使用数据库。
在参数文件设置如下:
init-connect='insert into log_xyz.accesslog(conn_id,time,localname,matchname) values(connection_id(),now(),user(),current_user());'


若是在mysql客户端命令如下:
set global init_connect='insert into log_xyz.accesslog(conn_id,time,localname,matchname) values(connection_id(),now(),user(),current_user());';


使用xyz登录并truncate t_xyz2表,
[mysql@zdzxdb02 ~]$ mysql -uxyz -p****** -h172.16.11.89 -P3306
mysql> use log_xyz;
mysql> truncate table t_xyz2;
Query OK, 0 rows affected (0.01 sec)


通过binlog日志:
mysqlbinlog binlog.000003>3.sql
cat 3.sql 
查看日志:
# at 890
#150105  9:45:56 server id 2  end_log_pos 977   Query   thread_id=845   exec_time=0     error_code=0
SET TIMESTAMP=1420422356/*!*/;
truncate table t_xyz2
/*!*/;
DELIMITER ;
# End of log file


thread_id=845和log_xyz.accesslog的登录时间段差不多有记录。
root@localhost:log_xyz 09:44:50> select * from accesslog;
+-----+---------+---------------------+------------------+----------------+
| id  | conn_id | time                | localname        | matchname      |
+-----+---------+---------------------+------------------+----------------+
| 825 |     845 | 2015-01-05 09:45:09 | xyz@172.16.11.89 | xyz@%          |
+-----+---------+---------------------+------------------+----------------+
这证明是xyz@%用户操作,登录主机是172.16.11.89。


审计对dba本身没用
而非dba的人只要加强权限管理就行,业务和运维账号实现最小权限原则,宁愿操作麻烦,确保数据安全。
对于连接不是非常频繁的数据库来说,增加写入日志功能,对性能影响不大。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29114615/viewspace-1408342/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2014-07-16

  • 博文量
    18
  • 访问量
    45390