Mysql 記錄用戶操作
阿新 • • 發佈:2018-05-24
mysql 記錄Mysql 連接通過init_connect來初始化,官網說明:
服務器為每個連接的客戶端執行的字符串。該字符串由一個或多個SQL語句組成,用分號字符分隔。 例如,默認情況下每個客戶端會話都啟用自動提交模式。對於較舊的服務器(在MySQL 5.5.8之前), 沒有全局自動提交系統變量來指定默認情況下應禁用自動提交,但作為解決方法,init_connect可用於實現相同的效果: SET GLOBAL init_connect ='SET autocommit = 0';
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
+---------------+-------+ | 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.為用戶賦記錄日誌權限
grant select,insert,update on dba.accesslog to 'zhaohongming'@'%';
4.模擬用戶操作添加刪除
root@localhost:(none) 11:34:49 >use sdlcqw; root@localhost:(none) 11:34:49 >crate table haha(cc int); root@localhost:(none) 11:34:49 >drop table haha;
5.查看binlog記錄
導出binlog內容:
# mysqlbinlog mysql-bin.000079 > /root/9.txt
查詢log表:
root@localhost:(none) 11:39:41 >use dba; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed root@localhost:dba 11:39:50 >select * from accesslog limit 1; +-----------+---------------------+------------------------+----------------+ | thread_id | time | localname | machine_name | +-----------+---------------------+------------------------+----------------+ | 279950 | 2018-05-24 08:51:37 | zhaohongming@localhost | zhaohongming@% | +-----------+---------------------+------------------------+----------------+ 1 row in set (0.00 sec)
6.查看binlog內容
上面的線程是279950
# cat 9.txt | grep -B 10 haha
# at 267289752 #180524 8:52:35 server id 1 end_log_pos 267289794 CRC32 0x542b2211 GTID 0-1-2743823 ddl /*!100001 SET @@session.gtid_seq_no=2743823*//*!*/; # at 267289794 #180524 8:52:35 server id 1 end_log_pos 267289888 CRC32 0x6d9ec74d Query thread_id=279950 exec_time=0 error_code=0 use `sdlcqw`/*!*/; SET TIMESTAMP=1527123155/*!*/; SET @@session.sql_auto_is_null=0, @@session.check_constraint_checks=1/*!*/; /*!\C utf8 *//*!*/; SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=33/*!*/; create table haha(cc int) -- #180524 8:52:42 server id 1 end_log_pos 267296661 CRC32 0x4552d77e Xid = 105731850 COMMIT/*!*/; # at 267296661 #180524 8:52:42 server id 1 end_log_pos 267296703 CRC32 0x42549f0e GTID 0-1-2743846 ddl /*!100001 SET @@session.gtid_seq_no=2743846*//*!*/; # at 267296703 #180524 8:52:42 server id 1 end_log_pos 267296815 CRC32 0x8b715e13 Query thread_id=279950 exec_time=0 error_code=0 use `sdlcqw`/*!*/; SET TIMESTAMP=1527123162/*!*/; SET @@session.sql_mode=1342177280/*!*/; DROP TABLE `haha` /* generated by server */
Mysql 記錄用戶操作