Oracle 10g stream 一对多复制
Oracle 流是一种数据共享的通用机制,可以被用于许多处理的基础,包括消息、复制和数据仓库的 ETL 过程。它是高级队列、LogMinor、作业调度等已存在技术的扩展。这里做一个简单的复制示例来说明其用法。试验环境:
windows server 2003 sp1;10g 10.1.0.2
源库:SID:db1 IP:10.1.8.201
目的库:SID:db2 IP:10.1.9.49
源、目的库的 tnsnames.ora 配置
DB1 =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.1.8.201)(PORT = 1521))
(CONNECT_DATA =
(SERVICE_NAME = db1)
(SERVER = DEDICATED)
)
)
DB2 =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.1.9.49)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = db2)
(SERVER = DEDICATED)
)
)
1. 基本体系结构
流处理分为捕捉、传输、应用三个主要进程。
捕捉进程是一个可选的后台进程。它从重做日志中捕获 DDL 和 DML 的变化,并且把它们封装成逻辑改变记录(LCRs)。缺省的捕捉机制可以立即获得用户定义的事件。
传输进程把 LCRs 存储到 SYS.AnyData 数据类型的队列中。LCRs 在不同数据库中的源和目的传输区域之间传播。传播使用工作队列来调度。
应用进程是一个可选的后台进程。它调出 LCRs 直接应用,或者为用户自定义消息作为参数传递给用户自定义包。
捕获和应用进程可以被用于表、模式、数据库等级别,各级别的规则决定了其不同的行为。
2. 实例设置 CONN sys/password@DB1 AS SYSDBA
create pfile from spfile;
-- 编辑 initsid.ora 文件,增加以下参数:
-- AQ_TM_PROCESSES=1;
-- GLOBAL_NAMES=TRUE;
SHUTDOWN IMMEDIATE;
create spfile from pfile;
STARTUP mount;
alter database archivelog; -- 改为归档模式
alter database open;
3. 流管理员设置 CONN sys/password@DB1 AS SYSDBA
CREATE USER strmadmin IDENTIFIED BY strmadmin DEFAULT TABLESPACE users QUOTA UNLIMITED ON users;
GRANT DBA, CONNECT, RESOURCE, SELECT_CATALOG_ROLE TO strmadmin;
GRANT EXECUTE ON DBMS_AQADM TO strmadmin;
GRANT EXECUTE ON DBMS_CAPTURE_ADM TO strmadmin;
GRANT EXECUTE ON DBMS_PROPAGATION_ADM TO strmadmin;
GRANT EXECUTE ON DBMS_STREAMS_ADM TO strmadmin;
GRANT EXECUTE ON DBMS_APPLY_ADM TO strmadmin;
GRANT EXECUTE ON DBMS_FLASHBACK TO strmadmin;
GRANT ALL ON scott.dept TO strmadmin;
BEGIN
DBMS_RULE_ADM.GRANT_SYSTEM_PRIVILEGE(
privilege => DBMS_RULE_ADM.CREATE_RULE_SET_OBJ,
grantee => 'strmadmin',
grant_option => FALSE);
END;
/
BEGIN
DBMS_RULE_ADM.GRANT_SYSTEM_PRIVILEGE(
privilege => DBMS_RULE_ADM.CREATE_RULE_OBJ,
grantee => 'strmadmin',
grant_option => FALSE);
END;
/
CONNECT strmadmin/strmadmin@DB1
EXEC DBMS_STREAMS_ADM.SET_UP_QUEUE(); -- 建立流队列
CREATE DATABASE LINK db2 CONNECT TO strmadmin IDENTIFIED BY strmadmin USING 'DB2'; -- 建立数据库连接
在目的库(DB2)重复上面2、3步骤。
4. LogMinor 表空间设置 CONN sys/password@DB1 AS SYSDBA
CREATE TABLESPACE logmnr_ts DATAFILE 'E:ORACLEPRODUCT10.1.0ORADATADB1logmnr01.dbf' SIZE 25 M REUSE AUTOEXTEND ON MAXSIZE UNLIMITED;
EXECUTE DBMS_LOGMNR_D.SET_TABLESPACE('logmnr_ts');
5. SUPPLEMENTAL 日志 CONN sys/password@DB1 AS SYSDBA
ALTER TABLE scott.dept ADD SUPPLEMENTAL LOG GROUP log_group_dept_pk (deptno) ALWAYS;
6. 配置传播进程 CONNECT strmadmin/strmadmin@DB1
BEGIN
DBMS_STREAMS_ADM.ADD_TABLE_PROPAGATION_RULES(
table_name => 'scott.dept',
streams_name => 'db1_to_db2',
source_queue_name => 'strmadmin.streams_queue',
destination_queue_name => 'strmadmin.streams_queue@db2',
include_dml => true,
include_ddl => true,
source_database => 'db1');
END;
/
-- 检查传播进程作业
SELECT job,TO_CHAR(last_date, 'DD-Mon-YYYY HH24:MI:SS') last_date,TO_CHAR(next_date, 'DD-Mon-YYYY HH24:MI:SS') next_date,what FROM dba_jobs;
7. 配置捕捉进程 CONNECT strmadmin/strmadmin@DB1
BEGIN
DBMS_STREAMS_ADM.ADD_TABLE_RULES(
table_name => 'scott.dept',
streams_type => 'capture',
streams_name => 'capture_simp',
queue_name => 'strmadmin.streams_queue',
include_dml => true,
include_ddl => true);
END;
/
8. 配置初始 SCN
在应用进程工作前必须在目的表中配置源表的 SCN。如果目的表已经存在,可用 exp/imp 元数据完成。
exp userid=scott/tiger@db1 FILE=dept_instant.dmp TABLES=dept OBJECT_CONSISTENT=y ROWS=n
imp userid=scott/tiger@db2 FILE=dept_instant.dmp IGNORE=y COMMIT=y LOG=import.log STREAMS_INSTANTIATION=y
因为在迁移元数据时 SUPPLEMENTAL 日志也被迁移。因为捕捉进程还没有启动,所以可以删除这些日志: CONN sys/password@DB2 AS SYSDBA
ALTER TABLE scott.dept DROP SUPPLEMENTAL LOG GROUP log_group_dept_pk;
用 DBMS_APPLY_ADM 包设置 SCN CONNECT strmadmin/strmadmin@db1
DECLARE
v_scn NUMBER;
BEGIN
v_scn := DBMS_FLASHBACK.GET_SYSTEM_CHANGE_NUMBER();
DBMS_APPLY_ADM.SET_TABLE_INSTANTIATION_SCN@DB2(
source_object_name => 'scott.dept',
source_database_name => 'db1',
instantiation_scn => v_scn);
END;
/
9. 配置应用进程 CONNECT strmadmin/strmadmin@DB2
BEGIN
DBMS_STREAMS_ADM.ADD_TABLE_RULES(
table_name => 'scott.dept',
streams_type => 'apply',
streams_name => 'apply_simp',
queue_name => 'strmadmin.streams_queue',
include_dml => true,
include_ddl => true,
source_database => 'db1');
END;
/
10. 启动应用进程 CONNECT strmadmin/strmadmin@DB2
BEGIN
DBMS_APPLY_ADM.SET_PARAMETER(
apply_name => 'apply_simp',
parameter => 'disable_on_error',
value => 'n');
DBMS_APPLY_ADM.START_APPLY(
apply_name => 'apply_simp');
END;
/
11. 启动捕捉进程 CONNECT strmadmin/strmadmin@DB1
BEGIN
DBMS_CAPTURE_ADM.START_CAPTURE(
capture_name => 'capture_simp');
END;
/
12. 测试
-- 测试 DML
CONNECT scott/tiger@db1
INSERT INTO dept (deptno, dname, loc) VALUES (99, 'Test Dept', 'UK');
COMMIT;
SELECT * FROM dept;
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
99 Test Dept UK
5 rows selected.
CONNECT scott/tiger@db2
SELECT * FROM dept;
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
99 Test Dept UK
5 rows selected.
-- 测试 DDL
CONNECT scott/tiger@db1
ALTER TABLE dept ADD (new_col NUMBER(10))
/
DESC dept
Name Null? Type
---------------------------- -------- --------------
DEPTNO NOT NULL NUMBER(2)
DNAME VARCHAR2(14)
LOC VARCHAR2(13)
NEW_COL NUMBER(10)
CONNECT scott/tiger@db2
DESC dept
Name Null? Type
---------------------------- -------- --------------
DEPTNO NOT NULL NUMBER(2)
DNAME VARCHAR2(14)
LOC VARCHAR2(13)
NEW_COL NUMBER(10)
-- 可以用下列语句查看流的内容
CONNECT strmadmin/strmadmin@DB1
SELECT s.user_data.getTypeName() FROM streams_queue_table s;
SET SERVEROUTPUT ON
DECLARE
v_anydata SYS.ANYDATA;
v_lcr SYS.LCR$_ROW_RECORD;
v_row_list SYS.LCR$_ROW_LIST;
v_result PLS_INTEGER;
BEGIN
SELECT user_data
INTO v_anydata
FROM strmadmin.streams_queue_table
WHERE rownum < 2;
v_result := ANYDATA.GetObject(
self => v_anydata,
obj => v_lcr);
DBMS_OUTPUT.PUT_LINE('Command Type : ' || v_lcr.Get_Command_Type);
DBMS_OUTPUT.PUT_LINE('Object Owner : ' || v_lcr.Get_Object_Owner);
DBMS_OUTPUT.PUT_LINE('Object Name : ' || v_lcr.Get_Object_Name);
DBMS_OUTPUT.PUT_LINE('Source Database Name : ' || v_lcr.Get_Source_Database_Name);
END;
/
13. 清理 -- 可以使用下面的语句识别并删除所有规则
conn / as sysdba
BEGIN
FOR cur_rec IN (SELECT rule_owner, rule_name FROM dba_rules) LOOP
DBMS_RULE_ADM.DROP_RULE(
rule_name => cur_rec.rule_owner || '.' || cur_rec.rule_name,
force => TRUE);
END LOOP;
END;
/
-- 识别、停止并删除所有捕捉进程和应用进程
conn / as sysdba
BEGIN
FOR cur_rec IN (SELECT capture_name FROM dba_capture) LOOP
DBMS_CAPTURE_ADM.STOP_CAPTURE(
capture_name => cur_rec.capture_name);
DBMS_CAPTURE_ADM.DROP_CAPTURE(
capture_name => cur_rec.capture_name);
END LOOP;
FOR cur_rec IN (SELECT apply_name FROM dba_apply) LOOP
DBMS_APPLY_ADM.STOP_APPLY(
apply_name => cur_rec.apply_name);
DBMS_APPLY_ADM.DROP_APPLY(
apply_name => cur_rec.apply_name);
END LOOP;
END;
/
-- 使用下面的语句彻底删除相关对象的流信息
BEGIN
DBMS_STREAMS_ADM.PURGE_SOURCE_CATALOG(
source_database => 'db1',
source_object_name => 'scott.dept',
source_object_type => 'TABLE');
END;
/
相关信息:
Oracle9i Streams Release 2 (9.2)
Monitoring a Streams Environment
Oracle9i Supplied PL/SQL Packages and Types Reference Release 2 (9.2)
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/wzy0623/archive/2007/06/19/1658294.aspx
----------------------------------------------------------------------------------------------------------------------------------------
传输进程把 LCRs 存储到 SYS.AnyData 数据类型的队列中。LCRs 在不同数据库中的源和目的传输区域之间传播。传播使用工作队列来调度。
应用进程是一个可选的后台进程。它调出 LCRs 直接应用,或者为用户自定义消息作为参数传递给用户自定义包。
create spfile from pfile;
STARTUP mount;
alter database archivelog; -- 改为归档模式
alter database open;
SQL>alter system set aq_tm_processes=4 scope=spfile;
SQL>alter system set global_names=true scope=spfile;
SQL>alter system set job_queue_processes=8 scope=spfile;
SQL>alter system set log_parallelism=1 scope=spfile;
1、创建流管理员,并对其进行授权
节点1:
C:\Documents and Settings\olivenan>set oracle_sid=orcl
连接到:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
A UNLIMITED ON users;
SQL> GRANT DBA, CONNECT, RESOURCE, SELECT_CATALOG_ROLE TO strmadmin;
SQL> GRANT EXECUTE ON DBMS_AQADM TO strmadmin;
SQL> GRANT ALL ON scott.dept TO strmadmin;
SQL> BEGIN
2 DBMS_RULE_ADM.GRANT_SYSTEM_PRIVILEGE(
3 privilege => DBMS_RULE_ADM.CREATE_RULE_SET_OBJ,
4 grantee => 'strmadmin',
5 grant_option => FALSE);
6 END;
7 /
SQL> BEGIN
2 DBMS_RULE_ADM.GRANT_SYSTEM_PRIVILEGE(
3 privilege => DBMS_RULE_ADM.CREATE_RULE_OBJ,
4 grantee => 'strmadmin',
5 grant_option => FALSE);
6 END;
7 /
已连接。
SQL> EXEC DBMS_STREAMS_ADM.SET_UP_QUEUE();
SQL> create database link olivedb connect to strmadmin identified by strmadmin
using 'db2';
ing 'db3';
SQL> select 1 from dual@olivedb;
----------
1
----------
1
节点2:
SQL> CREATE USER strmadmin IDENTIFIED BY strmadmin DEFAULT TABLESPACE users QUOTA UNLIMITED ON users;
GRANT EXECUTE ON DBMS_CAPTURE_ADM TO strmadmin;
GRANT EXECUTE ON DBMS_PROPAGATION_ADM TO strmadmin;
GRANT EXECUTE ON DBMS_STREAMS_ADM TO strmadmin;
GRANT EXECUTE ON DBMS_APPLY_ADM TO strmadmin;
GRANT EXECUTE ON DBMS_FLASHBACK TO strmadmin;
DBMS_RULE_ADM.GRANT_SYSTEM_PRIVILEGE(
privilege => DBMS_RULE_ADM.CREATE_RULE_SET_OBJ,
grantee => 'strmadmin',
grant_option => FALSE);
END;
/
DBMS_RULE_ADM.GRANT_SYSTEM_PRIVILEGE(
privilege => DBMS_RULE_ADM.CREATE_RULE_OBJ,
grantee => 'strmadmin',
grant_option => FALSE);
END;
/
User created.
Grant succeeded.
Grant succeeded.
Grant succeeded.
Grant succeeded.
Grant succeeded.
Grant succeeded.
Grant succeeded.
Grant succeeded.
PL/SQL procedure successfully completed.
Connected.
SQL> EXEC DBMS_STREAMS_ADM.SET_UP_QUEUE();
SQL> create database link orcl connect to strmadmin identified by strmadmin using 'db1';
Database link created.
SQL> select 1 from dual@orcl;
----------
1
节点3:
连接到:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
A UNLIMITED ON users;
SQL> GRANT DBA, CONNECT, RESOURCE, SELECT_CATALOG_ROLE TO strmadmin;
SQL> GRANT EXECUTE ON DBMS_AQADM TO strmadmin;
SQL> GRANT ALL ON scott.dept TO strmadmin;
SQL> BEGIN
2 DBMS_RULE_ADM.GRANT_SYSTEM_PRIVILEGE(
3 privilege => DBMS_RULE_ADM.CREATE_RULE_SET_OBJ,
4 grantee => 'strmadmin',
5 grant_option => FALSE);
6 END;
7 /
SQL> BEGIN
2 DBMS_RULE_ADM.GRANT_SYSTEM_PRIVILEGE(
3 privilege => DBMS_RULE_ADM.CREATE_RULE_OBJ,
4 grantee => 'strmadmin',
5 grant_option => FALSE);
6 END;
7 /
已连接。
SQL> EXEC DBMS_STREAMS_ADM.SET_UP_QUEUE();
SQL> create database link orcl connect to strmadmin identified by strmadmin usi
ng 'db1';
SQL> select 1 from dual@orcl;
----------
1
2、创建LogMinor 表空间,并添加SUPPLEMENTAL 日志
节点1:
SQL> conn /as sysdba
SQL> EXECUTE DBMS_LOGMNR_D.SET_TABLESPACE('logmnr_ts');
SQL> conn strmadmin/strmadmin@db1
已连接。
SQL> BEGIN
2 DBMS_STREAMS_ADM.ADD_TABLE_PROPAGATION_RULES(
3 table_name => 'scott.dept',
4 streams_name => 'db1_to_db2',
5 source_queue_name => 'strmadmin.streams_queue',
6 destination_queue_name => 'strmadmin.streams_queue@olivedb',
7 include_dml => true,
8 include_ddl => true,
9 source_database => 'orcl');
10 END;
11 /
备注:参数destination_queue_name中的olivedb是创建的指向olivedb数据库的db link;
source_database的值必须是目标数据库指向源数据库的db link;
在配置stream的整个过程中这两个参数遵循这个原则
2 DBMS_STREAMS_ADM.ADD_TABLE_PROPAGATION_RULES(
3 table_name => 'scott.dept',
4 streams_name => 'db1_to_db3',
5 source_queue_name => 'strmadmin.streams_queue',
6 destination_queue_name => 'strmadmin.streams_queue@orcl2',
7 include_dml => true,
8 include_ddl => true,
9 source_database => 'orcl');
10 END;
11 /
ext_date, 'DD-Mon-YYYY HH24:MI:SS') next_date,what FROM dba_jobs;
---------- ------------------------- -------------------------
WHAT
--------------------------------------------------------------------------------
EMD_MAINTENANCE.EXECUTE_EM_DBMS_JOB_PROCS();
next_date := sys.dbms_aqadm.aq$_propaq(job);
next_date := sys.dbms_aqadm.aq$_propaq(job);
4、配置捕捉进程(源数据库)
2 DBMS_STREAMS_ADM.ADD_TABLE_RULES(
3 table_name => 'scott.dept',
4 streams_type => 'capture',
5 streams_name => 'capture_simp',
6 queue_name => 'strmadmin.streams_queue',
7 include_dml => true,
8 include_ddl => true);
9 END;
10 /
5、配置初始 SCN
在应用进程工作前必须在目的表中配置源表的 SCN。如果目的表已经存在,可用 exp/imp 元数据完成。
imp userid=scott/tiger@db2 FILE=dept_instant.dmp IGNORE=y COMMIT=y LOG=import.log STREAMS_INSTANTIATION=y
imp userid=scott/tiger@db3 FILE=dept_instant.dmp IGNORE=y COMMIT=y LOG=import.log STREAMS_INSTANTIATION=y
因为在迁移元数据时 SUPPLEMENTAL 日志也被迁移。因为捕捉进程还没有启动,所以可以删除这些日志:
CONN sys/password@DB2 AS SYSDBA
ALTER TABLE scott.dept DROP SUPPLEMENTAL LOG GROUP log_group_dept_pk;
CONN sys/password@DB3 AS SYSDBA
ALTER TABLE scott.dept DROP SUPPLEMENTAL LOG GROUP log_group_dept_pk;
SQL> DECLARE
2 v_scn NUMBER;
3 BEGIN
4 v_scn := DBMS_FLASHBACK.GET_SYSTEM_CHANGE_NUMBER();
5 DBMS_APPLY_ADM.SET_TABLE_INSTANTIATION_SCN@olivedb(
6 source_object_name => 'scott.dept',
7 source_database_name => 'orcl',
8 instantiation_scn => v_scn);
9 END;
10 /
2 v_scn NUMBER;
3 BEGIN
4 v_scn := DBMS_FLASHBACK.GET_SYSTEM_CHANGE_NUMBER();
5 DBMS_APPLY_ADM.SET_TABLE_INSTANTIATION_SCN@orcl2(
6 source_object_name => 'scott.dept',
7 source_database_name => 'orcl',
8 instantiation_scn => v_scn);
9 END;
10 /
DBMS_STREAMS_ADM.ADD_TABLE_RULES(
table_name => 'scott.dept',
streams_type => 'apply',
streams_name => 'apply_simp',
queue_name => 'strmadmin.streams_queue',
include_dml => true,
include_ddl => true,
source_database => 'orcl');
END; 2 3 4 5 6 7 8 9 10
11 /
配置应用进程 CONNECT strmadmin/strmadmin@DB3
2 DBMS_STREAMS_ADM.ADD_TABLE_RULES(
3 table_name => 'scott.dept',
4 streams_type => 'apply',
5 streams_name => 'apply_simp',
6 queue_name => 'strmadmin.streams_queue',
7 include_dml => true,
8 include_ddl => true,
9 source_database => 'orcl');
10 END;
11 /
7、启动应用进程 CONNECT strmadmin/strmadmin@DB2
BEGIN
DBMS_APPLY_ADM.SET_PARAMETER(
apply_name => 'apply_simp',
parameter => 'disable_on_error',
value => 'n');
apply_name => 'apply_simp');
END;
/
节点2:
CONNECT strmadmin/strmadmin@DB2
BEGIN
DBMS_APPLY_ADM.SET_PARAMETER(
apply_name => 'apply_simp',
parameter => 'disable_on_error',
value => 'n');
apply_name => 'apply_simp');
END;
/
SQL> BEGIN
2 DBMS_APPLY_ADM.SET_PARAMETER(
3 apply_name => 'apply_simp',
4 parameter => 'disable_on_error',
5 value => 'n');
6
7 DBMS_APPLY_ADM.START_APPLY(
8 apply_name => 'apply_simp');
9 END;
10 /
2 DBMS_CAPTURE_ADM.START_CAPTURE(
3 capture_name => 'capture_simp');
4 END;
5 /
9、DML:测试
节点1:
SQL> CONNECT scott/tiger@db1
已连接。
SQL> INSERT INTO dept (deptno, dname, loc) VALUES (99, 'Test Dept', 'UK');
节点2:
SQL> select count(*) from dept;
----------
4
----------
5
SQL> desc dept
Name Null? Type
----------------------------------------- -------- ----------------------------
DEPTNO NOT NULL NUMBER(2)
DNAME VARCHAR2(14)
LOC VARCHAR2(13)
----------
4
----------
5
----------
5
名称 是否为空? 类型
----------------------------------------- -------- --------------------------
DNAME VARCHAR2(14)
LOC VARCHAR2(13)
10、DDL测试:
SQL> desc dept;
名称 是否为空? 类型
----------------------------------------- -------- --------------------------
DNAME VARCHAR2(14)
LOC VARCHAR2(13)
名称 是否为空? 类型
----------------------------------------- -------- --------------------------
DNAME VARCHAR2(14)
LOC VARCHAR2(13)
节点2:
SQL> desc dept
Name Null? Type
----------------------------------------- -------- ----------------------------
DEPTNO NOT NULL NUMBER(3)
DNAME VARCHAR2(14)
LOC VARCHAR2(13)
SQL> desc dept;
名称 是否为空? 类型
----------------------------------------- -------- ----------------------------
DNAME VARCHAR2(14)
LOC VARCHAR2(13)