Oracle stream案例分享

hurp_oracle發表於2015-01-18
一、STREAM環境準備:
This chapter contains these topics:
 Configuring a Streams Administrator
 Setting Initialization Parameters Relevant to Streams
 Configuring Network Connectivity and Database Links
1.建立stream管理使用者; ---或都建立一個單獨的使用者,或使用已存在的使用者並賦予相應的許可權 不能使用sys、system且不能使用system tablespace作為預設表空間
主:
CREATE TABLESPACE streams_tbs DATAFILE 'C:\ORACLE\PRODUCT\10.2.0\ORADATA\ORA10G\streams_tbs.dbf'  SIZE 25M REUSE AUTOEXTEND ON MAXSIZE UNLIMITED;
CREATE USER strmadmin IDENTIFIED BY strmadminpw DEFAULT TABLESPACE streams_tbs QUOTA UNLIMITED ON streams_tbs;
grant connect,resource,dba,aq_administrator_role to strmadmin; 

BEGIN
DBMS_STREAMS_AUTH.GRANT_ADMIN_PRIVILEGE(
grantee          => 'strmadmin',    
grant_privileges => true);
END;
/
備:
CREATE TABLESPACE streams_tbs DATAFILE '/oradata/shrnc/streams_tbs.dbf'  SIZE 25M REUSE AUTOEXTEND ON MAXSIZE UNLIMITED;
CREATE USER strmadmin IDENTIFIED BY strmadminpw DEFAULT TABLESPACE streams_tbs QUOTA UNLIMITED ON streams_tbs;
grant connect,resource,dba,aq_administrator_role to strmadmin; 
BEGIN
DBMS_STREAMS_AUTH.GRANT_ADMIN_PRIVILEGE(
grantee          => 'strmadmin',    
grant_privileges => true);
END;
/
2.設定初始化引數對stream的操作、安全、效能方面都非常重要,透過alter system或alter session設定合適的引數(結合官方文件modifialbe來判斷是否修改)
Sqlplus ‘/ as sysdba’ 
alter system set aq_tm_processes=2 scope=both; 
alter system set global_names=true scope=both; 
alter system set job_queue_processes=10 scope=both; 
alter system set parallel_max_servers=20 scope=both; 
alter system set undo_retention=3600 scope=both; 
alter system set nls_date_format='YYYY-MM-DD HH24:MI:SS' scope=spfile; 
alter system set streams_pool_size=25M scope=spfile; 
alter system set utl_file_dir='*' scope=spfile; 
alter system set open_links=4 scope=spfile;
執行完畢後重啟資料庫。
3.配合網路連線(TNS)和Dblink
3.1主資料庫(tnsnames.ora)中新增從資料庫的配置。
以下是引用片段:
#Oracle stream master node  
master =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.5)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = ora10g)
    )
  )
3.2配置從環境tnsnames.ora
以下是引用片段:
  從資料庫(tnsnames.ora)中新增主資料庫的配置。 
#Oracle stream salve node
slave =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.104)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = shsnc)
    )
  )
3.3 Dblink 建立
主:
conn strmadmin/strmadminpw
create database link shrnc connect to strmadmin identified by "strmadminpw" using 'slave';
備:
conn strmadmin/strmadminpw
create database link ora10g connect to strmadmin identified by "strmadminpw" using 'master';
配置資料庫去執行捕獲程式:
The following sections describe database requirements for running a Streams
capture process:
Configuring the Database to Run in ARCHIVELOG Mode
Specifying an Alternate Tablespace for LogMiner
In addition to these tasks, make sure the initialization parameters are set properly
on any database that will run a capture process.
1.將資料庫置為歸檔模式
設定log_archive_dest_1到相應的位置;設定log_archive_start為TRUE,即啟用自動歸檔功能;設定log_archive_format指定歸檔日誌的命令格式。
舉例:
以下是引用片段:
sqlplus '/ as sysdba' 
alter system set log_archive_dest_1='location=C:\ORACLE\PRODUCT\10.2.0\ORADATA\ORA10G' scope=spfile; 
alter system set log_archive_start=TRUE scope=spfile; 
alter system set log_archive_format='arch%t_%s_%r.arc' scope=spfile; 
shutdown immediate; 
startup mount; 
alter database archivelog; 
alter database open;
資料庫置為歸檔模式後,可以按如下方式檢驗一下:
以下是引用片段:
SQL> archive log list 
2.指定一個表空間為logminer
預設logminer表在system表空間上,但system表空間可能沒有足夠的空間;
execute dbms_logmnr_d.set_tablespace('streams_tbs'); 
二、建立Capture程式
(如果增加要配置的表,直接在STRMADMIN.TMP_STREAM_TABLES中新增即可,並且批次指令碼的執行需要在中心庫執行)
以下是引用片段:
#以strmadmin身份,登入主資料庫。提醒一下,本文件以表hurp.test表做示例。 
connect strmadmin/strmadminpw 
SQL> create table TMP_STREAM_TABLES(owner varchar2(20),table_name varchar2(30));
表已建立。             
SQL> insert into TMP_STREAM_TABLES values('HURP','TEST');                       
已建立 1 行。          
SQL> commit;                                                                    
提交完成。
(如果增加要配置的表,直接在STRMADMIN.TMP_STREAM_TABLES中新增即可,並且批次指令碼的執行需要在中心庫執行)
SELECT 'begin dbms_streams_adm.add_table_rules(table_name => '''||OWNER||'.'||
       TABLE_NAME || ''',' || 'streams_type => ''capture'',' ||
       'streams_name => ''CAPTURE_CENDB1'',' ||
       'queue_name => ''slave_queue'',' ||
       'include_dml => true,' || 'include_ddl => true,' ||
       'inclusion_rule => true); end; /'
  FROM TMP_STREAM_TABLES;

begin 
dbms_streams_adm.add_table_rules(
table_name => 'HURP.TEST',
streams_type => 'capture',
streams_name => 'capture_master',
queue_name => 'strmadmin.master_queue',
include_dml => true,
include_ddl => true,
inclusion_rule => true); 
end;
/
begin 
dbms_streams_adm.remove_table(
streams_type => 'capture',
streams_name => 'CAPTURE_MASTER');
end;
/
三、例項化複製資料庫
在主資料庫環境中,執行如下Shell語句。如果從庫的admin使用者不存在,建立一個admin的空使用者。
exp  file=C:\ORACLE\PRODUCT\10.2.0\ORADATA\ORA10G\test.dmp object_consistent=y rows=y tables=hurp.test
imp file=/oradata/arch/test.dmp  ignore=y commit=y  streams_instantiation=y  fromuser=hurp touser=hurp

三、管理Staging和Propagation
This chapter contains these topics:
n Managing Streams Queues
n Managing Streams Propagations and Propagation Jobs
n Managing a Streams Messaging Environment
建立流佇列:
1.建立Master流佇列
以下是引用片段:、
#以strmadmin身份,登入主資料庫。 
connect strmadmin/strmadminpw 
begin 
dbms_streams_adm.set_up_queue( 
queue_table => 'master_queue_table', 
queue_name => 'master_queue'); 
end; 
/
begin
dbms_streams_adm.REMOVE_QUEUE( 
queue_name => 'slave_queue'); 
end; 
/
2.建立slave流佇列
以下是引用片段:
#以strmadmin身份,登入從資料庫。 
connect strmadmin/strmadminpw 
begin 
dbms_streams_adm.set_up_queue( 
queue_table => 'slave_queue_table', 
queue_name => 'slave_queue'); 
end; 
/


begin
dbms_streams_adm.REMOVE_QUEUE( 
queue_name => 'slave_queue'); 
end; 
/
建立傳播程式:
以下是引用片段:
connect strmadmin/strmadminpw 
select 'begin dbms_streams_adm.add_table_propagation_rules(table_name => '''||OWNER||'.' || table_name || ''',' ||
        'streams_name => ''CAPTURE_MASTER'',' ||
        'source_queue_name => ''strmadmin.master_queue'',' ||
        'destination_queue_name => ''strmadmin.slave_queue'',' ||
        'include_dml => true,' || 'include_ddl => true,' ||
        'source_database => ''ora10g'',' || 'inclusion_rule => true,' ||
        'queue_to_queue => true); end; /'
  FROM TMP_STREAM_TABLES;
  
begin 
dbms_streams_adm.add_table_propagation_rules(
table_name => 'HURP.TEST',
streams_name => 'propagation_master',
source_queue_name => 'strmadmin.master_queue',
destination_queue_name => 'strmadmin.slave_queue@shrnc',
include_dml => true,
include_ddl => true,
source_database => 'ora10g',
inclusion_rule => true,
queue_to_queue => true);
end;
/  


BEGIN
DBMS_PROPAGATION_ADM.DROP_PROPAGATION(
propagation_name => 'CAPTURE_MASTER');
END;
/

#修改propagation休眠時間為0,表示實時傳播LCR。 
begin 
dbms_aqadm.alter_propagation_schedule( 
queue_name => 'prod_queue', 
destination => 'shrnc', 
latency => 0); 
end; 
/
第 1 行出現錯誤:                           
ORA-24042: 不存在 QUEUE STRMADMIN.MASTER_QUEUE 及 DESTINATION SHRNC 的傳播日程
ORA-06512: 在 "SYS.DBMS_SYS_ERROR", line 95                                                                                                                     
ORA-06512: 在 "SYS.DBMS_PRVTAQIP", line 957 
ORA-06512: 在 "SYS.DBMS_AQADM", line 910                                                                                                                          
ORA-06512: 在 line 2      
注:跳過以上報錯,繼續以下可以正常進行,後續查詢相關資料,執行以下PL/SQL可以
begin                                       
dbms_aqadm.alter_propagation_schedule(      
queue_name => 'strmadmin.master_queue',                 
destination => 'shrnc',                    
destination_queue => 'strmadmin.slave_queue',        
latency => 0);                              
end;                                        
/                                           
建立應用程式:
以下是引用片段:
#以strmadmin身份,登入從資料庫。 
connect strmadmin/strmadminpw 
從庫命令視窗執行應用程式
select 'begin dbms_streams_adm.add_table_rules(table_name => '''||OWNER||'.' || table_name || ''',' ||
        'streams_type => ''apply'',' || 'streams_name => ''APPLY_SLAVE'',' ||
        'queue_name => ''strmadmin.slave_queue'',' ||
        'include_dml => true,' || 'include_ddl => true,' ||
        'include_tagged_lcr => false,' || 'source_database => ''ora10g'',' ||
        'inclusion_rule => true); end; /'
  FROM TMP_STREAM_TABLES ;

begin 
dbms_streams_adm.add_table_rules(
table_name => 'HURP.TEST',
streams_type =>'apply',
streams_name => 'APPLY_SLAVE',
queue_name => 'strmadmin.slave_queue',
include_dml => true,
include_ddl => true,
include_tagged_lcr => false,
source_database => 'ora10g',
inclusion_rule => true);
end;
/
Exec dbms_apply_adm.set_parameter(apply_name => 'APPLY_SLAVE',parameter => 'disable_on_error',value => 'n');
啟停STREAM:
啟動STREAM
以下是引用片段:
#以strmadmin身份,登入從資料庫。 
connect strmadmin/strmadminpw
#啟動Apply程式 
begin 
dbms_apply_adm.start_apply( 
apply_name => 'APPLY_SLAVE'); 
end; 

#以strmadmin身份,登入主資料庫。 
connect strmadmin/strmadminpw
#啟動Capture程式 
begin 
dbms_capture_adm.start_capture( 
capture_name => 'capture_master'); 
end; 
/
exec dbms_propagation_adm.start_propagation(propagation_name =>'propagation_master');
停止STREAM
以下是引用片段:
#以strmadmin身份,登入從資料庫。 
connect strmadmin/strmadminpw
#啟動Apply程式 
begin 
dbms_apply_adm.stop_apply( 
apply_name => 'APPLY_SLAVE'); 
end; 


#以strmadmin身份,登入主資料庫。 
connect strmadmin/strmadminpw
#啟動Capture程式 
begin 
dbms_capture_adm.stop_capture( 
capture_name => 'CAPTURE_MASTER'); 
end; 
/
問題診斷:
SELECT apply_name,queue_name,apply_captured,status,error_number,error_message FROM dba_apply@shrnc;
SELECT local_transaction_id,apply_name,queue_name,error_number,error_message,error_creation_time FROM dba_apply_error@shrnc ORDER BY error_creation_time DESC;

--以 strmadmin身份,登入從資料庫,執行如下語句:
--主庫執行
SELECT capture_name,status,queue_name,start_scn,captured_scn,applied_scn FROM dba_capture;
SELECT propagation_name,status,queue_to_queue from dba_propagation;
SELECT capture_name,state,capture_message_number,capture_message_create_time FROM v$streams_capture;


select sid,capture_name,startup_time,CAPTURE_TIME,CAPTURE_MESSAGE_NUMBER,state from v$streams_capture;


select sid,state,APPLY_NAME,TOTAL_RECEIVED,TOTAL_IGNORED,TOTAL_ERRORS from V$STREAMS_APPLY_COORDINATOR;


select streams_name,streams_type,CUMULATIVE_MESSAGE_COUNT,TOTAL_MESSAGE_COUNT from v$streams_transaction;
select PROPAGATION_NAME,SOURCE_QUEUE_OWNER,SOURCE_QUEUE_NAME,DESTINATION_QUEUE_OWNER,DESTINATION_QUEUE_NAME,DESTINATION_DBLINK,NEGATIVE_RULE_SET_OWNER,QUEUE_TO_QUEUE from dba_propagation;


批次插入指令碼:
create or replace procedure batch_insert is 
v_sql02 varchar2(3999);
v_value number;
begin
for i in 1..10000 loop 
v_value:=i;
v_sql02:='insert into hurp.test values(:i,''a''||:i)';
execute immediate v_sql02 using v_value,v_value;   
commit;                                            
end loop;
end;
/




重新配置需要執行下列操作
從庫執行:
要清除 Stream配置資訊,需要先停止Stream程式。
#以strmadmin身份,登入主資料庫。
--主庫清理捕獲程式
exec dbms_capture_adm.stop_capture(capture_name => 'CAPTURE_MASTER');
exec dbms_capture_adm.drop_capture(capture_name => 'CAPTURE_MASTER');


--主庫清理分發程式
exec dbms_propagation_adm.stop_propagation(propagation_name =>  'propagation_master',force=>true);
exec dbms_propagation_adm.drop_propagation(propagation_name => 'propagation_master');


--主庫清理佇列
exec dbms_streams_adm.remove_queue(queue_name=>'master_queue');


--主庫清理配置
exec dbms_streams_adm.remove_streams_configuration();
--從庫停止應用程式
exec dbms_apply_adm.stop_apply(apply_name => 'APPLY_SLAVE');


--清理錯誤資訊
exec dbms_apply_adm.delete_all_errors(apply_name => 'APPLY_SLAVE');


--從庫清理應用程式
exec dbms_apply_adm.drop_apply(apply_name => 'APPLY_SLAVE');


--從庫清理佇列
exec dbms_streams_adm.remove_queue(queue_name=>'slave_queue');


--清除配置
exec dbms_streams_adm.remove_streams_configuration();
--各個庫執行
truncate table sys.STREAMS$_APPLY_MILESTONE;
truncate table sys.STREAMS$_APPLY_PROCESS;
truncate table sys.STREAMS$_APPLY_PROGRESS;
truncate table sys.STREAMS$_APPLY_SPILL_MESSAGES;
truncate table sys.STREAMS$_APPLY_SPILL_MSGS_PART;
truncate table sys.STREAMS$_APPLY_SPILL_TXN;
truncate table sys.STREAMS$_APPLY_SPILL_TXN_LIST;
truncate table sys.STREAMS$_CAPTURE_PROCESS;
truncate table sys.STREAMS$_DEF_PROC;
truncate table sys.STREAMS$_DEST_OBJS;
truncate table sys.STREAMS$_DEST_OBJ_COLS;
truncate table sys.STREAMS$_EXTRA_ATTRS;
truncate table sys.STREAMS$_INTERNAL_TRANSFORM;
truncate table sys.STREAMS$_KEY_COLUMNS;
truncate table sys.STREAMS$_MESSAGE_CONSUMERS;
truncate table sys.STREAMS$_MESSAGE_RULES;
truncate table sys.STREAMS$_PREPARE_DDL;
truncate table sys.STREAMS$_PREPARE_OBJECT;
truncate table sys.STREAMS$_PRIVILEGED_USER;
truncate table sys.STREAMS$_PROCESS_PARAMS;
truncate table sys.STREAMS$_PROPAGATION_PROCESS;
truncate table sys.STREAMS$_RULES;
truncate table system.LOGMNRC_DBNAME_UID_MAP;
truncate table system.LOGMNRC_GSII;
truncate table system.LOGMNRC_GTCS;
truncate table system.LOGMNRC_GTLO;
truncate table system.LOGMNRP_CTAS_PART_MAP;
truncate table system.LOGMNRT_MDDL$;
truncate table system.LOGMNR_AGE_SPILL$;
truncate table system.LOGMNR_ATTRCOL$;
truncate table system.LOGMNR_ATTRIBUTE$;
truncate table system.LOGMNR_CCOL$;
truncate table system.LOGMNR_CDEF$;
truncate table system.LOGMNR_COL$;
truncate table system.LOGMNR_COLTYPE$;
truncate table system.LOGMNR_DICTIONARY$;
truncate table system.LOGMNR_DICTSTATE$;
truncate table system.LOGMNR_ERROR$;
truncate table system.LOGMNR_FILTER$;
truncate table system.LOGMNR_HEADER1$;
truncate table system.LOGMNR_HEADER2$;
truncate table system.LOGMNR_ICOL$;
truncate table system.LOGMNR_IND$;
truncate table system.LOGMNR_INDCOMPART$;
truncate table system.LOGMNR_INDPART$;
truncate table system.LOGMNR_INDSUBPART$;
truncate table system.LOGMNR_LOB$;
truncate table system.LOGMNR_LOBFRAG$;
truncate table system.LOGMNR_LOG$;
truncate table system.LOGMNR_OBJ$;
truncate table system.LOGMNR_PARAMETER$;
truncate table system.LOGMNR_PROCESSED_LOG$;
truncate table system.LOGMNR_RESTART_CKPT$;
truncate table system.LOGMNR_RESTART_CKPT_TXINFO$;
truncate table system.LOGMNR_SESSION$;
truncate table system.LOGMNR_SESSION_EVOLVE$;
truncate table system.LOGMNR_SPILL$;
truncate table system.LOGMNR_TAB$;
truncate table system.LOGMNR_TABCOMPART$;
truncate table system.LOGMNR_TABPART$;
truncate table system.LOGMNR_TABSUBPART$;
truncate table system.LOGMNR_TS$;
truncate table system.LOGMNR_TYPE$;
truncate table system.LOGMNR_UID$;
truncate table system.LOGMNR_USER$;






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

相關文章