在Oracle11g Streams測試Streams資料傳輸

mengzhaoliang發表於2010-12-09

描述:
源資料庫為兩臺叢集64位HP-UNIX的Oracle11gR2資料庫,
目標資料庫為1臺64位Linux的Oracle11gR2的資料庫,
把源資料庫的某個使用者的資料傳輸到目標資料庫中

1、源資料庫、目標資料庫均為歸檔模式

源:
SQL> archive log list;
Database log mode              Archive Mode
Automatic archival             Enabled
Archive destination            +ARCHDG
Oldest online log sequence     60
Next log sequence to archive   61
Current log sequence           61

目標:
SQL> archive log list;
Database log mode              No Archive Mode
Automatic archival             Disabled
Archive destination            USE_DB_RECOVERY_FILE_DEST
Oldest online log sequence     54
Current log sequence           56

SQL> startup mount
ORACLE instance started.

Total System Global Area 6747725824 bytes
Fixed Size                  2213976 bytes
Variable Size            4697622440 bytes
Database Buffers         2013265920 bytes
Redo Buffers               34623488 bytes
Database mounted.
SQL> alter database archivelog;

Database altered.

SQL> alter database open;

Database altered.

2、設定源、目標資料庫的相關引數
源:
SQL> show parameter global_names

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
global_names                         boolean     FALSE
SQL>
SQL> alter system set global_names=true scope=both;

System altered.

SQL> show parameter aq_tm_processes

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
aq_tm_processes                      integer     0

SQL> alter system set aq_tm_processes=2 scope=both;

System altered.

目標:
SQL> show parameter global_names

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
global_names                         boolean     FALSE
SQL>
SQL> alter system set global_names=true scope=both;

System altered.

SQL> show parameter aq_tm_processes

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
aq_tm_processes                      integer     0

SQL> alter system set aq_tm_processes=2 scope=both;

System altered.

3、在源資料庫啟用追加日誌
啟用輔助日誌
SQL> alter database add supplemental log data;

Database altered.


4、在源、目標資料庫建立表空間、使用者、授權
源:
檢視資料檔案位置:
select file_name from dba_data_files
建立表空間:
create tablespace streams_space
datafile '+DATADG/cboms/datafile/streams_space.dbf'
size 5M autoextend on
建立使用者:
create user strmadmin
identified by strmadmin
default tablespace streams_space
授予dba角色:
grant dba to strmadmin
授予流管理許可權:
exec dbms_streams_auth.grant_admin_privilege('strmadmin')    ----使用sys 或 system在命令列執行

目標:
檢視資料檔案位置:
select file_name from dba_data_files
建立表空間:
create tablespace streams_space
datafile '/u01/app/oracle/oradata/bhoms/streams_space.dbf'
size 5M autoextend on
建立使用者:
create user strmadmin
identified by strmadmin
default tablespace streams_space
授予dba角色:
grant dba to strmadmin
授予流管理許可權:
exec dbms_streams_auth.grant_admin_privilege('strmadmin')    ----使用sys 或 system在命令列執行

5、配置源、目標資料庫的tnsnames.ora
源資料庫節點1:
CBDBS01-> cd $ORACLE_HOME
CBDBS01-> pwd
/oracle/db/product/11.2.0/db_1
CBDBS01-> cd network/admin
CBDBS01-> vi tnsnames.ora
在檔案中末尾增加:

BHOMS_192.168.2.2 =                                                                   
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.2.2)(PORT = 1521))                     
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = bhoms)                                                          
    )
  )

測試連線到目標資料庫
CBDBS01-> sqlplus testUser /testPassword@BHOMS_192.168.2.2

源資料庫節點2:
源資料庫節點1:
CBDBS02-> cd $ORACLE_HOME
CBDBS02-> pwd
/oracle/db/product/11.2.0/db_1
CBDBS02-> cd network/admin
CBDBS02-> vi tnsnames.ora
在檔案中末尾增加:

BHOMS_192.168.2.2 =                                                                   
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.2.2)(PORT = 1521))                     
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = bhoms)                                                          
    )
  )

測試連線到目標資料庫
CBDBS02-> sqlplus testUser/testPassword@BHOMS_192.168.2.2


目標資料庫:
[oraoms@BZXXDBS01 ~]$ cd $ORACLE_HOME
[oraoms@BZXXDBS01 dbhome_1]$ cd network
[oraoms@BZXXDBS01 network]$ cd admin
[oraoms@BZXXDBS01 admin]$ pwd
/u01/app/oracle/product/11.2.0/dbhome_1/network/admin

在這目錄下沒有tnsnames.ora檔案,新建一個tnsnames.ora檔案,然後增加下面的內容:
[oraoms@BZXXDBS01 admin]$ vi tnsnames.ora

CBOMS =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.1)(PORT = 1568))
    (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.2)(PORT = 1568))
    (LOAD_BALANCE = yes)
    (FAILOVER = ON)
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = cboms)
      (FAILOVER_MODE =
        (TYPE = Select)
        (METHOD = BASIC)
      )
    )
  )


測試是否連線到源資料庫。
[oraoms@BZXXDBS01 admin]$ sqlplus testUser

6、在源、目標資料庫建立到目標資料庫的db_link
源:
SQL> conn strmadmin/strmadmin

SQL>create database link BHOMS connect to strmadmin identified by strmadmin using 'BHOMS_192.168.2.2';

測試:
select * from

目標:
SQL> conn strmadmin/strmadmin

SQL>create database link CBOMS connect to strmadmin identified by strmadmin using 'CBOMS';

測試:
select * from


7、需要傳輸testUser 使用者的資料,在源資料庫建立source佇列

CBDBS01-> sqlplus  strmadmin/strmadmin

SQL> BEGIN DBMS_STREAMS_ADM.SET_UP_QUEUE(
  2  queue_table=>'SOURCE_QUEUE_TABLE',
  3  queue_name=>'SOURCE_QUEUE',
  4  queue_user=>'strmadmin');
  5  END;
  6  /

PL/SQL procedure successfully completed.

或者:

SQL> EXEC DBMS_STREAMS_ADM.SET_UP_QUEUE();

PL/SQL procedure successfully completed.
該命令會建立一個佇列預設名:streams_queue,佇列表預設是:STREAMS_QUEUE_TABLE

佇列儲存的object型別是anaydata

移除佇列:
exec dbms_streams_adm.remove_queue(
queue_name => 'streams_queue',
cascade => true,
drop_unused_queue_table => true);

可以用查詢dba_queues,dba_queue_tables來檢查:

SQL>  select owner,queue_table,name from dba_queues where wner='STRMADMIN';

OWNER               QUEUE_TABLE              NAME

----------------- ------------------------- ------------------

STRMADMIN          SOURCES_QUEUE_TABLE        SOURCES_QUEUE

STRMADMIN          SOURCES_QUEUE_TABLE        AQ$_SOURCES_QUEUE_TABLE_E

 

SQL>select owner,queue_table,object_type from dba_queue_tables where wner='STRMADMIN';

OWNER            QUEUE_TABLE           OBJECT_TYPE

-------------- --------------------- ------------------

STRMADMIN       SOURCES_QUEUE_TABLE     SYS.ANYDATA

8、在目標資料庫建立接收佇列
[oraoms@BZXXDBS01 admin]$ sqlplus strmadmin/strmadmin

SQL*Plus: Release 11.2.0.1.0 Production on Thu Dec 9 16:16:20 2010

Copyright (c) 1982, 2009, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> BEGIN DBMS_STREAMS_ADM.SET_UP_QUEUE(
  2  queue_table=>'TARGET_QUEUE_TABLE',
  3  queue_name=>'TARGET_QUEUE',
  4  queue_user=>'strmadmin');
  5  END;
  6  /

PL/SQL procedure successfully completed.


9、在源資料庫建立capture程式
CBDBS01-> sqlplus

SQL> BEGIN DBMS_STREAMS_ADM.ADD_SCHEMA_RULES(
  2  schema_name=>'testUser ',
  3  streams_type=>'capture',
  4  streams_name=>'capture_stream',
  5  queue_name=>'strmadmin.SOURCE_QUEUE',
  6  include_dml=>true,
  7  include_ddl=>true,
  8  SOURCE_DATABASE=>'CBOMS',
  9  include_tagged_lcr=>false,
 10  inclusion_rule=>true);
 11  END;
 12  /

PL/SQL procedure successfully completed.

可以透過dba_capture檢視:

SQL> select CAPTURE_NAME,QUEUE_NAME,START_SCN,STATUS,CAPTURE_TYPE from dba_capture;

SQL> select * from  ALL_CAPTURE_PREPARED_SCHEMAS;

10、在源資料庫繼續建立傳播程式
CBDBS01-> sqlplus

SQL> BEGIN DBMS_STREAMS_ADM.ADD_SCHEMA_PROPAGATION_RULES(
  2  schema_name=>'testUser ',
  3  streams_name=>'source_to_target',
  4  source_queue_name=>'strmadmin.SOURCE_QUEUE',
  5  destination_queue_name=>'strmadmin.TARGET_QUEUE@BHOMS',
  6  include_dml=>true,
  7  include_ddl=>true,
  8  source_database=>'CBOMS',
  9  inclusion_rule=>true,
 10  queue_to_queue=>true);
 11  END;
 12  /

PL/SQL procedure successfully completed.

重新啟動propagation process
 檢視propagation process名稱
 select * from all_propagation;

可以透過dba_propagations檢視propagation是否啟動

SQL> select PROPAGATION_NAME,SOURCE_QUEUE_NAME,DESTINATION_QUEUE_NAME,DESTINATION_DBLINK,STATUS from dba_propagation
如果status不是“ENABLED”,而是“DISABLED”

可以啟動
SQL>exec dbms_propagation_adm.start_propagation('source_to_target');   (關閉為exec dbms_propagation_adm.stop_propagation('source_to_target');)


11、在目標資料庫建立apply程式
[oraoms@BZXXDBS01 admin]$ sqlplus strmadmin/strmadmin

SQL> BEGIN
  2  DBMS_STREAMS_ADM.ADD_SCHEMA_RULES(
  3  schema_name=>'testUser ',
  4  streams_type=>'apply',
  5  streams_name=>'target_apply_stream',
  6  queue_name=>'strmadmin.TARGET_QUEUE',
  7  include_dml=>true,
  8  include_ddl=>true,
  9  include_tagged_lcr=>false,
 10  source_database=>'CBOMS',
 11  inclusion_rule=>true);
 12  END;
 13  /

PL/SQL procedure successfully completed.

可以透過:

dba_apply

v$streams_apply_reader

v$streams_apply_coordinator

v$streams_apply_server

12、把源資料庫的使用者複製目標資料庫上
  可以用exp、imp或expdp、impdp把testUser 使用者資料遷移過去


13、直接設定SCN的方式進行例項化
源:
獲取源庫互置使用者的SCN
sqlplus
SQL> set serveroutput on
SQL> DECLARE
  2  iscn NUMBER;
  3  BEGIN
  4  iscn:=DBMS_FLASHBACK.GET_SYSTEM_CHANGE_NUMBER();
  5  DBMS_OUTPUT.PUT_LINE('Instantiation SCN is :'||iscn);
  6  END;
  7  /
Instantiation SCN is :5475315

PL/SQL procedure successfully completed.


設定為目標庫互置使用者的SCN

sqlplus
SQL> BEGIN
  2  DBMS_APPLY_ADM.SET_SCHEMA_INSTANTIATION_SCN(
  3  source_schema_name=>'testUser ',
  4  source_database_name=>'CBOMS',
  5  instantiation_scn=>&iscn);
  6  END;
  7  /
Enter value for iscn: 5475315
old   5: instantiation_scn=>&iscn);
new   5: instantiation_scn=>5475315);

PL/SQL procedure successfully completed.

14、在目標資料庫啟動Apply程式
SQL> BEGIN
  2  DBMS_APPLY_ADM.SET_PARAMETER(
  3  apply_name => 'target_apply_stream',
  4  parameter=>'disable_on_error',
  5  value=>'n');
  6  END;
  7  /

PL/SQL procedure successfully completed.


SQL> BEGIN
  2  DBMS_APPLY_ADM.START_APPLY(
  3  apply_name=>'target_apply_stream');
  4  END;
  5  /

PL/SQL procedure successfully completed.

#停止Apply程式

SQL>begin
dbms_apply_adm.stop_apply(
apply_name => 'target_apply_stream');
end;
/

 

檢視狀態

SQL> select apply_name,queue_name,status from dba_apply;

15、在源資料庫上啟動capture
SQL> BEGIN
  2  DBMS_CAPTURE_ADM.START_CAPTURE(
  3  capture_name=>'capture_stream');
  4  END;
  5  /

PL/SQL procedure successfully completed.

#停止Capture程式

begin
dbms_capture_adm.stop_capture(
capture_name => 'capture_stream');
end;
/

 

檢視狀態:

SQL> select capture_name,status from dba_capture;

15、測試
在源資料庫的bohomswas使用者建立一個表
create table test3(a int,b varchar2(64))
插入1條資料:
insert into test3 values(2,'bb');

在目標資料庫的testUser 使用者上如果有test3表和資料則成功。
最後測試像blob大物件檔案都可以順利傳輸。


 

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12778571/viewspace-681433/,如需轉載,請註明出處,否則將追究法律責任。

相關文章