oracle之二日志挖掘log miner
日志挖掘 log miner
6.1 log miner的作用:
数据库恢复中有时会需要对Redo log进行分析, 要会使用log miner,以便确定要恢复的时间点或SCN
6.2 有两种日志挖掘方法 针对DML和DDL,整理如下 :
6.2.1 对语句DML进行日志挖掘:
1)添加database补充日志
SQL>ALTER DATABASE ADD SUPPLEMENTAL LOG DATA;
//注意:通过PL/SQL包的DML的日志挖掘,这步要先执行,在此之后的DML操作才能从日志里挖到。
2)确定要分析的日志范围,添加日志,分析
SQL>execute dbms_logmnr.add_logfile(logfilename=>'归档日志',options=>dbms_logmnr.new); //第一个要加载的日志文件
SQL>execute dbms_logmnr.add_logfile(logfilename=>'补充日志',options=>dbms_logmnr.addfile); //可以反复添加补充多个日志文件
3)执行logmnr 分析
SQL>execute dbms_logmnr.start_logmnr(options=>dbms_logmnr.dict_from_online_catalog);
4)查询分析结果,可以设置时间格式,也可以在显示方式里再确定格式.
SQL>select username,scn,timestamp,sql_redo from v$logmnr_contents where seg_name='表名';
5)关闭日志分析
SQL>execute dbms_logmnr.end_logmnr;
例
session1 sys:
11:33:20 SQL> ALTER DATABASE ADD SUPPLEMENTAL LOG DATA;
session2 scott:
09:44:08 SQL> create table a (id int);
09:44:20 SQL> insert into a values(1);
09:44:29 SQL> update a set id=5;
09:44:45 SQL> commit;
09:44:47 SQL> delete a; //delete这条没有commit,我们要证明uncommit的DML操作也写入了日志。
这个session2留着,换session1进入sys
session1 sys:
11:32:12 SQL> select * from v$log;
GROUP# THREAD# SEQUENCE# BYTES MEMBERS ARCHIVED STATUS FIRST_CHANGE# FIRST_TIME
---------- ---------- ---------- ---------- ---------- -------- ---------------- ------------- -----------
1 1 26 52428800 2 NO CURRENT 2257870 2012-7-23 9
2 1 25 52428800 2 YES INACTIVE 2257866 2012-7-23 9
3 1 23 52428800 2 YES INACTIVE 2257862 2012-7-23 9
4 1 24 52428800 2 YES INACTIVE 2257864 2012-7-23 9
//上面a 表的DML操作都写进了current组里,记住sequence#是26号。然后手工切换当前日志到archive里。
11:32:18 SQL> alter system switch logfile;
11:33:00 SQL> /
11:33:02 SQL> /
11:33:02 SQL> /
11:33:09 SQL> select name from v$archived_log;
/u01/disk1/timran/arch_1_789252862_21.log
/u01/disk1/timran/arch_1_789252862_22.log
/u01/disk1/timran/arch_1_789252862_23.log
/u01/disk1/timran/arch_1_789252862_24.log
/u01/disk1/timran/arch_1_789252862_25.log
/u01/disk1/timran/arch_1_789252862_26.log
/u01/disk1/timran/arch_1_789252862_27.log
/u01/disk1/timran/arch_1_789252862_28.log
/u01/disk1/timran/arch_1_789252862_29.log
62 rows selected
//切换后,sequence#26的日志的应该对应的是/u01/disk1/timran/arch_1_789252862_26.log
11:33:48 SQL> execute dbms_logmnr.add_logfile(logfilename=>'/u01/data/urpdb/redo03.log',options=>dbms_logmnr.new);
11:34:13 SQL> execute dbms_logmnr.start_logmnr(options=>dbms_logmnr.dict_from_online_catalog);
11:34:49 SQL>select scn,to_char(timestamp,'yyyy-mm-dd hh24:mi:ss') timestamp,sql_redo from v$logmnr_contents where seg_name='AA';
SCN TIMESTAMP SQL_REDO
---------- ------------------- --------------------------------------------------------------------------------
2258232 2012-07-23 09:43:16 drop table a purge;
2258334 2012-07-23 09:44:20 create table a (id int);
2258341 2012-07-23 09:44:28 insert into "SCOTT"."A"("ID") values ('1');
2258349 2012-07-23 09:44:46 update "SCOTT"."A" set "ID" = '5' where "ID" = '1' and ROWID = 'AAANBAAAEAAAAGEA
2258353 2012-07-23 09:44:52 delete from "SCOTT"."A" where "ID" = '5' and ROWID = 'AAANBAAAEAAAAGEAAA';
6.2.2 对DDL的操作的log miner:
1)如果是第一次做,先要建好logmnr目录,
设置logmnr 参数,存放数据字典文件dict01.ora
$ mkdir /home/oracle/logmnr
SQL> alter system set utl_file_dir='/home/oracle/logmnr' scope=spfile;
2)建立数据字典文件dict.ora
SQL> execute dbms_logmnr_d.build('dict01.ora','/home/oracle/logmnr',dbms_logmnr_d.store_in_flat_file);
3)添加日志分析
SQL> alter system switch logfile;
SQL> execute dbms_logmnr.add_logfile(logfilename=>'归档文件',options=>dbms_logmnr.new);
SQL> execute dbms_logmnr.add_logfile(logfilename=>'归档文件',options=>dbms_logmnr.addfile);
4)执行分析
SQL> execute dbms_logmnr.start_logmnr(dictfilename=>'/home/oracle/logmnr/dict01.ora',options=>dbms_logmnr.ddl_dict_tracking);
5)查看分析结果
SQL> select username,scn,to_char(timestamp,'yyyy-mm-dd hh24:mi:ss'),sql_redo from v$logmnr_contents WHERE USERNAME ='SCOTT' and lower(sql_redo) like '%table%';
6)关闭日志分析
SQL> execute dbms_logmnr.end_logmnr;
例子将在053的不完全恢复一节中演示。
在oracle11g 的EM里已加入了有关log miner的功能,可根据时间段由oracle自动选择需要的日志,比较方便,但只有提交的事务信息,看来是从transaction的角度设计的。
EM-->Availability-->Manage-->View and Manage Transctions