mysql基於init-connect+binlog完成審計功能

來源:互聯網
上載者:User

標籤:int   login   local   use   lob   設定   nod   檔案   cal   

目前社區版本的mysql的審計功能還是比較弱的,基於外掛程式的審計目前存在於Mysql的企業版、Percona和MariaDB上,但是mysql社區版本有提供init-connect選項,基於此我們可以用它來完成審計功能。

init-connect參數說明:

http://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_init_connect

 

step1:建立使用者資料庫表

set names utf8create database auditlog;create table auditlog.t_audit(  id int not null auto_increment,  thread_id int not null,  login_time timestamp,  localname varchar(50) default null,  matchname varchar(50) default null,   primary key (id))ENGINE=InnoDB default charset=utf8 comment ‘審計使用者登入資訊‘; 

step2:授權所有的使用者擁有對審計表的插入許可權

select concat("grant insert on auditlog.t_audit to ‘",user,"‘@‘",host,"‘;") from mysql.user;  #拼接授權語句
……flush privileges;

注意,以後每添加一個使用者都必須授權此表的插入許可權,要不會串連不上。

 

step3:設定init_connect參數

set global init_connect=‘insert into auditlog.t_audit(id,thread_id,login_time,localname,matchname) values(null,connection_id(),now(),user(),current_user());‘;
並在設定檔中增加如下語句:init-connect=‘insert into auditlog.t_audit(id,thread_id,login_time,localname,matchname) values(null,connection_id(),now(),user(),current_user());‘以便下次重啟時能生效 

驗證:

我們登陸後並刪除一條記錄,查看binlog,我們可以看到此操作的thread_id為7:

 

然後我們來查看此表t_audit表:
[zejin] 3301>select * from auditlog.t_audit;+----+-----------+---------------------+---------------------------+-------------------------+| id | thread_id | login_time | localname | matchname |+----+-----------+---------------------+---------------------------+-------------------------+| 1 | 5 | 2016-08-10 11:01:07 | [email protected] | [email protected]% || 2 | 6 | 2016-08-10 11:02:02 | [email protected] | [email protected]% || 3 | 7 | 2016-08-10 11:19:54 | [email protected] | [email protected]% |+----+-----------+---------------------+---------------------------+-------------------------+3 rows in set (0.00 sec)

 

可以看到thread_id為7的使用者為user_yunwei,在192.168.1.240機器上操作刪除的,完成了對資料的簡單審計。 擴充說明:1.init-connect只會在串連時執行,不會對資料庫產生大的效能影響2.init-connect是在串連時執行的動作命令,故可以用它來完成其它的功能,如:init_connect=‘SET autocommit=0‘3.init-connect不會記錄擁有super許可權的使用者記錄,為了防止init_connect語句由於語法錯誤或許可權問題而所有使用者都登陸不了的情況,保證至少super使用者能登陸並修改此值

mysql基於init-connect+binlog完成審計功能

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.