1. 程式人生 > >LOGMNR日誌挖掘工具的使用

LOGMNR日誌挖掘工具的使用

LOGMNR日誌挖掘是oracle自帶的日誌挖掘工具,可以用於對oracle資料DDL和DML操作做問題診斷。

LOGMNR 對redo log進行挖掘,找出在某個時間所作的DDL或DML操作。

安裝LOGMNR

@?/rdbms/admin/dbmslm
@?/rdbms/admin/dbmslmd

與logmnr相關的檢視有:

v$logmnr_contents:This view contains log history information.
v$logmnr_logs:This view contains log information.

需要強調一點是:LOGMNR 是與session有關的,即只可以在一個session中完成對日誌挖掘操作,中途切換session進行操作將無法檢視logmnr結果。

初步使用LOGMNR實施日誌挖掘操作

1、新增logfile

檢視當前正在被使用的logfile [current]
SQL> select member,a.group#,a.status from v$log a join v$logfile b on a.group#=b.group#;
exec dbms_logmnr.add_logfile(logfilename => '+EZHOUDATA/ezhou/onlinelog/group_2.282.802786473',options => dbms_logmnr.new);
 檢視剛新增的logfile:
select filename from v$logmnr_logs;

執行部分SQL語句,等後面的操作中做查詢用:
   create user ezhou identified by ezhou;
   alter user ezhou default tablespace users;
   alter user ezhou temporary tablespace temp;
   grant connect,resource to ezhou;
   alter user ezhou quota unlimited on users;
   
   connect ezhou/ezhou
   create table ezhou_test(id number,name varchar2(10));

   

2、開始挖掘日誌


exec dbms_logmnr.start_logmnr() ;

col seg_owner for a10
col table_name for a15
col table_space for a15
col sql_redo for a60
col sql_undo for a60
set linesize 2000
select TABLE_SPACE,SQL_REDO
from v$logmnr_contents where seg_owner='EZHOU';

select SQL_REDO,TIMESTAMP ,SQL_UNDO
from v$logmnr_contents where SQL_REDO like '%grant%' OR seg_owner='EZHOU';

alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';


3、關閉LOGMNR 會話級的操作

exec dbms_logmnr.end_logmnr();

修復無法檢視insert語句的bug.
alter database add supplemental log data(primary key,unique index) columns;
alter system switch logfile;
alter system checkpoint;

SQL> select member,a.group#,a.status from v$log a join v$logfile b on a.group#=b.group#;
exec dbms_logmnr.add_logfile(logfilename => '+EZHOUDATA/ezhou/onlinelog/group_1.281.802786459',options => dbms_logmnr.new);

exec dbms_logmnr.start_logmnr(options => dbms_logmnr.dict_from_online_catalog) ;

exec dbms_logmnr.start_logmnr() ;
exec dbms_logmnr.end_logmnr();

-------------------------
select filename from v$logmnr_logs;

col seg_owner for a10
col table_name for a15
col table_space for a15
col sql_redo for a60
col sql_undo for a60
set linesize 2000
select TABLE_SPACE,SQL_REDO
from v$logmnr_contents where seg_owner='EZHOU';

select SQL_REDO,TIMESTAMP ,SQL_UNDO
from v$logmnr_contents where SQL_REDO like '%ezhou%' OR seg_owner='EZHOU';

alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';

----------------------------------------------------------
檢視歸檔日誌
select view_name from v$fixed_view_definition where view_name like '%ARCH%';

 select owner,object_name,object_id,object_type
 from dba_objects
 where object_name='EZHOU_TEST';

1, select name from V$ARCHIVED_LOG;

偷個懶,使用select語句生成新增logfile的語句:

select 'exec dbms_logmnr.add_logfile(logfilename => ''' || name || ','' options => dbms_logmnr.addfile);' from V$ARCHIVED_LOG;
exec dbms_logmnr.add_logfile(logfilename => '/u01/app/oracle/product/10.2/db_1/dbs/arch1_8_802786218.dbf', options  => dbms_logmnr.new);
exec dbms_logmnr.add_logfile(logfilename => '/u01/app/oracle/product/10.2/db_1/dbs/arch1_9_802786218.dbf', options  => dbms_logmnr.addfile);
exec dbms_logmnr.add_logfile(logfilename => '/u01/app/oracle/product/10.2/db_1/dbs/arch1_10_802786218.dbf', options  => dbms_logmnr.addfile);
----------------------------------------------------------------------------

2,exec dbms_logmnr.start_logmnr() ;

從v$logmnr_contents檢視中查詢sql語句:

select SQL_REDO,TIMESTAMP ,SQL_UNDO
from v$logmnr_contents where SQL_REDO like '%insert%' and SQL_REDO like '%52593%';
----------------------------------------------------------------------------

3,exec dbms_logmnr.end_logmnr();

關閉logmnr。