作者:王少鹏
爱可生 DBA 团队成员,负责项目数据库日常问题处理及公司 DMP 平台问题处理,对数据库有强烈的兴趣。认为不会游泳的厨师绝不是一个好数据库工程师。
本文来源:原创投稿
*爱可生开源社区出品,原创内容未经授权不得随意使用,转载请联系小编并注明来源。
2.1 init_connect 参数
mysql> show variables like 'init_connect';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| init_connect | |
+---------------+-------+
1 row in set (0.00 sec)
mysql> set global init_connect='insert into auditdb.accesslog(connectionID,ConnUser,MatchUser,LoginTime) values(connection_id(),user(),current_user(),now());';
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like 'init_connect';
+---------------+-------------------------------------------------------------------------------------------------------------------------------+
| Variable_name | Value |
+---------------+-------------------------------------------------------------------------------------------------------------------------------+
| init_connect | insert into auditd.accesslog(connectionID,ConnUser,MatchUser,LoginTime) values(connection_id(),user(),current_user(),now()); |
+---------------+-------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
2.2 连接信息表
mysql> create database auditdb charset utf8mb4;
Query OK, 1 row affected (0.01 sec)
mysql> create table auditdb.accesslog (id int (10) unsigned not null primary key auto_increment,
Connectionid int(10) unsigned,ConnUser varchar (30) not null default '',
MatchUser varchar (30) not null default '',
Logintime datetime);
Query OK, 0 rows affected (0.02 sec)
3.1 模拟误删除
[root@db ~]# mysql -u mindoc -p -h 172.18.1.76
Enter password:
mysql> create table temp(id int,name varchar(32));
Query OK, 0 rows affected (0.03 sec)
mysql> insert into temp values(1,'aa');
Query OK, 1 row affected (0.01 sec)
mysql> insert into temp values(2,'aa');
Query OK, 1 row affected (0.01 sec)
mysql> insert into temp values(3,'aa');
Query OK, 1 row affected (0.01 sec)
mysql> delete from temp;
Query OK, 3 rows affected (0.01 sec)
通过执行的 delete 语句与大概执行时间,确定是哪个用户连接( thread_id=130 )
[root@db ~]
SET TIMESTAMP=1574226138;
BEGIN
;
BINLOG '
2sjUXRNMAAAANwAAAO+/AAAAAPkAAAAAAAEACW1pbmRvY19kYgAEdGVtcAACAw8CgAAD5H8ONg==
2sjUXSBMAAAAOwAAACrAAAAAAPkAAAAAAAEAAgAC//wBAAAAAmFh/AIAAAACYWH8AwAAAAJhYY/X
8Ls=
';
COMMIT;
SET @@SESSION.GTID_NEXT= 'AUTOMATIC' ;
DELIMITER ;
;
;
通过 ConnUser 字段可以看到是 172.18.1.99 这个地址使用 mindoc@’%’ 用户连接的 MySQL 数据库。
mysql> select * from auditdb.accesslog where Connectionid=130;
+----+--------------+--------------------+-----------+---------------------+
| id | Connectionid | ConnUser | MatchUser | Logintime |
+----+--------------+--------------------+-----------+---------------------+
| 1 | 130 | [email protected] | mindoc@% | 2019-11-20 12:59:21 |
+----+--------------+--------------------+-----------+---------------------+
1 row in set (0.00 sec)