针对某个表使用高级复制进行数据同步示例

 

高级复制与流复制概念:

高级复制(Advanced Replication)

高级复制也称为对称复制,分为多主体站点复制(Multiple Master Rplication)、物化视图站点复制(Materialized View Replication)和前两种的混合复制。高级复制主要是用在对称的、等同的数据库(仅限Oracle数据库之间)表之间的(单向或双向)的复制,以满足分布式应用的需求。多主体站点复制基于Oracle的触发器(Trigger)捕获DMLDDL操作,并将这些操作封装在Remote Procedure Calls(RPCs)里,并借助Deferred Transaction Queue来传递RPCs并在目标数据库利用Internal Trigger执行传递过来的RPCs从而实现了数据的同步,如果仅仅是高级复制环境中的只读物化视图环境,那么会直接利用解析物化视图日志的方式来实现数据的同步,而不用DTQ来传递RPCs,基于这一特点,高级复制对网络的稳定性和传输速度要求较高,配置也稍显麻烦。另外一点要特别注意,就是物化视图复制方式不支持DDL的变更同步。

  流复制(Stream)

    流复制是Oracle 10g新推出的同步技术,基于logminer(数据库必须运行在Archive Log模式),通过Oracle Advanced Queue来实现数据的同步复制,可以实现表,用户,数据库级别的同步,而高级复制支持的对象仅为表,索引,同义词,触发器,视图,过程,函数,程序包,程序包体等。Stream相对高级复制而言配置简单,通过oracle gateway还可用于异构平台和异构数据库的数据同步,这是高级复制做不到的。

实验如下:

1.数据库名、global_name等基础环境:

源端:

SYS@bys1>select name,db_unique_name from v$database;

NAME      DB_UNIQUE_NAME
--------- ------------------------------
BYS1      bys1
SYS@bys1>show parameter global_name
NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
global_names                         boolean     TRUE
SYS@bys1>select * from global_name;
GLOBAL_NAME
----------------------------------------------------------------------------------------------------
BYS1
SYS@bys1>show parameter job_queue_processes
NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
job_queue_processes                  integer     1000

#######

目标端:

SYS@bys2>select name,db_unique_name from v$database;
NAME               DB_UNIQUE_NAME
------------------ ------------------------------------------------------------
BYS1               BYS2
SYS@bys2>show parameter global_name
NAME                                 TYPE                  VALUE
------------------------------------ ---------------------- ------------------------------
global_names                         boolean               TRUE
SYS@bys2>select * from global_name;
GLOBAL_NAME
----------------------------------------------------------------------------------------------------
BYS2
SYS@bys2>show parameter job_queue_processes
NAME                                 TYPE                  VALUE
------------------------------------ ---------------------- ------------------------------
job_queue_processes                  integer               1000

###########################################################


2.在源端和目标端分别使用以下语句创建用户:

复制用户--源和目标都做:

create user repadmin identified by repadmin;

默认的用户表空间是USER,默认临时表空间是TEMP的话,以下两句可以不执行。

alter user repadmin default tablespace users;
alter user repadmin temporary tablespace temp;
grant connect ,resource to repadmin;
execute dbms_repcat_admin.grant_admin_any_schema('REPADMIN');
grant comment any table to repadmin;
execute dbms_defer_sys.register_propagator('REPADMIN');
grant execute any procedure to REPADMIN;
源和目标端创建业务用户-源和目标都做:
create user gp identified by gp default tablespace users;
grant dba to gp;
###################################################################

3.创建测试表--源端和目标端都做

SYS@bys1>conn gp/gp
Connected.
GP@bys1>create table test(x int primary key);
Table created.
GP@bys1>select * from tab;
TNAME                          TABTYPE  CLUSTERID
------------------------------ ------- ----------

TEST                           TABLE

目标端:

SYS@bys2>conn gp/gp
Connected.
GP@bys2>create table test(x int primary key);
Table created.

###################################################################

4.创建DBLINK--源和目标都创建到对方的DBLINK

注意:如果db_domain值为空,并且global names设置为true的情况下,那么这里link关键词后面的这个“bys2”必须写目标端global_name的值,而using关键词后面的“bys2”表示的是连接目标端的SERVICE NET NAME

源端:

GP@bys1>conn repadmin/repadmin

Connected.
REPADMIN@bys1>create database link bys2 connect to repadmin identified by repadmin using 'bys2';
Database link created.
REPADMIN@bys1>select * from global_name@bys2;
GLOBAL_NAME
-----------------------------------------------
BYS2

目标端:

GP@bys2>conn repadmin/repadmin
Connected.
REPADMIN@bys2>create database link bys1 connect to repadmin identified by repadmin using 'bys1';
Database link created.
REPADMIN@bys2>select * from global_name@bys1;
GLOBAL_NAME
---------------------------------------
BYS1
################################################################################

5.在源端创建复制所需的操作

在源端创建复制组

REPADMIN@bys1> execute dbms_repcat.create_master_repgroup('rep');
PL/SQL procedure successfully completed.
REPADMIN@bys1>select gname ,master ,status from dba_repgroup where gname = 'REP';
GNAME                          M STATUS
------------------------------ - ---------
REP                            Y QUIESCED

在源端加入复制对象

REPADMIN@bys1>execute dbms_repcat.create_master_repobject(sname=>'gp',oname=>'test',type=>'table',use_existing_object=>true ,gname=>'rep' ,copy_rows=>false);
PL/SQL procedure successfully completed.
REPADMIN@bys1>select gname ,master ,status from dba_repgroup where gname = 'REP';
GNAME                          M STATUS
------------------------------ - ---------
REP                            Y QUIESCED

在源端启动复制支持

REPADMIN@bys1>execute dbms_repcat.generate_replication_support('gp','test' ,'table');
PL/SQL procedure successfully completed.
REPADMIN@bys1>select gname ,master ,status from dba_repgroup where gname = 'REP';
GNAME                          M STATUS
------------------------------ - ---------
REP                            Y QUIESCED
col sname for a10
col oname for a10
col gname for a10
REPADMIN@bys1>select sname,oname,status,gname from dba_repobject;      ---这一步要做一下检查,确认启动的复制对象是正常的。
SNAME      ONAME      STATUS     GNAME
---------- ---------- ---------- ----------
GP         TEST       VALID      REP
GP         TEST$RP    VALID      REP
GP         TEST$RP    VALID      REP
注:如添加源端复制支持这一步输入 错了表名或用户名,通过dba_repobject;可以查出,可以使用下面语句删除错误的配置,不然下一步添加复制节点会报错找不到相关对象。
execute DBMS_REPCAT.DROP_MASTER_REPOBJECT (sname=>'gp',oname=>'T',type=>'TABLE');

在源端添加复制节点

REPADMIN@bys1>execute dbms_repcat.add_master_database(gname=>'rep',master=>'bys2' ,use_existing_objects=>true ,copy_rows=>false ,propagation_mode=>'synchronous');
PL/SQL procedure successfully completed.
REPADMIN@bys1>col dblink for a10
REPADMIN@bys1> select gname ,dblink ,masterdef ,master from dba_repsites where gname='REP';
GNAME      DBLINK     M M
---------- ---------- - -
REP        BYS1       Y Y
REP        BYS2       N Y
REPADMIN@bys2>col gname for a10
REPADMIN@bys2>col dblink for a10
REPADMIN@bys2> select gname ,dblink ,masterdef ,master from dba_repsites where gname='REP';
GNAME      DBLINK     MA MA
---------- ---------- -- --
REP        BYS1       Y  Y
REP        BYS2       N  Y

在源端启动复制

REPADMIN@bys1>execute dbms_repcat.resume_master_activity('rep' ,true);
PL/SQL procedure successfully completed.

################################


6.进行数据同步测试:

DML操作可以同步:

源端:

13:51:48 GP@bys1>select * from test;

no rows selected
13:51:57 GP@bys1>insert into test values(2);
1 row created.
13:52:02 GP@bys1>commit;
Commit complete.
13:52:06 GP@bys1>select * from test;
         X
----------
         2

目标端:

13:51:19 GP@bys2>select * from test;
no rows selected
13:51:22 GP@bys2>select * from test;
no rows selected
13:51:52 GP@bys2>select * from test;
         X
----------
         2
13:52:13 GP@bys2>

DDL操作不能同步:

源端:

13:52:08 GP@bys1>truncate table test;

Table truncated.
13:52:21 GP@bys1>select * from test;
no rows selected

目标端:

13:52:27 GP@bys2>select * from test;
         X
----------
         2
13:52:28 GP@bys2>select * from test;
        X
----------
         2

你可能感兴趣的:(数据同步)