Mysql 記錄使用者操作

來源:互聯網
上載者:User

標籤: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.類比使用者操作添加刪除

[email protected]:(none) 11:34:49 >use sdlcqw;[email protected]:(none) 11:34:49 >crate table haha(cc int);[email protected]:(none) 11:34:49 >drop table haha;

5.查看binlog記錄

匯出binlog內容:

# mysqlbinlog mysql-bin.000079 > /root/9.txt

查詢log表:

[email protected]:(none) 11:39:41 >use dba;Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changed[email protected]:dba 11:39:50 >select * from accesslog limit 1;+-----------+---------------------+------------------------+----------------+| thread_id | time                | localname              | machine_name   |+-----------+---------------------+------------------------+----------------+|    279950 | 2018-05-24 08:51:37 | [email protected] | [email protected]% |+-----------+---------------------+------------------------+----------------+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=0use `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 = 105731850COMMIT/*!*/;# 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=0use `sdlcqw`/*!*/;SET TIMESTAMP=1527123162/*!*/;SET @@session.sql_mode=1342177280/*!*/;DROP TABLE `haha` /* generated by server */


Mysql 記錄使用者操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.