Created By : Taher YahiaDate : 15-04-2010Databases : DB1 and DB2 Schema : TestTable : ONEColumn :( ID and Name )
------------------------------------------------------------------------------------------------------------ 1- Create admin streams user in both databases and grant streams authorization to streams admin :-
DB1 connect as sys/sys
SQL > create user streamadmin identified by streamadmin;SQL > grant dba,select_catalog_role to streamadmin;SQL > exec dbms_streams_auth.grant_admin_privilege('streamadmin',true);
----------------------------------------DB2conn connect as sys/sys
SQL > create user streamadmin identified by streamadmin;SQL > grant dba,select_catalog_role to streamadmin;SQL > exec dbms_streams_auth.grant_admin_privilege('streamadmin',true);
___________________________________________________________________________________________ 2- Alter tables at schema you will work on at both databases :-
DB1 connect as TEST/TESTSQL > alter table one add supplemental log group supp_log_one (ID,NAME);
---------------------------------------------------------------------DB2 connect as TEST/TESTSQL > alter table one add supplemental log group supp_log_one (ID,NAME);____________________________________________________________________________________________ Note :- To check if table altered correctly or not you can run this script on both database :-SQL > select log_group_name, table_name from dba_log_groups where owner='TEST';_____________________________________________________________________________________________
3- Create Database Link between 2 databases :- Connect As streamadmin
DB1
SQL > CREATE DATABASE LINK DB2 CONNECT TO STREAMADMIN IDENTIFIED BY streamadmin USING 'DB2';
DB2
SQL > CREATE DATABASE LINK DB1 CONNECT TO STREAMADMIN IDENTIFIED BY streamadmin USING 'DB1';
_______________________________________________________________________________________________4- Create Stream Queues under the streamadmin user to apply and capture the database changes to be replicated DB1 AND DB2
SQL > exec dbms_streams_adm.set_up_queue(queue_table => 'apply_q', queue_name => 'apply_q', queue_user => 'streamadmin');SQL > exec dbms_streams_adm.set_up_queue(queue_table => 'capture_q',queue_name => 'capture_q',queue_user => 'streamadmin');
_____________________________________________________________________________________________5- Setup data capture on both the databases
DB1
SQL > BEGINDBMS_STREAMS_ADM.ADD_SCHEMA_RULES (SCHEMA_NAME => 'TEST',STREAMS_TYPE => 'CAPTURE',STREAMS_NAME => 'CAPTURE_STREAM',QUEUE_NAME => 'CAPTURE_Q',INCLUDE_DML => TRUE,INCLUDE_DDL => TRUE,INCLUSION_RULE => TRUE);END;
DB2
SQL > BEGINDBMS_STREAMS_ADM.ADD_SCHEMA_RULES (SCHEMA_NAME => 'TEST',STREAMS_TYPE => 'CAPTURE',STREAMS_NAME => 'CAPTURE_STREAM',QUEUE_NAME => 'CAPTURE_Q',INCLUDE_DML => TRUE,INCLUDE_DDL => TRUE,INCLUSION_RULE => TRUE);END;-----------------------------------------------------------------------------------------------------------------------------------------------------6- Setup data apply on both the databases:
DB1
SQL > BEGINDBMS_STREAMS_ADM.ADD_SCHEMA_RULES (SCHEMA_NAME => 'TEST',STREAMS_TYPE => 'APPLY',STREAMS_NAME => 'APPLY_STREAM',QUEUE_NAME => 'APPLY_Q',INCLUDE_DML => TRUE,INCLUDE_DDL => TRUE,SOURCE_DATABASE => 'DB2');END;
DB2
SQL > BEGINDBMS_STREAMS_ADM.ADD_SCHEMA_RULES (SCHEMA_NAME => 'TEST',STREAMS_TYPE => 'APPLY',STREAMS_NAME => 'APPLY_STREAM',QUEUE_NAME => 'APPLY_Q',INCLUDE_DML => TRUE,INCLUDE_DDL => TRUE,SOURCE_DATABASE => 'DB1');END;
_______________________________________________________________________________
7- Setup propogation process on both the databases: Note :- Its basically setting up related between the capture process on one database and apply process on the other database. Thes need to run as streamadmin user.
DB1
SQL > BEGINDBMS_STREAMS_ADM.ADD_SCHEMA_PROPAGATION_RULES (SCHEMA_NAME => 'TEST',STREAMS_NAME => 'TEST1_TO_TEST',SOURCE_QUEUE_NAME => 'CAPTURE_Q',DESTINATION_QUEUE_NAME => 'APPLY_Q@DB2',INCLUDE_DML => TRUE,INCLUDE_DDL => TRUE,SOURCE_DATABASE => 'DB1');END;
DB2
SQL > BEGINDBMS_STREAMS_ADM.ADD_SCHEMA_PROPAGATION_RULES (SCHEMA_NAME => 'TEST',STREAMS_NAME => 'TEST_TO_TEST1',SOURCE_QUEUE_NAME => 'CAPTURE_Q',DESTINATION_QUEUE_NAME => 'APPLY_Q@DB1',INCLUDE_DML => TRUE,INCLUDE_DDL => TRUE,SOURCE_DATABASE => 'DB2');END;
____________________________________________________________________________________8- Setup schema instantiation SCN on Both DataBase :
DB1SQL > DECLAREISCN NUMBER;BEGINISCN := DBMS_FLASHBACK.GET_SYSTEM_CHANGE_NUMBER();DBMS_APPLY_ADM.SET_SCHEMA_INSTANTIATION_SCN@DB2 (SOURCE_SCHEMA_NAME => 'TEST',SOURCE_DATABASE_NAME => 'DB1',INSTANTIATION_SCN => ISCN,RECURSIVE => TRUE);END;
DB2
SQL > .DECLAREISCN NUMBER;BEGINISCN := DBMS_FLASHBACK.GET_SYSTEM_CHANGE_NUMBER();DBMS_APPLY_ADM.SET_SCHEMA_INSTANTIATION_SCN@DB1 (SOURCE_SCHEMA_NAME => 'TEST',SOURCE_DATABASE_NAME => 'DB2',INSTANTIATION_SCN => ISCN,RECURSIVE => TRUE); END;
__________________________________________________________________________________9- Start capture and apply process:
DB1 and DB2
SQL > EXEC DBMS_APPLY_ADM.SET_PARAMETER (APPLY_NAME => 'APPLY_STREAM', PARAMETER => 'DISABLE_ON_ERROR', VALUE => 'N');SQL > EXEC DBMS_APPLY_ADM.START_APPLY (APPLY_NAME => 'APPLY_STREAM');SQL > EXEC DBMS_CAPTURE_ADM.START_CAPTURE (CAPTURE_NAME => 'CAPTURE_STREAM');
____________________________________________________________________________________
10- To make sure that there are any errors
SQL > SELECT APPLY_NAME, SOURCE_DATABASE, LOCAL_TRANSACTION_ID, ERROR_NUMBER,ERROR_MESSAGE,MESSAGE_COUNTFROM DBA_APPLY_ERROR;
------------------------------------------------------------------------------------------------------------ 1- Create admin streams user in both databases and grant streams authorization to streams admin :-
DB1 connect as sys/sys
SQL > create user streamadmin identified by streamadmin;SQL > grant dba,select_catalog_role to streamadmin;SQL > exec dbms_streams_auth.grant_admin_privilege('streamadmin',true);
----------------------------------------DB2conn connect as sys/sys
SQL > create user streamadmin identified by streamadmin;SQL > grant dba,select_catalog_role to streamadmin;SQL > exec dbms_streams_auth.grant_admin_privilege('streamadmin',true);
___________________________________________________________________________________________ 2- Alter tables at schema you will work on at both databases :-
DB1 connect as TEST/TESTSQL > alter table one add supplemental log group supp_log_one (ID,NAME);
---------------------------------------------------------------------DB2 connect as TEST/TESTSQL > alter table one add supplemental log group supp_log_one (ID,NAME);____________________________________________________________________________________________ Note :- To check if table altered correctly or not you can run this script on both database :-SQL > select log_group_name, table_name from dba_log_groups where owner='TEST';_____________________________________________________________________________________________
3- Create Database Link between 2 databases :- Connect As streamadmin
DB1
SQL > CREATE DATABASE LINK DB2 CONNECT TO STREAMADMIN IDENTIFIED BY streamadmin USING 'DB2';
DB2
SQL > CREATE DATABASE LINK DB1 CONNECT TO STREAMADMIN IDENTIFIED BY streamadmin USING 'DB1';
_______________________________________________________________________________________________4- Create Stream Queues under the streamadmin user to apply and capture the database changes to be replicated DB1 AND DB2
SQL > exec dbms_streams_adm.set_up_queue(queue_table => 'apply_q', queue_name => 'apply_q', queue_user => 'streamadmin');SQL > exec dbms_streams_adm.set_up_queue(queue_table => 'capture_q',queue_name => 'capture_q',queue_user => 'streamadmin');
_____________________________________________________________________________________________5- Setup data capture on both the databases
DB1
SQL > BEGINDBMS_STREAMS_ADM.ADD_SCHEMA_RULES (SCHEMA_NAME => 'TEST',STREAMS_TYPE => 'CAPTURE',STREAMS_NAME => 'CAPTURE_STREAM',QUEUE_NAME => 'CAPTURE_Q',INCLUDE_DML => TRUE,INCLUDE_DDL => TRUE,INCLUSION_RULE => TRUE);END;
DB2
SQL > BEGINDBMS_STREAMS_ADM.ADD_SCHEMA_RULES (SCHEMA_NAME => 'TEST',STREAMS_TYPE => 'CAPTURE',STREAMS_NAME => 'CAPTURE_STREAM',QUEUE_NAME => 'CAPTURE_Q',INCLUDE_DML => TRUE,INCLUDE_DDL => TRUE,INCLUSION_RULE => TRUE);END;-----------------------------------------------------------------------------------------------------------------------------------------------------6- Setup data apply on both the databases:
DB1
SQL > BEGINDBMS_STREAMS_ADM.ADD_SCHEMA_RULES (SCHEMA_NAME => 'TEST',STREAMS_TYPE => 'APPLY',STREAMS_NAME => 'APPLY_STREAM',QUEUE_NAME => 'APPLY_Q',INCLUDE_DML => TRUE,INCLUDE_DDL => TRUE,SOURCE_DATABASE => 'DB2');END;
DB2
SQL > BEGINDBMS_STREAMS_ADM.ADD_SCHEMA_RULES (SCHEMA_NAME => 'TEST',STREAMS_TYPE => 'APPLY',STREAMS_NAME => 'APPLY_STREAM',QUEUE_NAME => 'APPLY_Q',INCLUDE_DML => TRUE,INCLUDE_DDL => TRUE,SOURCE_DATABASE => 'DB1');END;
_______________________________________________________________________________
7- Setup propogation process on both the databases: Note :- Its basically setting up related between the capture process on one database and apply process on the other database. Thes need to run as streamadmin user.
DB1
SQL > BEGINDBMS_STREAMS_ADM.ADD_SCHEMA_PROPAGATION_RULES (SCHEMA_NAME => 'TEST',STREAMS_NAME => 'TEST1_TO_TEST',SOURCE_QUEUE_NAME => 'CAPTURE_Q',DESTINATION_QUEUE_NAME => 'APPLY_Q@DB2',INCLUDE_DML => TRUE,INCLUDE_DDL => TRUE,SOURCE_DATABASE => 'DB1');END;
DB2
SQL > BEGINDBMS_STREAMS_ADM.ADD_SCHEMA_PROPAGATION_RULES (SCHEMA_NAME => 'TEST',STREAMS_NAME => 'TEST_TO_TEST1',SOURCE_QUEUE_NAME => 'CAPTURE_Q',DESTINATION_QUEUE_NAME => 'APPLY_Q@DB1',INCLUDE_DML => TRUE,INCLUDE_DDL => TRUE,SOURCE_DATABASE => 'DB2');END;
____________________________________________________________________________________8- Setup schema instantiation SCN on Both DataBase :
DB1SQL > DECLAREISCN NUMBER;BEGINISCN := DBMS_FLASHBACK.GET_SYSTEM_CHANGE_NUMBER();DBMS_APPLY_ADM.SET_SCHEMA_INSTANTIATION_SCN@DB2 (SOURCE_SCHEMA_NAME => 'TEST',SOURCE_DATABASE_NAME => 'DB1',INSTANTIATION_SCN => ISCN,RECURSIVE => TRUE);END;
DB2
SQL > .DECLAREISCN NUMBER;BEGINISCN := DBMS_FLASHBACK.GET_SYSTEM_CHANGE_NUMBER();DBMS_APPLY_ADM.SET_SCHEMA_INSTANTIATION_SCN@DB1 (SOURCE_SCHEMA_NAME => 'TEST',SOURCE_DATABASE_NAME => 'DB2',INSTANTIATION_SCN => ISCN,RECURSIVE => TRUE); END;
__________________________________________________________________________________9- Start capture and apply process:
DB1 and DB2
SQL > EXEC DBMS_APPLY_ADM.SET_PARAMETER (APPLY_NAME => 'APPLY_STREAM', PARAMETER => 'DISABLE_ON_ERROR', VALUE => 'N');SQL > EXEC DBMS_APPLY_ADM.START_APPLY (APPLY_NAME => 'APPLY_STREAM');SQL > EXEC DBMS_CAPTURE_ADM.START_CAPTURE (CAPTURE_NAME => 'CAPTURE_STREAM');
____________________________________________________________________________________
10- To make sure that there are any errors
SQL > SELECT APPLY_NAME, SOURCE_DATABASE, LOCAL_TRANSACTION_ID, ERROR_NUMBER,ERROR_MESSAGE,MESSAGE_COUNTFROM DBA_APPLY_ERROR;