drop user strmadmin cascade;
create user strmadmin identified by strmadmin default tablespace tbs_stream temporary tablespace temp;
grant connect,resource,dba,aq_administrator_role to strmadmin;
create user strmadmin identified by strmadmin default tablespace tbs_stream temporary tablespace temp;
grant connect,resource,dba,aq_administrator_role to strmadmin;
begin
dbms_streams_auth.grant_admin_privilege(grantee =>'strmadmin',grant_privileges => true);
end;
切换回source数据库,以streams的操作用户stradmin连接 ,创建队列
exec dbms_streams_adm.set_up_queue();
切换到target数据库,以streams的操作用户stradmin连接 ,创建队列
exec dbms_streams_adm.set_up_queue();
sour> begin
dbms_rule_adm.create_rule_set(
rule_set_name => 'strmadmin.propagation_rules',
evaluation_context => 'sys.streams$_evaluation_context');
end;
sour> begin
dbms_rule_adm.create_rule(
rule_name => 'strmadmin.member_pro_dml',
condition => ':dml.get_object_owner()=''TEST02'' AND ' ||
' :dml.is_null_tag() =''Y'' AND '||
' :dml.get_source_database_name()= ''LIRHZ'' ');
end;
写入表: dba_rules
begin
dbms_rule_adm.create_rule(
rule_name => 'strmadmin.member_pro_ddl',
condition => '(:ddl.get_object_owner()=''TEST02'' OR ' ||
' :ddl.get_base_table_owner() =''TEST02'') AND ' ||
' :ddl.is_null_tag() =''Y'' AND '||
' :ddl.get_source_database_name()= ''LIRHZ'' ');
end;
把dml、ddl规则添加到规则集中
begin
dbms_rule_adm.add_rule(
rule_name => 'strmadmin.member_pro_dml',
rule_set_name => 'strmadmin.propagation_rules');
end;
begin
dbms_rule_adm.add_rule(
rule_name => 'strmadmin.member_pro_ddl',
rule_set_name => 'strmadmin.propagation_rules');
end;
创建propagation进程。
dbms_propagation_adm.create_propagation(
propagation_name => 'pri_to_klir',
source_queue => 'strmadmin.streams_queue',
destination_queue => 'strmadmin.streams_queue',
destination_dblink => 'klir',
rule_set_name => 'strmadmin.propagation_rules');
end;
手动建立捕获规则和捕获进程:
sour> begin
dbms_rule_adm.create_rule_set(
rule_set_name => 'strmadmin.capture_rules',
evaluation_context => 'sys.streams$_evaluation_context');
end;
begin
dbms_rule_adm.create_rule(
rule_name => 'strmadmin.member_cap_dml',
condition => ':dml.get_object_owner()=''TEST02'' AND ' ||
' :dml.is_null_tag() =''Y'' AND '||
' :dml.get_source_database_name()= ''LIRHZ'' ');
end;
sour> begin
dbms_rule_adm.create_rule(
rule_name => 'strmadmin.member_cap_ddl',
condition => '(:ddl.get_object_owner()=''TEST02'' OR ' ||
' :ddl.get_base_table_owner() =''TEST02'') AND ' ||
' :ddl.is_null_tag() =''Y'' AND '||
' :ddl.get_source_database_name()= ''LIRHZ'' ');
end;
加入规则集。
sour> begin
dbms_rule_adm.add_rule(
rule_name => 'strmadmin.member_cap_dml',
rule_set_name => 'strmadmin.capture_rules');
end;
dbms_rule_adm.add_rule(
rule_name => 'strmadmin.member_cap_ddl',
rule_set_name => 'strmadmin.capture_rules');
end;
begin
dbms_capture_adm.create_capture(
queue_name => 'strmadmin.streams_queue',
capture_name => 'lirhz_capture',
rule_set_name => 'strmadmin.capture_rules'); 使用捕获的规则集。
end;
RULE_NAME RULE_CONDITION
------------------------------ --------------------------------------------------------------------------------
MEMBER_CAP_DDL (:ddl.get_object_owner()='TEST02' OR :ddl.get_base_table_owner() ='TEST02') AND
MEMBER_CAP_DML :dml.get_object_owner()='TEST02' AND :dml.is_null_tag() ='Y' AND :dml.get_sour
MEMBER_PRO_DDL (:ddl.get_object_owner()='TEST02' OR :ddl.get_base_table_owner() ='TEST02') AND
MEMBER_PRO_DML :dml.get_object_owner()='TEST02' AND :dml.is_null_tag() ='Y' AND :dml.get_sour
begin
dbms_capture_adm.prepare_schema_instantiation(
schema_name => 'test02',
supplemental_logging => 'keys');
end;
目标库做应用scn。
declare
iscn number;
begin
iscn:=dbms_flashback.get_system_change_number();
dbms_apply_adm.set_schema_instantiation_scn@klir(
source_schema_name => 'test02',
source_database_name => 'lirhz',
instantiation_scn => iscn,
recursive => true);
end;
目标库使用dbms_streams_adm。建立apply.建立后的规则由ORACLE自动命名。
begin
dbms_streams_adm.add_schema_rules(
schema_name => 'test02',
streams_type => 'apply',
streams_name => 'apply_standy',
queue_name => 'strmadmin.STREAMS_QUEUE',
include_dml => true,
include_ddl => true,
include_tagged_lcr => false,
source_database => 'lirhz',
inclusion_rule => true);
end;
设置异常规则。
1.在传播级别修改规则,即设置某些规则不传播。
2.在捕获级别修改规则,即设置某些规则不捕获。
原先的传播规则命令
begin
dbms_rule_adm.create_rule(
rule_name => 'strmadmin.member_pro_ddl',
condition => '(:ddl.get_object_owner()=''TEST02'' OR ' ||
' :ddl.get_base_table_owner() =''TEST02'') AND ' ||
' :ddl.is_null_tag() =''Y'' AND '||
' :ddl.get_source_database_name()= ''LIRHZ'' ');
end;
修改dml传播规则。增加F1,F5的不传条件。
dbms_rule_adm.alter_rule(
rule_name => 'strmadmin.member_pro_dml',
condition => ':dml.get_object_owner()=''TEST02'' AND ' ||
' :dml.is_null_tag() =''Y'' AND '||
' :dml.get_source_database_name()= ''LIRHZ'' and ' ||
' (:dml.get_object_name() <> ''F1'' and :dml.get_object_name() <> ''F5'' ) ');
end;
修改ddl传播规则。
begin
dbms_rule_adm.alter_rule(
rule_name => 'strmadmin.member_pro_ddl',
condition => '(:ddl.get_object_owner()=''TEST02'' OR ' ||
' :ddl.get_base_table_owner() =''TEST02'') AND ' ||
' :ddl.is_null_tag() =''Y'' AND '||
' :ddl.get_source_database_name()= ''LIRHZ'' and ' ||
' (:dml.get_object_name() <> ''F1'' and :dml.get_object_name() <> ''F5'' ) ');
end;
(:ddl.get_object_owner()='TEST02' OR :ddl.get_base_table_owner() ='TEST02') AND :ddl.is_null_tag() ='Y' AND :ddl.get_source_database_name()= 'LIRHZ' and (:dml.get_object_name() <> 'F1' and :dml.get_object_name() <> 'F5' )
:dml.get_object_owner()='TEST02' AND :dml.is_null_tag() ='Y' AND :dml.get_source_database_name()= 'LIRHZ' and (:dml.get_object_name() <> 'F1' and :dml.get_object_name() <> 'F5' )
SQL> exec dbms_propagation_adm.start_propagation('PRI_TO_KLIR');
PL/SQL procedure successfully completed
源库的f1,f5分别插入记录。
SQL> insert into test02.f1 values (33,'guangzhou');
1 row inserted
SQL> insert into test02.f1 values (34,'guangzhou');
1 row inserted
SQL> insert into test02.f5 values (34,'guangzhou');
1 row inserted
SQL> insert into test02.f5 values (33,'guangzhou');
1 row inserted
SQL> commit;
Commit complete
源库可以看到新加的33,34记录,
SQL> select * from test02.f5 where id in (33,34);
ID NAME
---------- ----------
33 guangzhou
34 guangzhou
SQL> select * from test02.f1 where id in (33,34);
ID NAME
---------- ----------
33 guangzhou
34 guangzhou
SQL> select * from test02.f5 where id in (33,34);
ID NAME
---------- ----------
SQL> select * from test02.f1 where id in (33,34);
ID NAME
---------- ----------
通过以上实验可以证明,在传播进程中设置条件也可以控制到那些表不传输。
SQL> select capture_name,START_SCN,STATUS,CAPTURED_SCN,APPLIED_SCN from dba_capture;
CAPTURE_NAME START_SCN STATUS CAPTURED_SCN APPLIED_SCN
------------------------------ ---------- -------- ------------ -----------
LIRHZ_CAPTURE 2582441264 ENABLED 2582659272 2582659140
实验需求,取消在传播进程规则中设置条件,修改为在捕获进程中设置条件。设置F6,F7表不传。
SQL> exec dbms_propagation_adm.stop_propagation('PRI_TO_KLIR');
修改dml传播规则。重新删除F1,F5表的不传条件。
sour> begin
dbms_rule_adm.alter_rule(
rule_name => 'strmadmin.member_pro_dml',
condition => ':dml.get_object_owner()=''TEST02'' AND ' ||
' :dml.is_null_tag() =''Y'' AND '||
' :dml.get_source_database_name()= ''LIRHZ'' ');
end;
修改ddl传播规则。
begin
dbms_rule_adm.alter_rule(
rule_name => 'strmadmin.member_pro_ddl',
condition => '(:ddl.get_object_owner()=''TEST02'' OR ' ||
' :ddl.get_base_table_owner() =''TEST02'') AND ' ||
' :ddl.is_null_tag() =''Y'' AND '||
' :ddl.get_source_database_name()= ''LIRHZ'' ');
end;
SQL> exec dbms_propagation_adm.start_propagation('PRI_TO_KLIR');
SQL> exec dbms_capture_adm.stop_capture('LIRHZ_CAPTURE');
源库在f6,f7表中各插入2条记录。
1 row inserted
SQL> insert into test02.f6 values (21);
1 row inserted
SQL> insert into test02.f7 values (21);
1 row inserted
SQL> insert into test02.f7 values (20);
1 row inserted
SQL> commit;
Commit complete
修改源库的捕获规则
begin
dbms_rule_adm.alter_rule(
rule_name => 'strmadmin.member_cap_dml',
condition => ':dml.get_object_owner()=''TEST02'' AND ' ||
' :dml.is_null_tag() =''Y'' AND '||
' :dml.get_source_database_name()= ''LIRHZ'' and ' ||
' (:dml.get_object_name() <> ''F6'' and :dml.get_object_name() <> ''F7'' ) ');
end;
sour> begin
dbms_rule_adm.alter_rule(
rule_name => 'strmadmin.member_cap_ddl',
condition => '(:ddl.get_object_owner()=''TEST02'' OR ' ||
' :ddl.get_base_table_owner() =''TEST02'') AND ' ||
' :ddl.is_null_tag() =''Y'' AND '||
' :ddl.get_source_database_name()= ''LIRHZ'' and ' ||
' (:dml.get_object_name() <> ''F6'' and :dml.get_object_name() <> ''F7'' ) ');
end;
SQL> exec dbms_capture_adm.start_capture('LIRHZ_CAPTURE');
SQL> select * from test02.f6 where id in (20,21);
ID
----------
SQL> select * from test02.f7 where id in (20,21);
ID
----------
SQL> create table test02.f8 (id number,name varchar2(10));
Table created
ID NAME
---------- ----------
SQL> select capture_name,START_SCN,STATUS,CAPTURED_SCN,APPLIED_SCN from dba_capture;
CAPTURE_NAME START_SCN STATUS CAPTURED_SCN APPLIED_SCN
------------------------------ ---------- -------- ------------ -----------
LIRHZ_CAPTURE 2582441264 ENABLED 2582752740 2582752740