构建Oracle双向流复制是一个十分复杂的过程,我写这个文档的目的是尽量有条理地列出所需做的工作,帮助DBA更有效的建设流复制环境。
1.以scott模式为复制示例,一般只要在创建数据库时选择了安装sample schema,都会存在该scott模式;至少保证源库中存在该schema,以便可以初始化到目标库中。
2.在源和目标2个数据库中创建strmadmin流管理用户,当然你也可以选用其他名字。同时在2个库中都要创建streams使用的表空间,以便让logmnr使用它:
CREATE TABLESPACE streams_tbs DATAFILE 'XXXXXX' SIZE 100M REUSE AUTOEXTEND ON MAXSIZE UNLIMITED; /* 10g r2中logmnr默认使用SYSAUX表空间 */ exec DBMS_LOGMNR_D.SET_TABLESPACE ('streams_tbs'); /* 创建完表空间后,接着要创建strmadmin用户 */ CREATE USER strmadmin IDENTIFIED BY strmadmin DEFAULT TABLESPACE streams_tbs QUOTA UNLIMITED ON streams_tbs; GRANT DBA TO strmadmin; BEGIN DBMS_STREAMS_AUTH.GRANT_ADMIN_PRIVILEGE( grantee => 'strmadmin', grant_privileges => true); END; / /* 可以通过查询dba_streams_administrator视图检查用户是否正确授予流管理权限 */ SQL> SELECT * FROM dba_streams_administrator; USERNAME LOC ACC ------------------------------ --- --- MACLEAN YES YES STRMADMIN YES YES
3.在2边数据库中都需要设置合理的实例初始化参数,我们以10g release2为例:
参数名与推荐值:_job_queue_interval = 1 描述:job的队列的扫描参数,默认为5,即5s扫描一次 出于何种考虑:设置较小的_job_queue_interval有利于propagation作业 如何设置:alter system set "_job_queue_interval"=1 scope=spfile; /* 注意scope=spfile的参数都需要重启实例方能生效 */ 参数名与推荐值:COMPATIBLE>= 10.2.0.0 描述:数据库版本兼容性参数,以前介绍过,不再展开 出于何种考虑:10g release2的部分Streams新特性要求该参数至少为10.2.0.0或更高 如何设置:只有从较低版本升级到10g r2的数据库需要设置该参数, alter system set compatible="10.2.0.0" scope=spfile; 参数名与推荐值:GLOBAL_NAMES=true 描述:指定是否要求database link名与数据库全局名一致,默认为FALSE也就是不需要一致 出于何种考虑:帮助我们准确识别database link和数据库的关系,避免误操作 如何设置:alter system set global_names=true scope=spfile; 参数名与推荐值:job_queue_processes>4 描述:指定了实例中job队列进程的数量(如J000…J999). 出于何种考虑:该参数控制了实例中能够并行运行的job的最大值,应设一个大于已配置的propagations 数量的值,同时也要考虑到可能还有其他数据库作业 如何设置:alter system set job_queue_processes=15; 参数名与推荐值:PARALLEL_MAX_SERVERS 描述:指定了实例中最大并行进程的数量 出于何种考虑: 在Streams环境中,capture进程和apply进程都会用到多个并行进程。 设置该初始化参数为适当值(10*CPU#)以保证总是有足够的可用并行进程; 每多一个capture或apply进程,则有必要为该参数+2再加上加入的capture或apply进程的并行度parallelism参数。 如何设置: alter system set PARALLEL_MAX_SERVERS=40; 参数名与推荐值:REMOTE_ARCHIVE_ENABLE 描述:指定是否将归档日志传送到远程目的地 出于何种考虑:只有downstream capture时会用到,不展开 参数名与推荐值:SGA_MAX_SIZE 描述:设置合理的SGA内存最大值 出于何种考虑:常见参数,不展开 参数名与推荐值:SGA_TARGET=0 描述:disable掉10g中的Automatic Shared Memory Management. 出于何种考虑:Oracle推荐在stream环境中手动指定streams_pool和shared_pool的大小而不使用10g中的内存自动管理特性 如何设置: alter system set sga_target=0; 参数名与推荐值:调优STREAMS_POOL_SIZE 描述: 为流池指定大小。流池包括了缓存的队列消息。此外,流池也会被用于并行capture和apply的内部通信。 建议参考V$STREAMS_POOL_ADVICE视图的信息判断最佳大小,避免spill溢出 出于何种考虑: 该参数可以动态修改。若该参数归零则实例中streams相关的进程和作业都将无法运行。流池的大小受到以下因素的影响: 1.capture进程的并行度,每增加一个capture进程有必要为流池增加10MB的大小; 此外当capture参数PARALLELISM大于1时,有必要为流池增加10Mb*parallelism的大小; 举例来说,若某capture进程的并行度parallelism设置为3,则需要为Streams池增加30Mb。 2.apply进程的并行度,每增加一个apply进程有必要为streams pool增加1MB; 此外当apply进程的并行度大于1时,为streams pool增加1Mb*parallelism的大小; 举例来说某apply进程的parallelism被设置为5,则需要为streams池增加5Mb。 3.Logical Change Records(LCRS)被存储在buffered queues缓存队列中; 适当增加Streams pool大小以适应源库和目标库上数据复制的数据量; Oracle建议在低负载的数据库上最小设置Streams pool为256Mb,而在活跃度高的OLTP环境中设置为500Mb; 通过V$STREAMS_POOL_ADVISE视图给出的建议进一步调整Streams Pool的大小 到一个合理值以避免过多的缓存队列溢出到磁盘上。 如何设置: select * from v$streams_pool_advice; /* 查询v$streams_pool_advice视图了解不同streams_Pool_size情况下的estd_spill_time */ alter system set streams_pool_size=500M;
完成以上设置后建议重启实例以便让全部参数生效,2边都要做。
4.为scott schema下的对象创建追加日志(supplemental log),可以使用dbms_capture_adm包的prepare_schema_instantiation存储过程为指定模式创建追加日志:
NAME prepare_schema_instantiation() FUNCTION prepare a schema for instantiation PARAMETERS schema_name - (IN) the name of the schema to prepare supplemental_logging - (IN) supplemental logging level ('NONE', 'KEYS', or 'ALL') NOTES KEYS means PRIMARY KEY, UNIQUE INDEX, and FOREIGN KEY levels combined. ----------------------------------------------------------------------------*/ PROCEDURE prepare_schema_instantiation( schema_name IN VARCHAR2, supplemental_logging IN VARCHAR2 DEFAULT 'KEYS'); /* 其默认supplemental_logging选项为Key,即为PRIMARY KEY, UNIQUE INDEX, and FOREIGN KEY等键 创建IMPLICIT的追加日志 */ /* 在10g或以上版本中prepare_xxx_instantiation存储过程也会隐式地创建追加日志组了 (In versions 10g and above,prepare_xxx_instantiation procedure implicitly creates supplemenal log groups. Type of supplemental logging that is enabled implicitly using this command can be checked using the sql in the following link to the documentation. However, additional supplemental logging might need to be enabled depending on the requirements as mentioned above)。 可以通过以下查询了解其追加日志组的属性: SELECT SCHEMA_NAME, SUPPLEMENTAL_LOG_DATA_PK log_pk, SUPPLEMENTAL_LOG_DATA_FK log_fk, SUPPLEMENTAL_LOG_DATA_UI log_ui, SUPPLEMENTAL_LOG_DATA_ALL log_all FROM DBA_CAPTURE_PREPARED_SCHEMAS; SCHEMA_NAME LOG_PK LOG_FK LOG_UI LOG_ALL ------------------------------ -------- -------- -------- -------- SCOTT IMPLICIT IMPLICIT IMPLICIT NO
5.
在源库上创建到目标库strmadmin用户的database link:
conn strmadmin/strmadmin; Connected. create database link clinicb.rh3.oracle.com connect to strmadmin identified by strmadmin using 'clinicb.rh3.oracle.com'; Database link created. /* 其中clinicb.rh3.oracle.com为目标库的全局数据库名,clinicb为db_name,rh3.oracle.com为domain_name */
在目标库上创建到源库strmadmin用户的database link:
conn strmadmin/strmadmin; Connected. create database link clinica.rh2.oracle.com connect to strmadmin identified by strmadmin using 'clinica.rh2.oracle.com'; Database link created. /* 其中clinica.rh2.oracle.com为源库的全局数据库名,clinica为数据库名,rh2.oracle.com为domain_name */
6.
在源库中分别为capture和apply创建队列queue:
begin dbms_streams_adm.set_up_queue( queue_table => 'apply_srctab', queue_name => 'apply_src', queue_user => 'strmadmin'); end; / PL/SQL procedure successfully completed. begin dbms_streams_adm.set_up_queue( queue_table => 'capture_srctab', queue_name => 'capture_src', queue_user => 'strmadmin'); end; / PL/SQL procedure successfully completed.
在目标库分别为capture和apply创建队列queue:
conn strmadmin/strmadmin@clinicb.rh3.oracle.com Connected. begin dbms_streams_adm.set_up_queue( queue_table => 'apply_desttab', queue_name => 'apply_dest', queue_user => 'strmadmin'); end; / PL/SQL procedure successfully completed. begin dbms_streams_adm.set_up_queue( queue_table => 'capture_desttab', queue_name => 'capture_dest', queue_user => 'strmadmin'); end; / PL/SQL procedure successfully completed.
8.
在源库clinica上为scott模式创建capture process:
conn strmadmin/strmadmin@clinica.rh2.oracle.com Connected. begin dbms_streams_adm.add_schema_rules ( schema_name => 'scott', streams_type => 'capture', streams_name => 'captures_src', queue_name => 'capture_src', include_dml => true, include_ddl => true, inclusion_rule => true); end; / PL/SQL procedure successfully completed.
9.
在源库clinica上创建apply process:
conn strmadmin/strmadmin@clinica.rh2.oracle.com Connected. begin dbms_streams_adm.add_schema_rules ( schema_name => 'scott', streams_type => 'apply', streams_name => 'applys_src', queue_name => 'apply_src', include_dml => true, include_ddl => true, source_database => 'clinicb.rh3.oracle.com'); end; / PL/SQL procedure successfully completed.
10.若需要在源库中解决冲突处理,则有必要设置conflict handlers,可以参考这个streams文件链接:http://download-east.oracle.com/docs/cd/B19306_01/server.102/b14228/conflict.htm
11.在源库clinica上配置propagation process:
conn strmadmin/strmadmin@clinica.rh2.oracle.com Connected. begin dbms_streams_adm.add_schema_propagation_rules ( schema_name => 'scott', streams_name => 'prop_src_to_dest', source_queue_name => 'capture_src', destination_queue_name => 'apply_dest@clinicb.rh3.oracle.com', include_dml => true, include_ddl => true, source_database => 'clinica.rh2.oracle.com'); end; / PL/SQL procedure successfully completed.
12.在目标库clinicb上配置capture process:
conn strmadmin/strmadmin@clinicb.rh3.oracle.com Connected. begin dbms_streams_adm.add_schema_rules ( schema_name => 'scott', streams_type => 'capture', streams_name => 'captures_dest', queue_name => 'capture_dest', include_dml => true, include_ddl => true); end; / PL/SQL procedure successfully completed.
13.在原库clinica上以目标库clinicb的SCN设置模式实例化SCN(schema instantiation SCN):
conn strmadmin/strmadmin@clinicb.rh3.oracle.com Connected. declare v_scn number; begin v_scn := dbms_flashback.get_system_change_number(); dbms_apply_adm.set_schema_instantiation_scn@clinica.rh2.oracle.com( source_schema_name => 'scott', source_database_name => 'clinicb.rh3.oracle.com', instantiation_scn => v_scn, recursive => true); end; /
14.在目标库clinicb上配置apply process:
conn strmadmin/strmadmin@clinicb.rh3.oracle.com Connected. begin dbms_streams_adm.add_schema_rules ( schema_name => 'scott', streams_type => 'apply', streams_name => 'applys_dest', queue_name => 'apply_dest', include_dml => true, include_ddl => true, source_database => 'clinica.rh2.oracle.com'); end; / PL/SQL procedure successfully completed.
15.在目标库上配置propagation process:
conn strmadmin/strmadmin@clinicb.rh3.oracle.com Connected. begin dbms_streams_adm.add_schema_propagation_rules ( schema_name => 'scott', streams_name => 'prop_dest_to_src', source_queue_name => 'capture_dest', destination_queue_name => 'apply_src@clinica.rh2.oracle.com', include_dml => true, include_ddl => true, source_database => 'clinicb.rh3.oracle.com'); end; / PL/SQL procedure successfully completed.
16.在目标库上初始化scott模式下的对象,有多种方法可以完成初始化。若对象均不存在,在可以使用export/import导入导出工具完成instantiation,若对象均已经存在,则直接使用dbms_apply_adm.set_schema_instantiation_scn 存储过程。
我们的环境中scott模式已存在于目标库中:
conn strmadmin/strmadmin@clinica.rh2.oracle.com Connected. declare v_scn number; begin v_scn := dbms_flashback.get_system_change_number(); dbms_apply_adm.set_schema_instantiation_scn@clinicb.rh3.oracle.com( source_schema_name => 'scott', source_database_name => 'clinica.rh2.oracle.com', instantiation_scn => v_scn, recursive => true); end; / PL/SQL procedure successfully completed.
17.若需要在目标库中解决冲突处理,则有必要设置conflict handlers,可以参考这个streams文件链接:
http://download-east.oracle.com/docs/cd/B19306_01/server. 102/b14228/conflict.htm
18.在目标库中启动capture和apply进程:
启动Apply: /* 以disable_on_erro参数为'N'启动apply进程,即便遭遇错误,apply也会继续处理LCR */ conn strmadmin/strmadmin@clinicb.rh3.oracle.com Connected. begin dbms_apply_adm.set_parameter ( apply_name => 'applys_dest', parameter => 'disable_on_error', value => 'N'); end; / PL/SQL procedure successfully completed. exec dbms_apply_adm.start_apply (apply_name=> 'applys_dest'); PL/SQL procedure successfully completed.
在目标库中启动capture process:
exec dbms_capture_adm.start_capture (capture_name=>'captures_dest'); PL/SQL procedure successfully completed.
19.同样的在源库启动capture和apply进程:
begin dbms_apply_adm.set_parameter ( apply_name => 'applys_src', parameter => 'disable_on_error', value => 'N'); end; / PL/SQL procedure successfully completed. exec dbms_apply_adm.start_apply (apply_name=> 'applys_src'); PL/SQL procedure successfully completed. 在源库启动capture: exec dbms_capture_adm.start_capture (capture_name=>'captures_src'); PL/SQL procedure successfully completed.
20.测试双向流复制是否成功:
测试ddl语句: SQL> conn scott/tiger@clinica.rh2.oracle.com Connected. SQL> create table test_streams(t1 int); Table created. SQL> conn scott/tiger@clinicb.rh3.oracle.com Connected. SQL> desc test_streams; Name Null? Type ----------------------------------------- -------- ---------------------------- T1 NUMBER(38) SQL> create table anti_test(t1 int); Table created. SQL> conn scott/tiger@clinica.rh2.oracle.com Connected. SQL> desc anti_test; Name Null? Type ----------------------------------------- -------- ---------------------------- T1 NUMBER(38) /* ddl语句双向复制成功 */ 测试dml语句: SQL> conn scott/tiger@clinica.rh2.oracle.com Connected. SQL> desc emp; Name Null? Type ----------------------------------------- -------- ---------------------------- EMPNO NOT NULL NUMBER(4) ENAME VARCHAR2(10) JOB VARCHAR2(9) MGR NUMBER(4) HIREDATE DATE SAL NUMBER(7,2) COMM NUMBER(7,2) DEPTNO NUMBER(2) SQL> update emp set sal=sal+50; 14 rows updated. SQL> commit; Commit complete. SQL> select sum(sal) from emp; SUM(SAL) ---------- 29725 SQL> conn scott/tiger@clinicb.rh3.oracle.com Connected. SQL> select sum(sal) from emp; SUM(SAL) ---------- 29725 SQL> update emp set sal=sal+50; 14 rows updated. SQL> commit; Commit complete. SQL> select sum(sal) from emp; SUM(SAL) ---------- 30425 SQL> conn scott/tiger@clinica.rh2.oracle.com Connected. SQL> select sum(sal) from emp; SUM(SAL) ---------- 30425 /* dml语句双向复制成功 */
to be continued…..
Reference:
Oracle Streams Replication Example:http://prodlife.wordpress.com/2008/02/21/oracle-streams-replication-example/
Oracle Streams: Recommendations for Heartbeat table:http://ksadba.wordpress.com/2008/07/02/oracle-streams-recommendations-for-heartbeat-table/
你好!
咨询您一个问题。我现在也有一套双向复制的环境,数据库都是10.2的,一个是10.2.0.5,另一个是10.2.0.4.目前的问题是存在重复capture的问题。A节点插入或更新操作propagation 到B节点后,B节点会重新capture这个操作,然后又传回A节点,A节点重新APPLY这个操作,导致数据重复复制出错。但这个现象在11G中是不存在的。
hello!
请问你是否在是在生产环境部署的?有没有具体的冲突解决方案?谢谢!
你好,该篇文章是我以教学为目的写的,现有的测试说明其步骤是有效的。
具体的冲突解决方案可以参考MOs Note:
Streams Conflict Resolution [ID 779801.1]
Streams Conflict Resolution [ID 230049.1]
你好,我现在的需求是,oracle 11gR2,两个数据库之间要进行全库的双向同步,看你的文章上面是以SCOTT为例子的,不知道全库同步怎么配置,另我看在oracel的OEM中有这个配置功能,不知道怎么操作,还望赐教。
GRANT CONNECT, RESOURCE, AQ_ADMINISTRATOR_ROLE,DBA to STRMADMIN;