ORACLE审计DDL语句

    • 创建存储DDL语句的表
    • 创建 捕获DDL语句的触发器
    • 创建审计DDL的触发器成功,并且是生效的
    • 查看并确认隐藏参数_system_trig_enabled 为 true:
    • 创建测试账户test 进行测试

创建存储DDL语句的表

create table audit_ddl
(
opertime timestamp PRIMARY KEY,
ip varchar2(20),
hostname varchar2(30),
operation varchar2(30),
object_type varchar2(30),
object_name varchar2(30),
sql_stmt clob,
db_schema varchar2(30)
);

创建 捕获DDL语句的触发器

create or replace trigger trg_audit_ddl
  after ddl on database
DECLARE
  PRAGMA AUTONOMOUS_TRANSACTION;
  n        NUMBER;
  stmt     clob := NULL;
  sql_text ora_name_list_t;
BEGIN
  n := ora_sql_txt(sql_text);
  FOR i IN 1 .. n LOOP
    stmt := stmt || sql_text(i);
  END LOOP;
  INSERT INTO audit_ddl
    (opertime, ip, hostname, operation, object_type, object_name, sql_stmt,db_schema)
  VALUES
    (systimestamp,
     sys_context('userenv', 'ip_address'),
     sys_context('userenv', 'terminal'),
     ora_sysevent,
     ora_dict_obj_type,
     ora_dict_obj_name,
     stmt,
     user
   );
  COMMIT;
END;
/

创建审计DDL的触发器成功,并且是生效的

SYS@ >select t.object_name,t.object_type,t.status,t.last_ddl_time from  dba_objects t where t.object_type='TRIGGER' and t.object_name='TRG_AUDIT_DDL';

OBJECT_NAME          OBJECT_TYPE         STATUS  LAST_DDL_TIME
-------------------- ------------------- ------- ------------------
TRG_AUDIT_DDL        TRIGGER             VALID   03-MAY-18

查看并确认隐藏参数_system_trig_enabled 为 true:

SYS@ >SELECT   ksppinm, ksppstvl, ksppdesc FROM   x$ksppi x, x$ksppcv y WHERE   x.indx = y.indx AND  ksppinm = '_system_trig_enabled';

KSPPINM              KSPPSTVL             KSPPDESC
-------------------- -------------------- ----------------------------------------
_system_trig_enabled TRUE                 are system triggers enabled

创建测试账户test 进行测试

SYS@  >create user test identified by 123456;
User created.

SYS@ >grant connect,resource to test;

Grant succeeded.

SYS@  >conn test/123456;
Connected.

TEST@ >create table audit_ddl_test(id number,name varchar2(20));

Table created.
TEST@  >insert into audit_ddl_test values(1,'wwww');

1 row created.

TEST@  >commit;

Commit complete.

TEST@ >truncate table audit_ddl_test;

Table truncated.

TEST@ >alter table audit_ddl_test add (insert_date date default(sysdate));

Table altered.

TEST@>desc audit_ddl_test
 Name                                                                                      Null?    Type
 ----------------------------------------------------------------------------------------- -------- ------------------------------------------------------------
 ID                                                                                                 NUMBER
 NAME                                                                                               VARCHAR2(20)
 INSERT_DATE                                                                                        DATE

TEST@ >drop table audit_ddl_test;

Table dropped.
SYS@  >select * from audit_ddl where db_schema='TEST';

OPERTIME        IP              HOSTNAME        OPERATION                      OBJECT_TYPE                    OBJECT_NAME
--------------- --------------- --------------- ------------------------------ ------------------------------ --------------------
SQL_STMT                                                                         DB_SCHEMA
-------------------------------------------------------------------------------- ---------------
03-MAY-18 04.12                 pts/1           CREATE                         TABLE                          AUDIT_DDL_TEST
.13.072252 PM
create table audit_ddl_test(id number,name varchar2(20))                         TEST


03-MAY-18 04.14                 pts/1           TRUNCATE                       TABLE                          AUDIT_DDL_TEST
.27.000973 PM
truncate table audit_ddl_test                                                    TEST

03-MAY-18 04.16                 pts/1           ALTER                          TABLE                          AUDIT_DDL_TEST
.48.211531 PM
alter table audit_ddl_test add (insert_date date default(sys                     TEST
date))

03-MAY-18 04.17                 pts/1           ALTER                          TABLE                          AUDIT_DDL_TEST
.20.898208 PM
ALTER TABLE "TEST"."AUDIT_DDL_TEST" RENAME TO "BIN$a0nVHhtJf                     TEST
kTgU5cAAArVkA==$0"

03-MAY-18 04.17                 pts/1           DROP                           TABLE                          AUDIT_DDL_TEST
.20.900534 PM
drop table audit_ddl_test                                                        TEST

你可能感兴趣的:(Oracle,Other)