標籤:資料庫 動態 審計
作為一名dba,有時候,總會遇到資料庫某個庫,某個表,某個欄位異常,或者資料被莫名的刪除了,這個時候大家各種推斷是不是bug了啊,是不是被黑了啊。。。這個時候一個審計功能就凸顯出來了。
mariadb資料庫對審計外掛程式(server_audit.so)支援比較良好,今天我們進行一個簡單安裝和測試
安裝方法也很簡單:INSTALL SONAME "server_audit.so"
650) this.width=650;" title="無標題.png" src="http://s1.51cto.com/wyfs02/M02/8C/2A/wKioL1hke-mB-kB3AABLFurffjA252.png-wh_500x0-wm_3-wmp_4-s_2453716365.png" alt="wKioL1hke-mB-kB3AABLFurffjA252.png-wh_50" />
這樣就安裝完成了,我們可以看看對應的參數變數
650) this.width=650;" title="無標題.png" src="http://s4.51cto.com/wyfs02/M02/8C/2A/wKioL1hkfVvSvU0tAACJdy2Oma0984.png-wh_500x0-wm_3-wmp_4-s_166257261.png" alt="wKioL1hkfVvSvU0tAACJdy2Oma0984.png-wh_50" />
這些參數和變數都是可以動態設定的,具體參數說明,見官網:https://mariadb.com/kb/en/mariadb/server_audit-system-variables/
一般我們主要設定一下幾個參數就可以了
set global server_audit_events = QUERY /*這個參數有三個值connect,table,query
set global server_audit_file_rotate_size = 524288000 /*每個記錄檔的大小
sset global erver_audit_file_rotations = 200
set global server_audit_file_path ="/data/mysql/auditlog/server_audit.log" /*log檔案路徑 必須保證/data/mysql/auditlog/這個路徑是存在的!而且 chown mysql.mysql ,否則將 開啟審計功能 後,將導致mysql報錯,甚至服務掛掉!
set global server_audit_logging = 1 /*開啟審計功能 強烈建議:在設定好以下參數完成後,再開啟
設定完成後,再將對應參數添加到設定檔中
server_audit_logging = 1
server_audit_events = QUERY
server_audit_file_rotate_size = 524288000
server_audit_file_rotations = 200
server_audit_file_path =/data/mysql/auditlog/server_audit.log
下面我們看看實際效果,對比一下server_audit_events這個參數connet,table,query三個不同值的日誌對比;
1.參數為server_audit_events= query時
650) this.width=650;" title="無標題.png" src="http://s3.51cto.com/wyfs02/M00/8C/2F/wKiom1hkheHC8a2PAACh6P8BAgg046.png-wh_500x0-wm_3-wmp_4-s_2560620423.png" alt="wKiom1hkheHC8a2PAACh6P8BAgg046.png-wh_50" />
日誌內容:
20161229 11:35:39,localhost.localdomain,root,localhost,42,745,QUERY,mysql,‘show databases‘,0
20161229 11:35:55,localhost.localdomain,root,localhost,42,746,QUERY,mysql,‘create database yhtest‘,0
20161229 11:36:03,localhost.localdomain,root,localhost,42,747,QUERY,mysql,‘SELECT DATABASE()‘,0
20161229 11:36:03,localhost.localdomain,root,localhost,42,749,QUERY,yhtest,‘show databases‘,0
20161229 11:36:03,localhost.localdomain,root,localhost,42,750,QUERY,yhtest,‘show tables‘,0
20161229 11:36:42,localhost.localdomain,root,localhost,42,751,QUERY,yhtest,‘create table yhtest(a int primary ,b int)‘,1064
20161229 11:36:56,localhost.localdomain,root,localhost,42,752,QUERY,yhtest,‘create table yhtest(a int primary key ,b int)‘,0
20161229 11:37:35,localhost.localdomain,root,localhost,42,753,QUERY,yhtest,‘insert into yhtest value(1,1),(2,2),(3,3)‘,0
20161229 11:37:46,localhost.localdomain,root,localhost,42,754,QUERY,yhtest,‘select * from yhtest‘,0
20161229 11:38:07,localhost.localdomain,root,localhost,42,755,QUERY,yhtest,‘delete from yhtest where a=1‘,0
20161229 11:38:15,localhost.localdomain,root,localhost,42,756,QUERY,yhtest,‘drop table yhtest‘,0
20161229 15:45:07,localhost.localdomain,root,localhost,42,757,QUERY,yhtest,‘show variables like \‘server%\‘‘,0
2.參數為server_audit_events=connect時 20161229 16:09:50,localhost.localdomain,root,localhost,42,0,DISCONNECT,yhtest,,0
20161229 16:09:54,localhost.localdomain,root,localhost,43,0,CONNECT,,,0
20161229 16:11:37,localhost.localdomain,root,localhost,43,0,DISCONNECT,yhtest2,,0
20161229 16:11:39,localhost.localdomain,root,localhost,44,0,CONNECT,,,0
20161229 16:12:06,localhost.localdomain,root,localhost,44,0,DISCONNECT,mysql,,0
3.參數為server_audit_events=table時 20161229 16:17:52,localhost.localdomain,root,localhost,47,857,CREATE,yhtest,t2,
20161229 16:17:59,localhost.localdomain,root,localhost,47,858,WRITE,mysql,table_stats,
20161229 16:17:59,localhost.localdomain,root,localhost,47,858,WRITE,mysql,column_stats,
20161229 16:17:59,localhost.localdomain,root,localhost,47,858,WRITE,mysql,index_stats,
20161229 16:17:59,localhost.localdomain,root,localhost,47,858,DROP,yhtest,t2,
20161229 16:18:04,localhost.localdomain,root,localhost,47,859,CREATE,yhtest,t3,
20161229 16:18:27,localhost.localdomain,root,localhost,47,860,WRITE,yhtest,t3,
20161229 16:19:04,localhost.localdomain,root,localhost,47,861,WRITE,yhtest,t3,
20161229 16:19:18,localhost.localdomain,root,localhost,47,862,WRITE,mysql,table_stats,
20161229 16:19:18,localhost.localdomain,root,localhost,47,862,WRITE,mysql,column_stats,
20161229 16:19:18,localhost.localdomain,root,localhost,47,862,WRITE,mysql,index_stats,
20161229 16:19:18,localhost.localdomain,root,localhost,47,862,DROP,yhtest,t3,
從上面可以看出,我們需要一般我們使用query就足夠了,需要注意的是我們在使用這個外掛程式的時候需要注意磁碟空間,如果資料庫操作頻繁,可能產生大量的日誌!
資料庫內建審計功能