2.演示针对某个表使用高级复制进行数据同步。
3.演示使用物化视图的方式进行数据同步。
2.演示针对某个表使用高级复制进行数据同步。
2.1 配置GLOBAL_NAME
2.1.1 源库
SQL> alter database rename global_name to db1.com;
Database altered.
SQL> show parameter global_names;
NAME TYPE VALUE
-----------------------------------------
global_names boolean FALSE
SQL> alter system set global_names=true scope=both;
System altered.
2.1.2 目标库:
SQL> alter system set global_names=true scope=both;
System altered.
SQL> alter database rename global_name to db2.com;
Database altered.
SQL> show parameter global_name;
NAME TYPE VALUE
-----------------------------------------------------
global_names boolean TRUE
SQL> select * from global_name;
GLOBAL_NAME
---------------------
DB2.COM
2.2 两个库中创建应用用户,
这里我还是使用HR 用户及用户下的po 来做实验
SQL> create table po(id integer);
Table created.
SQL> alter table po add constraint pk_po_id primary key (ID);
Table altered.
2.3 两个库中创建复制管理用户repadmin
CREATE USER repadmin IDENTIFIED BY oracle DEFAULT TABLESPACE users;
GRANT connect,resource,DBA TO repadmin;
--授予repadmin用户权限可以管理当前站点中任何主体组
EXECUTE dbms_repcat_admin.grant_admin_any_schema('REPADMIN');
--授予repadmin用户权限可以为任何表创建snapshot logs
GRANT comment any table TO REPADMIN;
GRANT lock any table TO REPADMIN;
--指定repadmin用户为propagator,并授予执行任何procedure的权限
execute dbms_defer_sys.register_propagator('REPADMIN');
GRANT execute any procedure TO REPADMIN;
2.4在两个数据库上建立数据库链接
create public database link db2.com connect to repadmin identified by oracle using 'DB2';
create public database link db1.com connect to repadmin identified by oracle using 'DB1';
2.5 创建复制组:
execute dbms_repcat.create_master_repgroup('repg');
--删除复制组
--execute dbms_repcat.drop_master_repgroup('repg');
SQL>
SQL> SELECT GNAME,MASTER,STATUS FROM DBA_REPGROUP;
GNAME MASTER STATUS
--------------------------------
REPG Y QUIESCED
2.6 在复制组里加入复制对象:
execute dbms_repcat.create_master_repobject(sname=>'hr',oname=>'po',type=>'table',use_existing_object=>true,gname=>'repg',copy_rows=>false);
--删除对象
--execute dbms_repcat.drop_master_repobject(sname=>'hr',oname=>'po',type=>'table',DROP_OBJECTS=>true);
select sname,oname,status,gname from dba_repobject where gname='REPG';
SNAME ONAME STATUS GNAME
----------------------------------------------------------------------------------------
HR PO VALID REPG
SQL>
2.7对复制对象产生复制支持:
execute dbms_repcat.generate_replication_support('hr','po','table');
execute dbms_repcat.drop_master_repobject('hr','po','table'); [取消同步]
SQL> select sname,oname,status,gname from dba_repobject where gname='REPG';
SNAME ONAME STATUS GNAME
----------------------------------------------------------------------------------------
HR PO VALID REPG
HR PO$RP VALID REPG
HR PO$RP VALID REPG
2.8添加主体复制节点:
begin
dbms_repcat.add_master_database(gname=>'repg',
master=>'db2.com',
use_existing_objects=>true,
copy_rows=>false,
propagation_mode=>'synchronous');
end;
/
第一次执行上面代码出错:
ERROR at line 1:
ORA-23358: invalid remote user
ORA-06512: at "SYS.DBMS_SYS_ERROR", line 79
ORA-06512: at "SYS.DBMS_REPCAT_RPC", line 12
ORA-06512: at "SYS.DBMS_REPCAT_RPC", line 1768
ORA-06512: at "SYS.DBMS_REPCAT_UTL", line 4280
ORA-06512: at "SYS.DBMS_REPCAT_MAS", line 2158
ORA-06512: at "SYS.DBMS_REPCAT", line 146
ORA-06512: at line 2
重做后又没问题,不知什么原因。
select gname,dblink,masterdef ,master FROM SYS.DBA_REPSITES WHERE GNAME='REPG';
SQL> select gname,dblink,masterdef ,master FROM SYS.DBA_REPSITES WHERE GNAME='REPG';
GNAME DBLINK MASTERDEF MASTER
--------------- ------------------------- ---------- ----------
REPG DB1.COM Y Y
测试数据同步,通过
**********************************************
参数说明:
gname 主复制组名
master 加入主复制节点的另一个数据库
use_existing_object true 表示用主复制节点已经存在的数据库对象
copy_rows false表示第一次开始复制时不用和主复制节点保持一致
propagation_mode 同步地执行
synchronous(上边配置异步复制)
***********************************************
在主体定义站点启动复制:
execute dbms_repcat.resume_master_activity('repg',true);
execute dbms_repcat.resume_master_activity('repg',false);
execute dbms_repcat.suspend_master_activity('repg')[停止复制]
--------------------------------------------------------------------
3.演示使用物化视图的方式进行数据同步。
1.在DB1.COM建立一个新表,用于测试物化视图
create table hr.emp(id int primary key);
insert into hr.emp
select rownum from dual connect by rownum<=1000;
2.在DB1.COM 创建物化视图日志
CREATE MATERIALIZED VIEW LOG ON hr.emp WITH PRIMARY KEY INCLUDING NEW VALUES
3.在DB2.COM 创建物化视同步数据。
create materialized view hr.mv_emp build immediate refresh fast
enable query rewrite as select * from
[email protected];
DB2.COM查询数据:
SQL> select count(0) from hr.mv_emp;
COUNT(0)
----------
1000
DB1.COM 插入新数据
SQL> insert into hr.emp values(1001);
1 row created.
SQL> commit;
Commit complete.
SQL>
DB2.COM 刷新特化视图并查询数据。
SQL> execute dbms_mview.refresh('hr.mv_emp','f');
PL/SQL procedure successfully completed.
SQL> select count(0) from hr.mv_emp;
COUNT(0)
----------
1001
SQL>
看到数据已同步过来了。