alter database add supplemental log data;
alter database add supplemental log data (primary key) columns;
2、字典來源:源庫日誌字典,源庫在線數據字典。
(1)源庫日誌字典:調用dbms_logmnr_d.build存儲過程把LogMiner字典提取到源庫的重做日誌。
begin
dbms_logmnr_d.build(options=>dbms_logmnr_d.store_in_redo_logs);
End;
/
下面查詢日誌日誌字典存儲位置
Select sequence#,name,dictionary_begin,dictionary_end from v$archived_log where dictionary_begin='YES' or dictionary_end='YES';
(2)源庫在線數據字典作爲LogMiner字典時,直接在啓動日誌挖掘時通過start_logmnr過程的options參數指定。
如
begin
dbms_logmnr.start_logmnr(options=>dbms_logmnr.dict_from_online_catalog);
end;
/
做破壞,然後使用日誌挖掘修復。
select ename,sal from scott.emp where deptno=30;
update scott.emp set sal=sal/2 where deptno=30;
commit;
select ename,sal from scott.emp where deptno=30;
(1)手動註冊。
begin
dbms_logmnr.add_logfile(
logfilename=>’/u01/app/oracle/o1_mf_1_138_3f3303_.arc’,
options=>dbms_logmnr.addfile);
end;
/
反覆執行可以增加多個歸檔日誌,如果採用日誌字典,則需要註冊日誌字典所在日誌。
(2)自動註冊。
使用dbms_logmnr.start_logmnr啓動挖掘會話時,使用options參數說明即可,比手動註冊方便多了。
如
a.以時間戳計算時間窗口
begin
dbms_logmnr.start_logmnr(
starttime=>to_date(’20170609 09:00:00’,’yyyymmdd hh24:mi:ss’),
endtime=>to_date(’20170609 09:10:00’,’yyyymmdd hh24:mi:ss’),
options=> dbms_logmnr.dict_from_online_catalog+dbms_logmnr.continuous_mine);
end;
/
b.利用以SCN計算時間窗口
begin
dbms_logmnr.start_logmnr(
startscn=>。。。,
endscn=>。。。,
options=> dbms_logmnr.dict_from_online_catalog+dbms_logmnr.continuous_mine);
end;
/
自動註冊的限制:挖掘庫與源庫必須是同一個庫。必須指定時間窗口,控制文件內必須具有所需日誌記錄—可以搜索v$archived_log和v$logfile顯示的範圍。
4 啓動挖掘:此時需要調用dbms_logmnr.start_logmnr,它需要預先知道兩件事:如何提供LogMiner字典(源庫在線數據字典or日誌字典)和是否註冊了日誌(手動自動)。
示例1:源庫在線數據字典,之前已經手工註冊了日誌。
begin
dbms_logmnr.start_logmnr(options=>dbms_logmnr.dict_from_online_catalog);
end;
/
示例2 源庫在線數據字典 ,日誌自動註冊。
begin
dbms_logmnr.start_logmnr(
startscn=>。。。,
endscn=>。。。,
options=> dbms_logmnr.dict_from_online_catalog+dbms_logmnr.continuous_mine);
end;
/
begin
dbms_logmnr.start_logmnr(
starttime=>to_date(’20170609 09:00:00’,’yyyymmdd hh24:mi:ss’),
endtime=>to_date(’20170609 09:10:00’,’yyyymmdd hh24:mi:ss’),
options=> dbms_logmnr.dict_from_online_catalog+dbms_logmnr.continuous_mine);
end;
/
示例3 日誌字典作爲LogMiner字典啓動挖掘會話,之前已經註冊了包含日誌字典的日誌和需要挖掘的日誌。
begin
dbms_logmnr.start_logmnr(options=>dbms_logmnr.dict_from_redo_logs);
end;
/
示例4 日誌字典作爲LogMiner字典啓動挖掘會話 自動註冊。
begin
dbms_logmnr.start_logmnr(
startscn=>2121813,
endscn=>2126140,
options=> dbms_logmnr.dict_from_redo_logs+dbms_logmnr.continuous_mine);
end;
/
完整示例:
#### manually register redo logfile,maybe need scn related to archived log;
select name,sequence#,first_change#,last_change# from v$archived_log;
SQL> select group#,first_change#,next_change# ,status from v$log ;
GROUP# FIRST_CHANGE# NEXT_CHANGE# STATUS
---------- ------------- ------------ ----------------
1 2245104 2261729 INACTIVE
2 2261729 2272294 INACTIVE
3 2272294 2.8147E+14 CURRENT
###find redo record in controlfile through v$archived_log,v$logfile;
begin
dbms_logmnr.start_logmnr(
startscn=>。。。,
endscn=>。。。,
options=>dbms_logmnr.dict_from_online_catalog+dbms_logmnr.continuous_mine);
end;
/
select '('||rownum||')' as sql#,sql_redo from v$logmnr_contents where seg_owner='SCOTT' and seg_name='EMP' and sql_redo like 'update%';
###find redo_sql and undo_sql
begin
for rec in
(select sql_undo from v$logmnr_contents where seg_owner='SCOTT' and seg_name='EMP' and sql_redo like 'update%')
loop
if rec.sql_undo is not null then
execute immediate substr(rec.sql_undo,1,length(rec.sql_undo)-1);
end if;
end loop;
commit;
end;
/