Mysql對使用者操作加審計功能——初級版

來源:互聯網
上載者:User

標籤:style   color   os   使用   ar   for   資料   sp   on   

在某些應用裡,需要知道誰對錶進行了操作,進行了什麼操作,所為責任的追朔。在MYSQL裡,可以使用觸發器實現。

1:建立測試表

mysql> create table A(a int);
Query OK, 0 rows affected (0.01 sec)

2:建立追蹤表,裡麵包含表名稱,操作類型,操作時間,操作員,操作機器IP地址

mysql> create table trace(tbname varchar(30),DML_type varchar(10),DML_time datetime,DML_user varchar(30),DML_IP varchar(30));
Query OK, 0 rows affected (0.01 sec)

3:在A表建立觸發器

create trigger insert_a after insert on  A1
for each row
insert into trace values (‘A1‘,‘INSERT‘,NOW(),substring_index(current_User(),‘@‘,1),substring_index(current_User(),‘@‘,-1));

4:插入測試資料

mysql> insert into A values(1);
Query OK, 1 row affected (0.00 sec)

mysql> select * from trace;
+--------+----------+---------------------+----------+-----------+
| tbname | DML_type | DML_time | DML_user | DML_IP |
+--------+----------+---------------------+----------+-----------+
| A | INSERT | 2010-11-29 17:58:12 | root | localhost |
+--------+----------+---------------------+----------+-----------+
1 row in set (0.00 sec)

mysql> select * from trace;
+--------+----------+---------------------+----------+--------------+
| tbname | DML_type | DML_time | DML_user | DML_IP |
+--------+----------+---------------------+----------+--------------+
| A | INSERT | 2010-11-29 17:58:12 | root | localhost |
| A | INSERT | 2010-11-29 17:58:38 | cpc | 192.168.0.30 |
+--------+----------+---------------------+----------+--------------+
2 rows in set (0.00 sec)

mysql> 

這樣,當0.30的機器以CPC使用者登入時候,對A作了一個插入的操作,在這個表裡就記錄得很詳細了。
管理好資料庫的使用者下,查詢這個表,就能夠追蹤到責任人了。

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.