如何?MySQL資料庫使用方式的審計

來源:互聯網
上載者:User

標籤:如何   tle   title   使用者資訊   測試   mysql   select   分離   1.4   

如何?MySQL資料庫使用方式的審計 最佳答案 
mysql的審計功能

mysql伺服器自身沒有提供審計功能,但是我們可以使用init-connect + binlog的方法進行mysql的Action Trail。由於mysql binlog記錄了所有對資料庫長生實際修改的sql語句,及其執行時間,和connection_id但是卻沒有記錄connection_id對應的詳細使用者資訊。在後期審計進行行為追蹤時,根據binlog記錄的行為及對應的connection-id 結合 之前串連日誌記錄 進行分析,得出最後的結論。

1. 設定init-connect
1.1 建立用於存放串連日誌的資料庫和表
create database accesslog;
CREATE TABLE accesslog.accesslog (`id` int(11) primary key auto_increment, `time` timestamp, `localname` varchar(30), `matchname` varchar(30))

1.2 建立使用者權限
可用現成的root使用者用於資訊的讀取
grant select on accesslog.* to root;
如果存在具有to *.* 許可權的使用者需要進行限制。

這裡還需要注意使用者必須對accesslog表具有insert許可權
grant select on accesslog.* to [email protected]’%’;

1.3 設定init-connect
在[mysqld]下添加以下設定:
init-connect=’insertinto accesslog.accesslog(id, time, localname, matchname)
values(connection_id(),now(),user(),current_user());’
------注意user()和current_user()的區別
log-bin=xxx
這裡必須開啟binlog

1.4 重啟資料庫生效
shell> /etc/init.d/mysql restart

2. 記錄追蹤
2.1 thread_id確認

可以用以下語句定位語句執行人

Tencent:~ # mysqlbinlog --start-datetime=‘2011-01-26 16:00:00‘
--stop-datetime=‘2011-01-26 17:00:00‘ /var/lib/mysql/mysql-bin.000010
| grep -B 5 ‘wsj‘

COMMIT/*!*/;
# at 767
#110126 16:16:43 server id 1 end_log_pos 872 Query thread_id=19 exec_time=0 error_code=0
use test/*!*/;
SET TIMESTAMP=1296029803/*!*/;
create table wsj(id int unsigned not null)
--
BEGIN
/*!*/;
# at 940
#110126 16:16:57 server id 1 end_log_pos 1033 Query thread_id=19 exec_time=0 error_code=0
SET TIMESTAMP=1296029817/*!*/;
insert into wsj(id) values (1)
--
BEGIN
/*!*/;
# at 1128
#110126 16:16:58 server id 1 end_log_pos 1221 Query thread_id=19 exec_time=0 error_code=0
SET TIMESTAMP=1296029818/*!*/;
insert into wsj(id) values (2)

2.2 使用者確認

thread_id 確認以後,找到元兇就只是一條sql語句的問題了。

mysql> select * from accesslog where id=19;
+----+---------------------+---------------------+-----------+
| id | time | localname | matchname |
+----+---------------------+---------------------+-----------+
| 19 | 2011-01-26 16:15:54 | [email protected] | [email protected]% |
+----+---------------------+---------------------+-----------+
1 row in set (0.00 sec)

3. Q
Q:使用init-connect會影響伺服器效能嗎?
A:理論上,只會在使用者每次串連時往資料庫裡插入一條記錄,不會對資料庫產生很大影響。除非串連頻率非常高(當然,這個時候需要注意的就是如何進行串連複用和控制,而非是不是要用這種方法的問題了)---如果採用長串連並且緩衝的話,可以提高效能

Q:access-log表如何維護?
A: 由於是一個log系統,推薦使用archive儲存引擎,有利於資料厄壓縮存放。如果資料庫連接數量很大的話,建議一定時間做一次資料匯出,然後清表。
Q:表有其他用途嗎?
A:有!access-log表當然不只用於審計,當然也可以用於對於資料庫連接的情況進行資料分析,例如每日串連數分布圖等等,只有想不到沒有做不到。---可以用來測試讀寫分離,驗證負載平衡等

Q:會有遺漏的記錄嗎?
A:會的,init-connect 是不會在super使用者登入時執行的。所以access-log裡不會有資料庫超級使用者的記錄,這也是為什麼我們不主張多個超級使用者,並且多人使用的原因。--這種審計不會記錄root等具有super許可權的帳號對資料庫的訪問

如何?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.