oracle stream testing : test ---> standby
环境:
OS:Redhat Linux
Oracle 11.2.0.1
source:TEST
destnation:STANDBY
==准备==
1.归档日志配置:
SQL> archive log list;
Database log mode Archive Mode
Automatic archival Enabled
Archive destination /home/oracle/archivelog
Database log mode Archive Mode
Automatic archival Enabled
Archive destination /home/oracle/archivelog
---以下没有专门说明的都是sys用户---
2.在源stream1上配置Supplemental loging:
在源上:
SQL> alter database add supplemental log data;
Database altered.
3.两台机器上修改global_name:
stream1:
SQL> alter system set global_names=true scope=both;
SQL> alter database rename global_name to TEST.com;
stream2:
SQL> alter system set global_names=true scope=both;
SQL> alter database rename global_name to STANDBY.com;
4.修改初始化参数:
参考官方文档确定需要来检查和设定参数
----------ref 01----------------------------------------
在主/从数据库设置初始化参数(在主从数据库都执行以下操作)
1) sqlplus / as sysdba ----以dba身份登录
2)alter system set aq_tm_processes=2 scope=both; ---启用对队列消息的时间监视
alter system set global_names=true scope=both; ---设置全局名称为true
--alter system set job_queue_processes=10 scope=both; --设置定时作业进程最大为10个,默认是10
alter system set undo_retention=3600 scope=both; --设置回滚段时间,默认是900
alter system set streams_pool_size=25M scope=spfile; --sga设置为自动调整情况下不需设置该参
--------------ref 02-----------------------------------------
源数据库和目的数据库均需要设置的参数:
alter system set global_names=true scope = both;
默认为false, Database Link 使用的是数据库的global_name。
alter system set aq_tm_processes=2 scope=both;
以下参数都是10G的默认值, 检查下就可以了. 无需设置
如:Show parameter job_queue_processes
alter system set job_queue_processes = 10 scope=both;
alter system set sga_target = 300m scope=spfile;
alter system set open_links=4 scope=spfile;
alter system set statistics_level='TYPICAL' scope=both;
10g 默认为Typical, 性能统计模式
alter system set logmnr_max_persistent_sessions=1 scope=spfile;
10g 默认为1,持久的日志挖掘会话数。
alter system set "_job_queue_interval"=1 scope=spfile;
alter system set aq_tm_processes=1;
alter system set streams_pool_size=200m scope=both;
注意streams_pool_size一定要够大,因为如果启用了SGA_TARGET,ORACLE可能分配很少内存给stream导致大量信息被spill到磁盘导致查询DBA_APPLY,DBA_CAPTURE,DBA_PROPGATION全部状态ENABLED但就是没有数据被同步。
同时设置_job_queue_interval也是为了提高队列检查时间,防止apply出问题。
注意:与复制有关的2个参数:
如果等了足够长的时间发现数据没有复制过来,仔细检查了capture/propagation/apply各进程的状态都是正常的, 并检查参数.
alter system set "_job_queue_interval"=1 scope=spfile;
并且将aq_tm_processes参数改为1(我原来这是为10)
alter system set aq_tm_processes=1;
改完后重启,发现数据就可以去了。这个隐含参数只是控制对job队列的检查频率,默认5秒。
5.创建stream管理用户并表空间,配置权限,source和dest:
5.1 为strmadmin用户创建独立表空间
TEST:
SQL> CREATE TABLESPACE streams_tbs DATAFILE '/oracle/oradata/TEST/streams_tbs.dbf' SIZE 25M REUSE AUTOEXTEND ON MAXSIZE UNLIMITED;
Tablespace created.
STANDBY:
SQL> CREATE TABLESPACE streams_tbs DATAFILE '/oracle/oradata/STANDBY/streams_tbs.dbf' SIZE 25M REUSE AUTOEXTEND ON MAXSIZE UNLIMITED;
Tablespace created.
5.2 source和dest创建相同用户和权限:
SQL> CREATE USER strmadmin IDENTIFIED BY strmadminpw
DEFAULT TABLESPACE streams_tbs
QUOTA UNLIMITED ON streams_tbs;
User created.
SQL> GRANT DBA TO strmadmin;
Grant succeeded.
SQL>
BEGIN
DBMS_STREAMS_AUTH.GRANT_ADMIN_PRIVILEGE(
grantee => 'strmadmin',
grant_privileges => true);
END;
/
PL/SQL procedure successfully completed.
6.配置listener.ora和tnsnames.ora保证:
TEST 上:sqlplus strmadmin/strmadminpw@STANDBY
STANDBY 上:sqlplus strmadmin/strmadminpw@TEST
能够连通
---如果没有说明以下都是strmadmin/strmadminpw 用户---
7.创建DB link:
单向复制之需要一个database link即source 到dest:TEST-->STANDBY
TEST上:
sqlplus strmadmin/strmadminpw
SQL> CREATE DATABASE LINK STANDBY.com CONNECT TO strmadmin IDENTIFIED BY strmadminpw USING 'STANDBY';
Database link created.
测试:
SQL> select * from [email protected];
GLOBAL_NAME
-----------------------------------------
STANDBY.com
==开始stream的配置===
8. source和dest 创建队列:
TEST,STANDBY都要创建
SQL> EXEC DBMS_STREAMS_ADM.SET_UP_QUEUE();
PL/SQL procedure successfully completed.
上面命令会创建一个队列缺省名:streams_queue,队列表缺省是:STREAMS_QUEUE_TABLE
队列存储的object类型是anaydata
可以用查询dba_queues,dba_queue_tables来检查:
SQL> select owner,queue_table,name from dba_queues where owner='STRMADMIN';
OWNER QUEUE_TABLE NAME
---------------- ---------------------------------- ---------------------------------------------
STRMADMIN STREAMS_QUEUE_TABLE STREAMS_QUEUE
STRMADMIN STREAMS_QUEUE_TABLE AQ$_STREAMS_QUEUE_TABLE_E
SQL> select owner,queue_table,object_type from dba_queue_tables where owner='STRMADMIN';
OWNER QUEUE_TABLE OBJECT_TYPE
----------------- --------------------------------- -------------------------------
STRMADMIN STREAMS_QUEUE_TABLE SYS.ANYDATA
9. 在source: TEST上创建Stream propagation:
SQL>
BEGIN
DBMS_STREAMS_ADM.ADD_SCHEMA_PROPAGATION_RULES(
schema_name => 'scott',
streams_name => 'TEST_to_STANDBY',
source_queue_name => 'strmadmin.streams_queue',
destination_queue_name => '[email protected]',
include_dml => true,
include_ddl => true,
source_database => 'TEST.com',
inclusion_rule => true,
queue_to_queue => true);
END;
/
PL/SQL procedure successfully completed.
可以通过dba_propagations查看结果:
SQL> select PROPAGATION_NAME,SOURCE_QUEUE_NAME,DESTINATION_QUEUE_NAME,DESTINATION_DBLINK,STATUS from dba_propagation;
PROPAGATION_NAME SOURCE_QUEUE_NAME DESTINATION_QUEUE_NAME DESTINATION_DBL STATUS
-------------------------------- ------------------------------ --------------------------------- --------------- --------
STREAM1_TO_STREAM2 STREAMS_QUEUE STREAMS_QUEUE STANDBY.COM ENABLED
10. 在source: TEST上创建Capture进程:
SQL>
BEGIN
DBMS_STREAMS_ADM.ADD_SCHEMA_RULES(
schema_name => 'scott',
streams_type => 'capture',
streams_name => 'capture_TEST',
queue_name => 'strmadmin.streams_queue',
include_dml => true,
include_ddl => true,
inclusion_rule => true);
END;
/
PL/SQL procedure successfully completed.
可将hr改为scott
可以通过dba_capture查看:
SQL> select CAPTURE_NAME,QUEUE_NAME,START_SCN,STATUS,CAPTURE_TYPE from dba_capture;
CAPTURE_NAME QUEUE_NAME START_SCN STATUS CAPTURE_TY
---------------------------- -------------------------- --------------- ------------ ----------
CAPTURE_STREAM1 STREAMS_QUEUE 504733 DISABLED LOCAL
SQL> select * from ALL_CAPTURE_PREPARED_SCHEMAS;
SCHEMA_NAME TIMESTAMP SUPPLEME SUPPLEME SUPPLEME SUPPLEME
-------------------- --------------- -------------- -------------- ------------- --------
HR 12-JUN-08 IMPLICIT IMPLICIT IMPLICIT NO
11.将TEST上的scott schema数据导到STANDBY上:
在stream2上:
可先将scott用户删除 drop user scott cascade;
sqlplus strmadmin/strmadminpw
SQL> create public database link TEST.com connect to strmadmin identified by strmadminpw using 'TEST';
Database link created.
SQL> select * from [email protected];
GLOBAL_NAME
------------------------------------------------------
TEST
导数据:
[oracle@localhost admin]$ impdp strmadmin/strmadminpw network_link=TEST.com schemas=scott
Import: Release 10.2.0.2.0 - Production on Thursday, 12 June, 2008 17:47:59
Copyright (c) 2003, 2005, Oracle. All rights reserved.
.....
......
Processing object type SCHEMA_EXPORT/TABLE/STATISTICS/TABLE_STATISTICS
Job "SYSTEM"."SYS_IMPORT_SCHEMA_01" successfully completed at 17:50:32
遇到的问题:在导数据时发现导完后两边的表不一致,后发现在STANDBY从库上有scott用户及相关联的表,可使用命令drop user scott cascade 删除scott用户,再进行导数据即可解决此问题。
12. 在TEST上设置STANDBY上scott schema的Instantiation SCN
sqlplus strmadmin/strmadminpw
SQL>
DECLARE
iscn NUMBER;
BEGIN
iscn := DBMS_FLASHBACK.GET_SYSTEM_CHANGE_NUMBER();
[email protected](
source_schema_name => 'scott',
source_database_name => 'TEST.com',
instantiation_scn => iscn,
recursive => true);
END;
/
PL/SQL procedure successfully completed.
13.在STANDBY上创建apply进程apply_STANDBY:
SQL>
BEGIN
DBMS_STREAMS_ADM.ADD_SCHEMA_RULES(
schema_name => 'scott',
streams_type => 'apply',
streams_name => 'apply_STANDBY',
queue_name => 'strmadmin.streams_queue',
include_dml => true,
include_ddl => true,
source_database => 'TEST.com',
inclusion_rule => true);
END;
/
PL/SQL procedure successfully completed.
可以通过:
dba_apply
v$streams_apply_reader
v$streams_apply_coordinator
v$streams_apply_server
查看状态
SQL> select apply_name,queue_name,status from dba_apply;
APPLY_NAME QUEUE_NAME STATUS
--------------------- ------------------------ --------
APPLY_STREAM2 STREAMS_QUEUE DISABLED
==启动==
13.启动capture和apply:
13.1 STANDBY上启动 Apply Process
CONNECT strmadmin/strmadminpw
SQL>
BEGIN
DBMS_APPLY_ADM.SET_PARAMETER(
apply_name => 'apply_STANDBY',
parameter => 'disable_on_error',
value => 'n');
END;
/
PL/SQL procedure successfully completed.
SQL>
BEGIN
DBMS_APPLY_ADM.START_APPLY(
apply_name => 'apply_STANDBY');
END;
/
PL/SQL procedure successfully completed.
SQL> select apply_name,queue_name,status from dba_apply;
APPLY_NAME QUEUE_NAME STATUS
------------------------------ -------------------------- --------
APPLY_STREAM2 STREAMS_QUEUE ENABLED
这时候alert log有:
Thu Jun 12 18:00:36 2008
Streams APPLY A001 started with pid=25, OS id=30819
Streams Apply Reader started P000 with pid=26 OS id=30821
Streams Apply Server started P001 with pid=27 OS id=30823
如果有问题,没有能启动就查dba_apply的error_messages列
13.2 TEST上启动capture process:
sqlplus strmadmin/strmadminpw
SQL>
BEGIN
DBMS_CAPTURE_ADM.START_CAPTURE(
capture_name => 'capture_TEST');
END;
/
PL/SQL procedure successfully completed.
SQL> select capture_name,status from dba_capture;
CAPTURE_NAME STATUS
------------------------------ ------------
CAPTURE_STREAM1 ENABLED
alert 日志有:
Thu Jun 12 18:04:46 2008
Streams CAPTURE C001 started with pid=27, OS id=11884
Thu Jun 12 18:04:49 2008
LOGMINER: Parameters summary for session# = 1
LOGMINER: Number of processes = 3, Transaction Chunk Size = 1
LOGMINER: Memory Size = 10M, Checkpoint interval = 10M
LOGMINER: session# = 1, builder process P001 started with pid=32 OS id=11966
LOGMINER: session# = 1, reader process P000 started with pid=29 OS id=11964
LOGMINER: session# = 1, preparer process P002 started with pid=33 OS id=11968
Thu Jun 12 18:04:53 2008
LOGMINER: Begin mining logfile: /u01/oradata/stream1_arch/1_13_657197948.dbf
Thu Jun 12 18:06:40 2008
Thread 1 advanced to log sequence 15
Current log# 2 seq# 15 mem# 0: /u01/oradata/stream1/redo02.log
Thu Jun 12 18:06:53 2008
Thread 1 advanced to log sequence 16
Current log# 3 seq# 16 mem# 0: /u01/oradata/stream1/redo03.log
...
LOGMINER: Begin mining logfile: /u01/oradata/stream1_arch/1_13_657197948.dbf
Thu Jun 12 18:07:34 2008
LOGMINER: End mining logfile: /u01/oradata/stream1_arch/1_13_657197948.dbf
Thu Jun 12 18:07:34 2008
LOGMINER: Begin mining logfile: /u01/oradata/stream1_arch/1_14_657197948.dbf
信息,说明已经开始mining logfile.
===测试===
14.测试
在TEST上:
SQL> conn scott/tiger
select * from scott.emp;
update scott.emp set sal=sal+1 where empno=7369;
SQL> commit;
Commit complete.
STANDBY上检查结果(可能有延迟):
SQL> conn scott/tiger
select * from scott.emp;
----------------- ----------
197 3001
这个时间间隔跟性能及其它情况有关,包括
capture mining logfile的速度
propagation的间隔(默认3秒)及传输时间
apply log的速度
问题诊断
5.1 如何知道捕捉(Capture)进程是否运行正常?
以strmadmin身份,登录主数据库,执行如下语句:
SQL>
SELECT CAPTURE_NAME, QUEUE_NAME, RULE_SET_NAME,NEGATIVE_RULE_SET_NAME,STATUS FROM DBA_CAPTURE;
结果显示如下:
CAPTURE_NAME QUEUE_NAME
------------------------------ ------------------------------
RULE_SET_NAME NEGATIVE_RULE_SET_NAME STATUS
------------------------------ ------------------------------ --------
CAPTURE_PROD PROD_QUEUE
RULESET$_14 ENABLED
ENABLED
如果STATUS状态是ENABLED,表示Capture进程运行正常;
如果STATUS状态是DISABLED,表示Capture进程处于停止状态,只需重新启动即可;
如果STATUS状态是ABORTED,表示Capture进程非正常停止,查询相应的ERROR_NUMBER、ERROR_MESSAGE列可以得到详细的信息;同时,Oracle会在跟踪文件中记录该信息。
5.2 如何知道Captured LCR是否有传播GAP?
以strmadmin身份,登录主数据库,执行如下语句:
SQL> SELECT CAPTURE_NAME, QUEUE_NAME, STATUS, CAPTURED_SCN, APPLIED_SCN
2 FROM DBA_CAPTURE;
结果显示如下:
CAPTURE_NAME QUEUE_NAME STATUS
------------------------------ ------------------------------ --------
CAPTURED_SCN APPLIED_SCN
------------ -----------
CAPTURE_PROD PROD_QUEUE ENABLED
17023672 17023672
如果APPLIED_SCN小于CAPTURED_SCN,则表示在主数据库一端,要么LCR没有被dequeue,要么Propagation进程尚未传播到从数据库一端。
5.3 如何知道Appy进程是否运行正常?
以strmadmin身份,登录从数据库,执行如下语句:
SQL> SELECT apply_name, apply_captured, status FROM dba_apply;
结果显示如下:
APPLY_NAME APPLY_ STATUS
---------------------- ------ ----------------
APPLY_H10G YES ENABLED
如果STATUS状态是ENABLED,表示Apply进程运行正常;
如果STATUS状态是DISABLED,表示Apply进程处于停止状态,只需重新启动即可;
如果STATUS状态是ABORTED,表示Apply进程非正常停止,查询相应的ERROR_NUMBER、ERROR_MESSAGE列可以得到详细的信息;同时,可以查询DBA_APPLY_ERROR视图,了解详细的Apply错误信息。
参考文档: oracle_streams_b28322.pdf
![](file:/C:/Users/BO9E44~1.YUA/AppData/Local/Temp/enhtmlclip/EN20d8a6e5.png)
==================反向复制 从库-->主库 =========================
环境:
OS:Redhat Linux
Oracle 11.2.0.1
source:TEST
destnation:STANDBY
OS:Redhat Linux
Oracle 11.2.0.1
source:TEST
destnation:STANDBY
1. 在从库standby 上创建 capture
以
sqlplus strmadmin/strmadminpw@STANDBY连上从库
SQL>
BEGIN
DBMS_STREAMS_ADM.ADD_SCHEMA_RULES(
schema_name => 'scott',
streams_type => 'capture',
streams_name => 'capture_STANDBY',
queue_name => 'strmadmin.streams_queue',
include_dml => true,
include_ddl => true,
inclusion_rule => true);
END;
/
BEGIN
DBMS_STREAMS_ADM.ADD_SCHEMA_RULES(
schema_name => 'scott',
streams_type => 'capture',
streams_name => 'capture_STANDBY',
queue_name => 'strmadmin.streams_queue',
include_dml => true,
include_ddl => true,
inclusion_rule => true);
END;
/
2. 在从库
STANDBY
上创建Stream propagation:
SQL>
BEGIN
DBMS_STREAMS_ADM.ADD_SCHEMA_PROPAGATION_RULES(
schema_name => 'scott',
streams_name => 'STANDBY_to_TEST',
BEGIN
DBMS_STREAMS_ADM.ADD_SCHEMA_PROPAGATION_RULES(
schema_name => 'scott',
streams_name => 'STANDBY_to_TEST',
source_queue_name => 'strmadmin.streams_queue',
destination_queue_name => '[email protected]',
include_dml => true,
include_ddl => true,
source_database => ' STANDBY .com',
destination_queue_name => '[email protected]',
include_dml => true,
include_ddl => true,
source_database => ' STANDBY .com',
inclusion_rule => true,
queue_to_queue => true);
END;
/
queue_to_queue => true);
END;
/
![](file:/C:/Users/BO9E44~1.YUA/AppData/Local/Temp/enhtmlclip/Image.png)
3. 在主库TEST上创建apply进程apply_TEST:
以sqlplus strmadmin/strmadminpw@TEST连上主库
SQL>
BEGIN
DBMS_STREAMS_ADM.ADD_SCHEMA_RULES(
schema_name => 'scott',
streams_type => 'apply',
streams_name => 'apply_TEST',
queue_name => 'strmadmin.streams_queue',
include_dml => true,
include_ddl => true,
source_database => 'STANDBY.com',
inclusion_rule => true);
END;
/
BEGIN
DBMS_STREAMS_ADM.ADD_SCHEMA_RULES(
schema_name => 'scott',
streams_type => 'apply',
streams_name => 'apply_TEST',
queue_name => 'strmadmin.streams_queue',
include_dml => true,
include_ddl => true,
source_database => 'STANDBY.com',
inclusion_rule => true);
END;
/
4. 在master数据库中为应用进程设置实例化SCN
-- at the primary(TEST)
DECLARE
iscn NUMBER;
BEGIN
iscn := DBMS_FLASHBACK.GET_SYSTEM_CHANGE_NUMBER();
[email protected](
source_schema_name => 'scott',
source_database_name => 'STANDBY.com',
instantiation_scn => iscn,
recursive => true);
END;
/
5. 在standby 库上启用 capture 进程
sqlplus strmadmin/strmadminpw
SQL>
BEGIN
DBMS_CAPTURE_ADM.START_CAPTURE(
capture_name => 'capture_TEST');
END;
/
SQL>
BEGIN
DBMS_CAPTURE_ADM.START_CAPTURE(
capture_name => 'capture_TEST');
END;
/
6. 在 TEST库上启用 apply 进程
CONNECT strmadmin/strmadminpw
SQL>
BEGIN
DBMS_APPLY_ADM.SET_PARAMETER(
apply_name => 'apply_TEST',
parameter => 'disable_on_error',
value => 'n');
END;
/
PL/SQL procedure successfully completed.
SQL>
BEGIN
DBMS_APPLY_ADM.START_APPLY(
apply_name => ' apply_TEST ');
SQL>
BEGIN
DBMS_APPLY_ADM.SET_PARAMETER(
apply_name => 'apply_TEST',
parameter => 'disable_on_error',
value => 'n');
END;
/
PL/SQL procedure successfully completed.
SQL>
BEGIN
DBMS_APPLY_ADM.START_APPLY(
apply_name => ' apply_TEST ');
END;
/
PL/SQL procedure successfully completed.
SQL> select apply_name,queue_name,status from dba_apply;
APPLY_NAME QUEUE_NAME STATUS
/
PL/SQL procedure successfully completed.
SQL> select apply_name,queue_name,status from dba_apply;
APPLY_NAME QUEUE_NAME STATUS
到此从备库到主库的同步配置完成.
检验: 在备库上insert ,到主库上查看有没有.
=================Stream 的监控与维护==================================
--- 查看队列
可以用查询dba_queues,dba_queue_tables来检查:
select
owner,queue_table,
name
from
dba_queues
where
owner=
'STRMADMIN'
;
select
owner,queue_table,object_type
from
dba_queue_tables
where
owner=
'STRMADMIN'
;
-- 查看传播
select
PROPAGATION_NAME,SOURCE_QUEUE_NAME,DESTINATION_QUEUE_NAME,DESTINATION_DBLINK,STATUS
from
dba_propagation;
-- 查看捕捉
select
CAPTURE_NAME,QUEUE_NAME,START_SCN,STATUS,CAPTURE_TYPE
from
dba_capture;
select
*
from
ALL_CAPTURE_PREPARED_SCHEMAS;
-- 查看应用
可以通过:
dba_apply
v$streams_apply_reader
v$streams_apply_coordinator
v$streams_apply_server
查看状态
select
apply_name,queue_name,status
from
dba_apply;
--- 查看传播差异
SELECT
CAPTURE_NAME, QUEUE_NAME, STATUS, CAPTURED_SCN, APPLIED_SCN
FROM
DBA_CAPTURE;
----移除queue
exec dbms_streams_adm.remove_queue(
queue_name => 'streams_queue',
cascade => true,
drop_unused_queue_table => true);.
queue_name => 'streams_queue',
cascade => true,
drop_unused_queue_table => true);.