自Oracle 11g起,無需設定UTL_FILE_DIR就可以使用LOGMNR對本地資料庫的日誌進行分析,以下是使用LOGMNR的DICT_FROM_ONLINE_CATALOG分析REDO和歸檔日誌的步驟,供大家參考學習: 一、分析REDO日誌的實驗 1.建立測試表,並做DML操作。 SQL create table t_test
自Oracle 11g起,無需設定UTL_FILE_DIR就可以使用LOGMNR對本地資料庫的日誌進行分析,以下是使用LOGMNR的DICT_FROM_ONLINE_CATALOG分析REDO和歸檔日誌的步驟,供大家參考學習:
一、分析REDO日誌的實驗
1.建立測試表,並做DML操作。
SQL> create table t_test(id number,name varchar2(15));
Table created.
SQL> insert into t_test values(1,'stream');
1 row created.
SQL> insert into t_test values(2,'dbdream');
1 row created.
SQL> commit;
Commit complete.
SQL> update t_test set name='streamsong' where id=1;
1 row updated.
SQL> commit;
Commit complete.
SQL> delete from t_test;
2 rows deleted.
SQL> commit;
Commit complete.
2.檢視REDO日誌的路徑。
SQL> select member from v$logfile;
MEMBER
------------------------------------------------
/u01/app/oracle/oradata/stream/STREAM/redo03.log
/u01/app/oracle/oradata/stream/STREAM/redo02.log
/u01/app/oracle/oradata/stream/STREAM/redo01.log
3.新增REDO日誌
第一個新增的日誌需指定NEW,如果確定要查詢的資訊在指定的REDO日誌內,可以只新增那個REDO日誌,而不需要再新增其他
SQL> exec dbms_logmnr.add_logfile(logfilename=>'
/u01/app/oracle/oradata/stream/STREAM/redo01.log',
options=>dbms_logmnr.new);
PL/SQL procedure successfully completed.
4.新增其他REDO日誌
不是第一個新增的日誌需指定ADDFILE。
SQL> exec dbms_logmnr.add_logfile(logfilename=>'
/u01/app/oracle/oradata/stream/STREAM/redo02.log',
options=>dbms_logmnr.addfile);
PL/SQL procedure successfully completed.
SQL> exec dbms_logmnr.add_logfile(logfilename=>'
/u01/app/oracle/oradata/stream/STREAM/redo03.log',
options=>dbms_logmnr.addfile);
PL/SQL procedure successfully completed.
5.開始對新增的REDO進行分析
SQL> exec dbms_logmnr.start_logmnr(options=>
dbms_logmnr.dict_from_online_catalog);
PL/SQL procedure successfully completed.
6.檢視LOGMNR分析後得到的資訊
LOGMNR分析後的資料會存放在v$logmnr_contents檢視中,透過查詢v$logmnr_contents檢視就可以查詢到REDO日誌的資訊。
SQL> select timestamp,sql_redo,sql_undo from v$logmnr_contents where username='SCOTT'and table_name='T_TEST';
TIMESTAMP SQL_REDO
--------- -----------------------------------------------------------
21-MAR-12 create table t_test(id number,name varchar2(15));
21-MAR-12 insert into "SCOTT"."T_TEST"("ID","NAME") values ('1','stream');
21-MAR-12 insert into "SCOTT"."T_TEST"("ID","NAME") values ('2','dbdream');
21-MAR-12 update "SCOTT"."T_TEST" set "NAME" = 'streamsong' where "NAME" =
'stream' and ROWID = 'AAAR7fAAEAAAADXAAA';
21-MAR-12 delete from "SCOTT"."T_TEST" where "ID" = '1' and "NAME" =
'streamsong' and ROWID = 'AAAR7fAAEAAAADXAAA';
21-MAR-12 delete from "SCOTT"."T_TEST" where "ID" = '2' and "NAME" =
'dbdream' and ROWID = 'AAAR7fAAEAAAADXAAB';
SQL_REDO就是執行的SQL語句,SQL_UNDO是回滾操作的SQL語句,也就是執行SQL_UNDO的相關SQL,就可以回滾對應的操作。
注:LOGMNR是SESSION級的,以上實驗第3步到第6步需在同一個SESSION中進行,SESSION斷開連線後需重新執行,否則會報以下錯誤提示。
SQL> select count(*) from v$logmnr_contents;
select count(*) from v$logmnr_contents
*
ERROR at line 1:
ORA-01306: dbms_logmnr.start_logmnr() must be invoked before selecting from
v$logmnr_contents
7.結束LOGMNR操作
由於LOGMNR是會話級的,可以用直接退出或關閉當前的終端的方式來結束LOGMNR的操作,當然,正確的結束LOGMNR操作需使用下面的命令。
SQL> exec dbms_logmnr.end_logmnr;
PL/SQL procedure successfully completed.
二、分析歸檔日誌的實驗
分析歸檔日誌的操作和分析REDO的操作基本一樣,最重要的就是準確的找到需要查詢的資訊在哪些歸檔日誌內。既然是利用LOGMNR分析歸檔日誌,資料庫一定是在歸檔模式,要不哪來的歸檔日誌,可以透過如下命令檢視資料庫是否啟用歸檔模式。
SQL> archive log list
Database log mode Archive Mode
Automatic archival Enabled
Archive destination USE_DB_RECOVERY_FILE_DEST
Oldest online log sequence 15
Next log sequence to archive 17
Current log sequence 17
可以看到當前資料庫已經開啟歸檔模式,歸檔地址是USE_DB_RECOVERY_FILE_DEST,USE_DB_RECOVERY_FILE_DEST的具體位置可以透過下面的命令檢視。
SQL> show parameter db_recove
NAME TYPE VALUE
--------------------------- ----------- -------------------------------
db_recovery_file_dest string /u01/app/oracle/flash_recovery_area
db_recovery_file_dest_size big integer 3852M
如果資料據庫開啟閃回恢復區,閃回恢復區就是預設的歸檔地址,也是RMAN備份檔案的預設存放地址,如果資料庫開啟閃回特性,這也是存放閃回日誌的預設地址。強烈建議手動修改歸檔日誌的存放地址,閃回恢復區的大小受db_recovery_file_dest_size大小的限制,已經遇到好多客戶的資料庫由於沒有指定歸檔日誌的路徑,閃回恢復區滿導致REDO日誌無法歸檔,導致資料庫掛起的情況。如何更改歸檔日誌的路徑,請參照《Oracle系統歸檔模式管理》
本實驗步驟如下:
1.切換日誌,使REDO日誌歸檔。
SQL> alter system switch logfile;
System altered.
SQL> /
System altered.
SQL> /
System altered.
2.按照歸檔日誌的時間,找到存放需要分析資訊的歸檔日誌。
[oracle@stream 2012_03_21]$ pwd
/u01/app/oracle/flash_recovery_area/STREAM/archivelog/2012_03_21
[oracle@stream 2012_03_21]$ ll
total 32196
-rw-r----- 1 oracle oinstall 26598912 Mar 21 10:55 o1_mf_1_12_7pljs9lb_.arc
-rw-r----- 1 oracle oinstall 6206976 Mar 21 14:05 o1_mf_1_13_7plvx3bc_.arc
-rw-r----- 1 oracle oinstall 100864 Mar 21 14:07 o1_mf_1_14_7plw0bgo_.arc
-rw-r----- 1 oracle oinstall 1024 Mar 21 14:07 o1_mf_1_15_7plw0d0q_.arc
-rw-r----- 1 oracle oinstall 2048 Mar 21 14:07 o1_mf_1_16_7plw0gc1_.arc
3.將歸檔日誌新增到LOGMNR。
SQL> exec dbms_logmnr.add_logfile(logfilename=>
'/u01/app/oracle/flash_recovery_area/STREAM/archivelog/
2012_03_21/o1_mf_1_13_7plvx3bc_.arc',options=>dbms_logmnr.new);
PL/SQL procedure successfully completed.
SQL> exec dbms_logmnr.add_logfile(logfilename=>
'/u01/app/oracle/flash_recovery_area/STREAM/archivelog/
2012_03_21/o1_mf_1_14_7plw0bgo_.arc',options=>dbms_logmnr.addfile);
PL/SQL procedure successfully completed.
4.開始分析。
SQL> exec dbms_logmnr.start_logmnr(options=>
dbms_logmnr.dict_from_online_catalog);
PL/SQL procedure successfully completed.
5.檢視LOGMNR分析後的資料。
SQL> select timestamp,sql_redo from v$logmnr_contents where username='SCOTT' and table_name='T_TEST';
TIMESTAMP SQL_REDO
--------- -----------------------------------------------------------------
21-MAR-12 create table t_test(id number,name varchar2(15));
21-MAR-12 insert into "SCOTT"."T_TEST"("ID","NAME") values ('1','stream');
21-MAR-12 insert into "SCOTT"."T_TEST"("ID","NAME") values ('2','dbdream');
21-MAR-12 update "SCOTT"."T_TEST" set "NAME" = 'streamsong' where "NAME" =
'stream' and ROWID = 'AAAR7fAAEAAAADXAAA';
21-MAR-12 delete from "SCOTT"."T_TEST" where "ID" = '1' and "NAME" =
'streamsong' and ROWID = 'AAAR7fAAEAAAADXAAA';
21-MAR-12 delete from "SCOTT"."T_TEST" where "ID" = '2' and "NAME" =
'dbdream' and ROWID = 'AAAR7fAAEAAAADXAAB';
6 rows selected.
6.結束LOGMNR操作。
SQL> exec dbms_logmnr.end_logmnr;
PL/SQL procedure successfully completed.
我要分享該文章 0這篇文章很贊
本文連結: | Ask Oracle社群