一、源环境检查
(1)检查参数
检查是否开启归档及附加日志:
select para_name, para_value from v$dm_ini where para_name in ('ARCH_INI','RLOG_APPEND_LOGIC');
行号 PARA_NAME PARA_VALUE
---------- ----------------- ----------
1 RLOG_APPEND_LOGIC 0
2 ARCH_INI 0
已用时间: 11.913(毫秒). 执行号:500.
若没有开启归档,需要打开归档,开启归档需要mount状态配置,请注意对业务的影响(生产系统应该打开归档模式,否则不会生成归档日志)。
其中,归档文件路径、单个归档日志文件大小和归档总大小请根据实际情况设置,这里示例单个归档日志文件1G,归档日志总大小5G。
ALTER DATABASE MOUNT;
ALTER DATABASE ARCHIVELOG;
ALTER DATABASE ADD ARCHIVELOG 'DEST=/opt/dmdbms/data/DAMENG/arch, TYPE=LOCAL, FILE_SIZE=1024, SPACE_LIMIT=5120';
ALTER DATABASE OPEN;
附加日志参数需要设置为1或者2,含义如下:
1 代表如果有主键列,记录UPDATE和DELETE操作时只包含主键列信息,若没有主键列则包含所有列信息;
2 代表不论是否有主键列,记录UPDATE和DELETE操作时都包含所有列的信息。
SP_SET_PARA_VALUE(1,'RLOG_APPEND_LOGIC',1);
DMSQL 过程已成功完成
已用时间: 3.880(毫秒). 执行号:501.
--SP_SET_PARA_VALUE(1,'RLOG_APPEND_LOGIC',2);
完成修改后查询状态如下:
select para_name, para_value from v$dm_ini where para_name in ('ARCH_INI','RLOG_APPEND_LOGIC');
行号 PARA_NAME PARA_VALUE
---------- ----------------- ----------
1 RLOG_APPEND_LOGIC 1
2 ARCH_INI 1
已用时间: 4.197(毫秒). 执行号:502.
(2)准备文件
如果是刚刚开启的归档模式,那么当前还没有能够分析的对象,需要进行一些DDL或者DML操作。操作完成后,查看归档日志文件信息。
SQL> SELECT NAME, FIRST_TIME, NEXT_TIME, FIRST_CHANGE#, NEXT_CHANGE# FROM V$ARCHIVED_LOG;
行号 NAME FIRST_TIME NEXT_TIME
---------- --------------------------------------------------------------------------------- -------------------------- --------------------------
FIRST_CHANGE# NEXT_CHANGE#
-------------------- --------------------
1 /opt/dmdbms/data/DAMENG/arch/ARCHIVE_LOCAL1_0x5170E15A[0]_2021-09-07_10-42-33.log 2021-09-07 11:58:56.000000 2021-09-07 11:44:24.036000
28597 30349
2 /opt/dmdbms/data/DAMENG/arch/ARCHIVE_LOCAL1_0x5170E15A[0]_2021-09-07_11-59-00.log 2021-09-07 11:44:24.036000 2021-09-07 12:19:06.477000
30350 32086
3 /opt/dmdbms/data/DAMENG/arch/ARCHIVE_LOCAL1_0x5170E15A[0]_2021-09-07_12-20-37.log 2021-09-07 12:19:06.477000 2021-09-07 12:20:37.466282
32087 32271
已用时间: 1.294(毫秒). 执行号:519.
将需要分析的归档日志文件下载到本地(若在源库执行分析,则直接跳转到2.4步骤)。
二、本地环境准备(异机分析)
(1)开启归档(在源库挖掘跳过此步骤)
ALTER DATABASE MOUNT;
ALTER DATABASE ARCHIVELOG;
ALTER DATABASE ADD ARCHIVELOG 'DEST=D:\dmdbms\data\DAMENG\arch, TYPE=LOCAL, FILE_SIZE=1024, SPACE_LIMIT=5120';
ALTER DATABASE OPEN;
(2)查询魔数(在源库挖掘跳过此步骤)
SELECT DB_MAGIC FROM V$RLOG;
行号 DB_MAGIC
---------- --------------------
1 763331945
已用时间: 1.061(毫秒). 执行号:503.
(3)修改归档日志魔数(在源库挖掘跳过此步骤)
# 修改下载到本地的归档日志魔数,将其改为与本地库一致,下载下来的都要改。
PS D:\dmdbms\bin> dmmdf.exe TYPE=2 FILE="D:\test_arch\ARCHIVE_LOCAL1_0x5170E15A[0]_2021-09-07_10-42-33.log"
dmmdf V8
**********************************************************
1 sig = DMALOG
2 ver = 7006
3 chksum = 2666971802
4 sta = 0
5 n_magic = 0
6 db_magic = 1366352218 # 这是我们要修改的参数
7 len = 1073741824
8 free = 206336
9 clsn = 30055
10 clsn_fil = 0
11 clsn_off = 4096
12 pemnt_magic = 161901972
13 fil_id = 0
15 next_seq = 4185
16 g_next_seq = 4185
17 arch_lsn = 28597
18 arch_seq = 4163
19 dbversion = 0x7000c
20 min_exec_version = V8.1.1.1
21 min_dct_version = 4
22 p_db_magic = 0
23 n_apply_ep = 0
24 apply_info_lsn = 0
pkg_seq_arr: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
apply_lsn_arr: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
25 recv_p_db_magic = 0
26 recv_n_apply_ep = 0
recv_pkg_seq_arr: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
recv_apply_lsn_arr: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
**********************************************************
You can only reset sta(4) or db_magic (6) or clsn (9) or clsn_fil(10) or clsn_off(11) or pemnt_magic(12) or fil_id(13) or next_seq(15) or g_next_seq(16) or p_db_magic(20) or n_apply_ep(21).
Please input the num which one you want to change, q to quit: 6 #输入魔数参数序号
Input the new value: 763331945 #输入本地库查出的魔数
**********************************************************
1 sig = DMALOG
2 ver = 7006
3 chksum = 2666971802
4 sta = 0
5 n_magic = 0
6 db_magic = 763331945 # 这是修改后的参数
7 len = 1073741824
8 free = 206336
9 clsn = 30055
10 clsn_fil = 0
11 clsn_off = 4096
12 pemnt_magic = 161901972
13 fil_id = 0
15 next_seq = 4185
16 g_next_seq = 4185
17 arch_lsn = 28597
18 arch_seq = 4163
19 dbversion = 0x7000c
20 min_exec_version = V8.1.1.1
21 min_dct_version = 4
22 p_db_magic = 0
23 n_apply_ep = 0
24 apply_info_lsn = 0
pkg_seq_arr: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
apply_lsn_arr: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
25 recv_p_db_magic = 0
26 recv_n_apply_ep = 0
recv_pkg_seq_arr: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
recv_apply_lsn_arr: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
**********************************************************
Do you want to quit and save the change to file (y/n): y #保存退出
Save to file success!
PS D:\dmdbms\bin>
(4)创建系统包
如已创建,可跳过本步骤。
SP_CREATE_SYSTEM_PACKAGES (1,'DBMS_LOGMNR');
三、分析归档
以下过程需要在同一个数据库会话中进行。
(1)添加待分析日志
--可以添加一个或多个归档日志一起挖掘,但每条命令一次只能添加一个。
SQL> DBMS_LOGMNR.ADD_LOGFILE('D:\test_arch\ARCHIVE_LOCAL1_0x5170E15A[0]_2021-09-07_10-42-33.log');
DMSQL 过程已成功完成
已用时间: 0.863(毫秒). 执行号:16100.
SQL> DBMS_LOGMNR.ADD_LOGFILE('D:\test_arch\ARCHIVE_LOCAL1_0x5170E15A[0]_2021-09-07_11-59-00.log');
DMSQL 过程已成功完成
已用时间: 1.329(毫秒). 执行号:16101.
SQL> DBMS_LOGMNR.ADD_LOGFILE('D:\test_arch\ARCHIVE_LOCAL1_0x5170E15A[0]_2021-09-07_12-20-37.log');
DMSQL 过程已成功完成
已用时间: 0.876(毫秒). 执行号:16102.
可以通过动态视图 V$LOGMNR_LOGS 查询上一步骤添加的归档日志文件:
SELECT LOW_SCN, NEXT_SCN, LOW_TIME, HIGH_TIME, LOG_ID, FILENAME FROM V$LOGMNR_LOGS;
行号 LOW_SCN NEXT_SCN LOW_TIME HIGH_TIME
---------- -------------------- -------------------- -------------------------- --------------------------
LOG_ID FILENAME
----------- -----------------------------------------------------------------
1 28597 30349 2021-09-07 14:29:31.000000 2021-09-07 11:44:24.036000
0 D:\test_arch\ARCHIVE_LOCAL1_0x5170E15A[0]_2021-09-07_10-42-33.log
2 30350 32086 2021-09-07 11:44:24.036000 2021-09-07 12:19:06.477000
1 D:\test_arch\ARCHIVE_LOCAL1_0x5170E15A[0]_2021-09-07_11-59-00.log
3 32087 32346 2021-09-07 12:19:06.477000 2021-09-07 12:20:37.466282
2 D:\test_arch\ARCHIVE_LOCAL1_0x5170E15A[0]_2021-09-07_12-20-37.log
已用时间: 1.498(毫秒). 执行号:16103.
(2)执行分析
调用DBMS_LOGMNR.START_LOGMNR执行分析,其中options参数含义如下:
2 | COMMITTED_DATA_ONLY | 仅从已提交的事务的日志中挖掘信息 |
16 | DICT_FROM_ONLINE_CATALOG | 使用在线字典 |
64 | NO_SQL_DELIMITER | 拼写的 SQL 语句最后不添加分隔符 |
2048 | NO_ROWID_IN_STMT | 拼写的 SQL 语句中不包含 ROWID |
options参数可以根据需求设为某个项的值,也可以设为某几个项的和。这里示例2130,表示采用全部四项设置。
可以根据实际需求指定起止时间或者起止scn,也可以不指定。具体可用参数请参考官方文档《DM8系统包使用手册》的“DBMS_LOGMNR 包”章节。
SQL> DBMS_LOGMNR.START_LOGMNR(OPTIONS=>2130, STARTTIME=>TO_DATE('2021-09-07 10:40:00','YYYY-MM-DD HH24:MI:SS') , ENDTIME=>TO_DATE('2021-09-07 13:00:00','YYYY-MM-DD HH24:MI:SS'));
DMSQL 过程已成功完成
已用时间: 67.448(毫秒). 执行号:16104.
可以通过动态视图 V$LOGMNR_CONTENTS 查看分析结果(这里看到我们操作的是TEST.PERSON,但挖掘结果是"DMHR"."AQ$_QUEUE_TABLES",这是因为使用了在线字典的缘故,如果在源库挖掘,则可以正常显示表、列等对象名)。
SQL> set linesize 1000
SQL> set pagesize 100
SQL> SELECT SQL_REDO, TIMESTAMP FROM V$LOGMNR_CONTENTS WHERE TABLE_NAME IS NOT NULL;
行号 SQL_REDO TIMESTAMP
---------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------------------------
1 CREATE TABLE "TEST"."PERSON" ( "PERSONID" INT IDENTITY(1,1) NOT NULL, "SEX" CHAR(1) NOT NULL, "NAME" VARCHAR(50) NOT NULL, "EMAIL" VARCHAR(50) NULL, "PHONE" VARCHAR(25) NULL, CLUSTER PRIMARY KEY("PERSONID") ENABLE ) 2021-09-07 12:40:58.678000
2 INSERT INTO "DMHR"."AQ$_QUEUE_TABLES"("SCHM", "NAME", "OBJNO", "FLAGS", "SORT_COLS", "TABLE_COMMENT") VALUES(1, 'F', '李丽', 'lily@sina.com', '02788548562', NULL) 2021-09-07 12:40:58.680000
3 INSERT INTO "DMHR"."AQ$_QUEUE_TABLES"("SCHM", "NAME", "OBJNO", "FLAGS", "SORT_COLS", "TABLE_COMMENT") VALUES(2, 'M', '王刚', '', '02787584562', NULL) 2021-09-07 12:40:58.681000
4 INSERT INTO "DMHR"."AQ$_QUEUE_TABLES"("SCHM", "NAME", "OBJNO", "FLAGS", "SORT_COLS", "TABLE_COMMENT") VALUES(3, 'M', '李勇', '', '02782585462', NULL) 2021-09-07 12:40:58.681000
5 INSERT INTO "DMHR"."AQ$_QUEUE_TABLES"("SCHM", "NAME", "OBJNO", "FLAGS", "SORT_COLS", "TABLE_COMMENT") VALUES(4, 'F', '郭艳', '', '02787785462', NULL) 2021-09-07 12:40:58.681000
6 INSERT INTO "DMHR"."AQ$_QUEUE_TABLES"("SCHM", "NAME", "OBJNO", "FLAGS", "SORT_COLS", "TABLE_COMMENT") VALUES(5, 'F', '孙丽', '', '13055173012', NULL) 2021-09-07 12:40:58.682000
7 alter table "TEST"."PERSON" modify "EMAIL" VARCHAR(100) 2021-09-07 12:40:58.687000
8 INSERT INTO "DMHR"."AQ$_QUEUE_TABLES"("SCHM", "NAME", "OBJNO", "FLAGS", "SORT_COLS", "TABLE_COMMENT") VALUES(6, 'M', '张三', 'test@dameng.com', '13399990000', NULL) 2021-09-07 12:40:58.694000
9 DELETE FROM "DMHR"."AQ$_QUEUE_TABLES" WHERE "SCHM" = 2 2021-09-07 12:40:58.695000
10 UPDATE "DMHR"."AQ$_QUEUE_TABLES" SET "NAME" = 'M', "OBJNO" = 'Liuhuan', "FLAGS" = '12345678@qq.com', "SORT_COLS" = '13399990000' WHERE "SCHM" = 1 2021-09-07 12:40:58.696000
10 rows got
已用时间: 0.314(毫秒). 执行号:16120.
(3)终止分析
保存需要的内容后,终止分析(视图V$LOGMNR_CONTENTS的分析结果将自动清除,注意提前保存)
DBMS_LOGMNR.END_LOGMNR();