1. 程式人生 > >MySQL記錄使用者操作日誌

MySQL記錄使用者操作日誌

有時,我們想追蹤某個資料庫操作記錄,如想找出是誰操作了某個表(比如誰將欄位名改了)。

二進位制日誌記錄了操作記錄,執行緒號等資訊,但是卻沒有記錄使用者資訊,因此需要結合init-connect來實現追蹤。

init-connect,在每次連線的初始化階段,記錄下這個連線的使用者,和connection_id資訊。

1.建立監控連線資訊的資料庫及表

建立資料庫:

create database dba;

 建立表:

create table accesslog(`thread_id` int primary key auto_increment, `time` timestamp, `localname` varchar(40), `machine_name` varchar(40));  

thread_id : 記錄mysql 執行緒ID

time:記錄操作時間

localname:記錄操作遠端IP

machine_name:記錄使用者

2. 設定變數init_connect

檢視init_connect

mysql> show variables like 'init%';  
+---------------+-------+
| Variable_name | Value |                                                                                                               |
+---------------+-------+
| init_connect  |       |
| init_file     |       |
| init_slave    |       |
+---------------+-------+
3 rows in set (0.00 sec)

配置變數

set global init_connect='insert into dba.accesslog(thread_id,time,localname,machine_name) values(connection_id(),now(),user(),current_user());';

再次檢視init_connect

+---------------+-----------------------------------------------------------------------------------------------------------------------+
| Variable_name | Value                                                                                                                 |
+---------------+-----------------------------------------------------------------------------------------------------------------------+
| init_connect  | insert into dba.accesslog(thread_id,time,localname,machine_name) values(connection_id(),now(),user(),current_user()); |
| init_file     |                                                                                                                       |
| init_slave    |                                                                                                                       |
+---------------+-----------------------------------------------------------------------------------------------------------------------+
3 rows in set (0.00 sec)

3.為使用者賦記錄日誌許可權

mysql> grant select,insert,update on dba.accesslog to [email protected]'%'; 

4.建立原始資料

mysql> create database log_test;
mysql> create table test_table(uid INT,text VARCHAR(10));

5.賦予dba賬戶操作log_test庫的操作許可權

mysql> grant select,update,insert,delete on log_test.* to [email protected]'%' identified by 'dbapasswd' ;

如果想立即看到結果使用

flush  privileges ;

6.用dba賬戶登陸MySQL客戶端進行操作

[[email protected] ~]# mysql -u dba -p -h 192.168.10.145

mysql> use log_test;
mysql> show tables ;
+--------------------+
| Tables_in_log_test |
+--------------------+
| test_table         |
| tutorials_tbl      |
+--------------------+
2 rows in set (0.00 sec)
mysql> insert into test_table (uid,text) VALUES (1,11);

mysql> insert into test_table (uid,text) VALUES (2,11111);

7.檢視操作日誌

檢視日誌記錄所在的二進位制檔案

mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 |     3309 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

 檢視所操作的語句的執行緒號為10

[[email protected] mysql]# mysqlbinlog mysql-bin.000001 
......
# at 3078
#181029 23:12:29 server id 1  end_log_pos 3192  Query   thread_id=10    exec_time=0     error_code=0
use `log_test`/*!*/;
SET TIMESTAMP=1540825949/*!*/;
insert into test_table (uid,text) VALUES (1,11)
/*!*/;
# at 3192
#181029 23:12:40 server id 1  end_log_pos 3309  Query   thread_id=10    exec_time=0     error_code=0
SET TIMESTAMP=1540825960/*!*/;
insert into test_table (uid,text) VALUES (2,11111)
/*!*/; 
......

檢視當時時間點(181029 23:12:29)執行緒號為10 所登入的賬戶和ip地址

mysql> select * from dba.accesslog where thread_id=10;
+-----------+---------------------+--------------------+--------------+
| thread_id | time                | localname          | machine_name |
+-----------+---------------------+--------------------+--------------+
|        10 | 2018-10-29 23:08:36 | [email protected] | [email protected]%        |
+-----------+---------------------+--------------------+--------------+
1 row in set (0.00 sec)

此時即可鎖定此使用者ip為192.168.10.130。

注意: 對於所有的普通級別的使用者,必須全部都要對日誌表具有讀寫許可權, 否則將導致,沒有許可權的使用者無法使用資料庫。
init_connect 不會記錄有超級管理員許可權的使用者連線資訊 (原因:當init_connect設定有誤時,超級管理員可進行修改)
因此,對於一般的使用者,不能賦予all privileges許可權。
--如果想檢視所有的增刪改查記錄,在general log(需要先開啟)裡查詢即可。裡面記錄了連線的使用者和IP資訊。