簡單使用oracle的審計語句

mengzhaoliang發表於2008-10-20

 

 

審計(Audit)用於監視使用者所執行的資料庫操作,並且Oracle會將審計跟蹤結果存放到OS檔案或資料庫中。


1、使用審計,需要啟用審計
SQL> conn /as sysdba
已連線。
SQL> show parameter audit_sys_operations;

NAME                                 TYPE        VALUE                         
------------------------------------ ----------- ------------------------------
audit_sys_operations                 boolean     FALSE                         
SQL> show parameter audit_trail;

NAME                                 TYPE        VALUE                         
------------------------------------ ----------- ------------------------------
audit_trail                          string      NONE                          
SQL> alter system set audit_sys_operations=TRUE scope=spfile;

系統已更改。

                    
SQL> alter system set audit_trail=db scope=spfile;

系統已更改。


SQL> startup force
ORACLE 例程已經啟動。

Total System Global Area  289406976 bytes                                      
Fixed Size                  1248576 bytes                                      
Variable Size              79692480 bytes                                      
Database Buffers          201326592 bytes                                      
Redo Buffers                7139328 bytes                                      
資料庫裝載完畢。
資料庫已經開啟。
SQL> show parameter audit;

NAME                                 TYPE        VALUE                         
------------------------------------ ----------- ------------------------------
audit_file_dest                      string      D:\ORACLE\PRODUCT\10.2.0\ADMIN
                                                 \ORCL\ADUMP                   
audit_sys_operations                 boolean     TRUE                          
audit_trail                          string      DB                            

 

 


2、審計dept表

SQL> audit all on dept;

審計已成功。
                                                              

SQL> conn mzl/mzl
已連線。
SQL> select * from dept;

    DEPTNO DNAME          LOC                                                  
---------- -------------- -------------                                        
        10 ACCOUNTING     NEW YORK                                             
        20 RESEARCH       DALLAS                                               
        30 SALES          CHICAGO                                              
        40 OPERATIONS     PanJin                                               
        80 mengzhaoliang  beijing                                              

SQL> insert into dept
  2  values (90,'test','PanJin');

已建立 1 行。

SQL> commit;

提交完成。

SQL> conn scott/mzl
已連線。


SQL> insert into dept
  2  values(60,'June','ShangHai');

已建立 1 行。

SQL> commit;

提交完成。


3、檢視審計結果
SQL> conn /as sysdba
已連線。
SQL> select count(*) from dba_audit_trail;

  COUNT(*)                                                                     
----------                                                                     
         2                                                                     

SQL> select username,ses_actions,obj_name,
  2  to_char(timestamp,'YYYY-MM-DD HH24:MI:SS')
  3  from dba_audit_trail;

USERNAME                       SES_ACTIONS                                     
------------------------------ -------------------                             
OBJ_NAME                                                                       
--------------------------------------------------------------------------------
TO_CHAR(TIMESTAMP,'                                                            
-------------------                                                            
MZL                            ------S--S------                                
DEPT                                                                           
2008-10-20 10:28:08                                                            
                                                                               
SCOTT                          ------B---------                                
DEPT                                                                           
2008-10-20 10:29:04                                                            

USERNAME                       SES_ACTIONS                                     
------------------------------ -------------------                             
OBJ_NAME                                                                       
--------------------------------------------------------------------------------
TO_CHAR(TIMESTAMP,'                                                            
-------------------                                                            
                                                                               

                                                                

                                                                               

 

 

SQL> conn mzl/mzl
已連線。

4、禁止審計
SQL> noaudit all on dept;

審計未成功。

                                                                

5、mzl使用者為dba的使用者,使用精細審計,這樣可以知道具體的使用者更新的資料
SQL> show user;
USER 為 "MZL"


6、審計dept表
SQL> exec dbms_fga.add_policy(object_name=>'dept',policy_name=>'chk_dept',-
> statement_types=>'insert,update,delete,select');

PL/SQL 過程已成功完成。

SQL> select count(*) from dba_fga_audit_trail;

  COUNT(*)                                                                     
----------                                                                     
         0                                                                     

SQL> select * from dept;

    DEPTNO DNAME          LOC                                                  
---------- -------------- -------------                                        
        10 ACCOUNTING     NEW YORK                                             
        20 RESEARCH       DALLAS                                               
        30 SALES          CHICAGO                                              
        40 OPERATIONS     PanJin                                               
        60 June           ShangHai                                             
        80 mengzhaoliang  beijing                                              
        90 test           PanJin                                               

已選擇7行。

SQL> delete dept where deptno=90;

已刪除 1 行。

SQL> commit;

提交完成。

SQL> conn scott/mzl
已連線。


SQL> update dept set loc='PanJin' where deptno=60;

已更新 1 行。

SQL> commit;

提交完成。

SQL>  conn mzl/mzl
已連線。
SQL> select count(*) from dba_fga_audit_trail;

  COUNT(*)                                                                     
----------                                                                     
         3                                                                     


SQL> col sql_text for a40
SQL> l
  1* select db_user,sql_text from dba_fga_audit_trail
SQL> /

DB_USER                        SQL_TEXT                                        
------------------------------ ----------------------------------------        
MZL                            select * from dept                              
MZL                            delete dept where deptno=90                     
SCOTT                          update dept set loc='PanJin' where deptn        
                               o=60                                            
                                                                               
7、取消精細審計                                
SQL> exec dbms_fga.disable_policy(object_name=>'dept',-
> policy_name=>'chk_dept');

PL/SQL 過程已成功完成。


 

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12778571/viewspace-473207/,如需轉載,請註明出處,否則將追究法律責任。

相關文章