測試oracle11G使用logmnr分析日誌
事前準備:
1、設定資料庫強制歸檔:
驗證是否開啟focelogging
SQL>select force_logging from v$database;
若沒有啟用,可使用下面語法開啟強制歸檔:
SQL>alter database force logging; #開啟force logging
2、開啟最小附加日誌:最小附加日誌(Minimal supplemental logging):是開啟logmnr的最低日誌要求。
驗證是否開啟最小附加日誌:
SQL> SELECT supplemental_log_data_min FROM v$database;
若沒有啟用,可使用下面語法開啟最小附加日誌:
SQL> ALTER DATABASE ADD SUPPLEMENTAL LOG DATA;
3、設定日期格式,,如果不設定的話,最後顯示的結果只有日月年,沒有具體的時間點:
SQL> ALTER SESSION SET NLS_DATE_FORMAT = 'dd-mon-yyyy hh24:mi:ss';
測試開始:
1、建立測試表test,並進行insert、update、delete操作:
SQL> create table test (id number, name varchar(10) );
Table created.
SQL> insert into test values (1,'JACK');
1 row created.
SQL> insert into test values (2,'LISA');
1 row created.
SQL> select * from test;
ID NAME
---------- --------------------
1 JACK
2 LISA
SQL> update test set name ='ELLA' where id=2;
1 row updated.
SQL> select * from test;
ID NAME
---------- --------------------
1 JACK
2 ELLA
SQL> delete from test where id=1;
1 row deleted.
SQL> select * from test;
ID NAME
---------- --------------------
2 ELLA
2、完成insert、update、delete操作後切換日誌或者手工進行歸檔:
SQL> alter system archive log current;
System altered.
3、進入歸檔日誌檔案所在路徑:
[[email protected] 2018_11_06]$ ll -h
total 9.6M
-rw-r----- 1 oracle dba 5.4M Nov 6 09:00 o1_mf_1_33_fy1sww22_.arc
-rw-r----- 1 oracle dba 1.8M Nov 6 09:22 o1_mf_1_34_fy1v6oyj_.arc
-rw-r----- 1 oracle dba 2.5M Nov 6 10:07 o1_mf_1_35_fy1xtp1l_.arc
4、新增需要進行解析的日誌檔案:
SQL> execute dbms_logmnr.add_logfile(LOGFILENAME =>'/data/oracle/flash_recovery_area/MCJ/archivelog/2018_11_06/o1_mf_1_33_fy1sww22_.arc', OPTIONS => dbms_logmnr.new);
PL/SQL procedure successfully completed.
SQL> execute dbms_logmnr.add_logfile('/data/oracle/flash_recovery_area/MCJ/archivelog/2018_11_06/o1_mf_1_34_fy1v6oyj_.arc',dbms_logmnr.addfile);
PL/SQL procedure successfully completed.
SQL> execute dbms_logmnr.add_logfile('/data/oracle/flash_recovery_area/MCJ/archivelog/2018_11_06/o1_mf_1_35_fy1xtp1l_.arc',dbms_logmnr.addfile);
PL/SQL procedure successfully completed.
5、開始分析:
SQL> execute dbms_logmnr.start_logmnr(options=>dbms_logmnr.dict_from_online_catalog);
PL/SQL procedure successfully completed.
6、設定顯示格式:
SQL> set lines 32565
SQL> set pages 50000
SQL> col sql_redo format a9000
7、用spool命令將oracle所有的操作結果寫入到指定的檔案中:
SQL> spool /data/oracle/flash_recovery_area/MCJ/archivelog/2018_11_06/test_20181106.txt
SQL> select TIMESTAMP,USERNAME,OS_USERNAME,MACHINE_NAME,OPERATION,sql_redo from v$logmnr_contents where table_name='TEST' and sql_redo<>'Unsupported' order by timestamp;
06-nov-2018 09:55:48 SYS oracle mas DDL drop table test;
06-nov-2018 09:56:39 SYS oracle mas DDL create table test (id number, name varchar(10) );
06-nov-2018 09:57:10 SYS oracle mas INSERT insert into "SYS"."TEST"("ID","NAME") values ('1','JACK');
06-nov-2018 09:58:04 SYS oracle mas INSERT insert into "SYS"."TEST"("ID","NAME") values ('2','LISA');
06-nov-2018 10:02:25 SYS oracle mas UPDATE update "SYS"."TEST" set "NAME" = 'ELLA' where "NAME" = 'LISA' and ROWID = 'AAAR8qAABAAAUf5AAB';
06-nov-2018 10:02:58 SYS oracle mas DELETE delete from "SYS"."TEST" where "ID" = '1' and "NAME" = 'JACK' and ROWID = 'AAAR8qAABAAAUf5AAA';
6 rows selected.
SQL> spool off
8、結束分析:
SQL> exec dbms_logmnr.end_logmnr;
PL/SQL procedure successfully completed.
9、找到spool命令生成的檔案:
[[email protected] 2018_11_06]$ ll -h test_20181106.txt
-rw-r--r-- 1 oracle dba 252K Nov 6 10:22 test_20181106.txt
10、此時這個檔案裡面的格式較亂,需要進行格式調整:
[[email protected] 2018_11_06]$ sed -i 's/[ ][ ]*/ /g' test_20181106.txt
11、最後檢視結果:(結果列分別對應TIMESTAMP,USERNAME,OS_USERNAME,MACHINE_NAME,OPERATION,sql_redo)
SQL> select TIMESTAMP,USERNAME,OS_USERNAME,MACHINE_NAME,OPERATION,sql_redo from v$logmnr_contents where table_name='TEST' and sql_redo<>'Unsupported' order by timestamp;
TIMESTAMP USERNAME OS_USERNAME MACHINE_NAME OPERATION SQL_REDO
06-nov-2018 09:55:48 SYS oracle mas DDL drop table test;
06-nov-2018 09:56:39 SYS oracle mas DDL create table test (id number, name varchar(10) );
06-nov-2018 09:57:10 SYS oracle mas INSERT insert into "SYS"."TEST"("ID","NAME") values ('1','JACK');
06-nov-2018 09:58:04 SYS oracle mas INSERT insert into "SYS"."TEST"("ID","NAME") values ('2','LISA');
06-nov-2018 10:02:25 SYS oracle mas UPDATE update "SYS"."TEST" set "NAME" = 'ELLA' where "NAME" = 'LISA' and ROWID = 'AAAR8qAABAAAUf5AAB';
06-nov-2018 10:02:58 SYS oracle mas DELETE delete from "SYS"."TEST" where "ID" = '1' and "NAME" = 'JACK' and ROWID = 'AAAR8qAABAAAUf5AAA';
注:第7步在v$logmnr_contents查詢操作情況時,可根據實際需求新增其他欄位,例如操作時間、操作使用者、操作型別等等。
12、附上v$logmnr_contents欄位:
>SQL desc v$logmnr_contents;