主库实例名 clone, 从库实例名 pstandby
1 将主库(primary database)置于归档模式下
2 主库:
2.1 激活 forced logging
alter database force logging;
2.2 设置本地归档目标
alter system set log_archive_dest_1='LOCATION=d:\oracle\oradata\clone\archive' scope=BOTH;
2.3 将主库的数据库文件标记出:
SQL> select name from v$datafile;
D:\ORACLE\ORADATA\CLONE\SYSTEM01.DBF
D:\ORACLE\ORADATA\CLONE\UNDOTBS01.DBF
D:\ORACLE\ORADATA\CLONE\CWMLITE01.DBF
D:\ORACLE\ORADATA\CLONE\DRSYS01.DBF
D:\ORACLE\ORADATA\CLONE\EXAMPLE01.DBF
D:\ORACLE\ORADATA\CLONE\INDX01.DBF
D:\ORACLE\ORADATA\CLONE\ODM01.DBF
D:\ORACLE\ORADATA\CLONE\TOOLS01.DBF
D:\ORACLE\ORADATA\CLONE\USERS01.DBF
D:\ORACLE\ORADATA\CLONE\XDB01.DBF
D:\ORACLE\ORADATA\CLONE\WWFTS01.DBF
D:\ORACLE\ORADATA\CLONE\ENTSVR.DBF
已选择12行。
3 从库
3.1 关闭主库,将上述数据文件拷贝到从库d:\dataguard\pstandby目录下。如果没有该目录,请创建
3.2 启动主库到mount状态,为从库创建控制文件
ALTER DATABASE CREATE STANDBY CONTROLFILE
AS 'd:\dataguard\pstandby\pstandby.CTL';
3.3 为standby数据库创建初始化参数文件
create pfile='d:\oracle\ora92\database\INITpstandby.ora' from spfile;
3.4 设置从库的初始化参数
*.aq_tm_processes=1
*.background_dump_dest='d:\oracle\admin\clone\bdump'
*.compatible='9.2.0.0.0'
*.control_files='d:\dataguard\pstandby\PSTANDBY.CTL'
*.core_dump_dest='d:\oracle\admin\clone\cdump'
*.db_block_size=8192
*.db_cache_size=104857600
*.db_domain=''
*.db_file_multiblock_read_count=16
*.db_name='clone'
*.dispatchers='(PROTOCOL=TCP) (SERVICE=cloneXDB)'
*.fast_start_mttr_target=300
*.hash_join_enabled=TRUE
*.instance_name='pstandby'
*.java_pool_size=52428800
*.job_queue_processes=10
*.large_pool_size=8388608
*.log_archive_dest_1='LOCATION=d:\dataguard\pstandby\archive'
*.log_archive_dest_2=''
*.log_archive_start=TRUE
*.open_cursors=300
*.pga_aggregate_target=25165824
*.processes=150
*.query_rewrite_enabled='FALSE'
*.remote_login_passwordfile='EXCLUSIVE'
*.shared_pool_size=157286400
*.sort_area_size=524288
*.star_transformation_enabled='FALSE'
*.timed_statistics=TRUE
*.undo_management='AUTO'
*.undo_retention=10800
*.undo_tablespace='UNDOTBS1'
*.user_dump_dest='d:\oracle\admin\clone\udump'
*.lock_name_space='pstandby'
*.standby_file_management=AUTO
*.standby_archive_dest='D:\dataguard\pstandby\archive'
*.remote_archive_enable=TRUE
*.db_file_name_convert=('D:\oracle\oradata\clone', 'd:\dataguard\pstandby')
*.log_file_name_convert=('D:\oracle\oradata\clone', 'd:\dataguard\pstandby')
3.5 创建Windows服务
oradim -NEW -SID Pstandby -STARTMODE manual
3.6 编辑tnsnames.ora文件
*******************************************************
CLONE =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.168.0.39)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = clone)
)
)
pstandby =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.168.0.39)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = clone)
~~~~~~~~注意,这里是clone,而不是pstandby
)
)
********************************************************
然后,重新启动lsnrctl。
3.7 启动从库
C:\>set oracle_sid=pstandby
C:\>sqlplus /nolog
SQL> connect / as sysdba
SQL> STARTUP NOMOUNT;
SQL> ALTER DATABASE MOUNT STANDBY DATABASE;
3.8 在从库上,初始化log apply服务
ALTER DATABASE RECOVER MANAGED STANDBY DATABASE DISCONNECT FROM SESSION;
3.9 在主库上,执行如下sql:
SQL> ALTER SYSTEM SET LOG_ARCHIVE_DEST_2='SERVICE=Pstandby' SCOPE=BOTH;
SQL> ALTER SYSTEM SET LOG_ARCHIVE_DEST_STATE_2=ENABLE SCOPE=BOTH;
3.10 在主库上启动远程归档
SQL> ALTER SYSTEM ARCHIVE LOG CURRENT;
4 验证
4.1 在从库查询v$archived_log
SQL> select sequence#, to_char(first_time, 'yyyy-mm-dd hh24:mi:ss') first_time,
to_char(next_time, 'yyyy-mm-dd hh24:mi:ss') next_time from v$archived_log order
by sequence#;
SEQUENCE# FIRST_TIME NEXT_TIME
---------- ------------------- -------------------
123 2006-02-15 11:38:55 2006-02-15 11:50:27
124 2006-02-15 11:50:27 2006-02-15 12:07:49
125 2006-02-15 12:07:49 2006-02-15 12:32:11
126 2006-02-15 12:32:11 2006-02-15 12:33:51
127 2006-02-15 12:33:51 2006-02-15 15:12:38
128 2006-02-15 15:12:38 2006-02-15 15:23:24
129 2006-02-15 15:23:24 2006-02-15 15:27:39
已选择7行。
4.2 在主库上,归档当前日志
SQL> ALTER SYSTEM ARCHIVE LOG CURRENT;
4.3 验证是否收到,在从库上查询:
SQL> select sequence#, to_char(first_time, 'yyyy-mm-dd hh24:mi:ss') first_time,
to_char(next_time, 'yyyy-mm-dd hh24:mi:ss') next_time from v$archived_log order
by sequence#;
SEQUENCE# FIRST_TIME NEXT_TIME
--------- ------------------- -------------------
123 2006-02-15 11:38:55 2006-02-15 11:50:27
124 2006-02-15 11:50:27 2006-02-15 12:07:49
125 2006-02-15 12:07:49 2006-02-15 12:32:11
126 2006-02-15 12:32:11 2006-02-15 12:33:51
127 2006-02-15 12:33:51 2006-02-15 15:12:38
128 2006-02-15 15:12:38 2006-02-15 15:23:24
129 2006-02-15 15:23:24 2006-02-15 15:27:39
130 2006-02-15 15:27:39 2006-02-15 16:22:34
已选择8行。
4.4 验证是否新的归档日志是否已经被应用:
在从库上,查询:
SQL> select sequence#, applied from v$archived_log;
SEQUENCE# APP
--------- ---
123 YES
124 YES
125 YES
126 YES
127 YES
128 YES
129 YES
130 YES
已选择8行。
可见,新产生的日志130已经被应用到从库。
5 我们再用一个更加直观的例子来验证
在主库与从库上,我们事先建立了一个表wwf_test
查询主库:
SQL> select * from wwf_test;
A
----------
10
11
12
13
14
将从库在上面的基础上打开到如下状态
SQL> alter database recover managed standby database cancel;
数据库已更改。
SQL> alter database open read only;
数据库已更改。
SQL> select * from wwf_test;
A
----------
10
11
12
13
14
可见,两个表的状态是一致的。然后,在从库上恢复Log Apply服务,置从库于
恢复模式:
ALTER DATABASE RECOVER MANAGED STANDBY DATABASE DISCONNECT FROM SESSION;
现在,我们在主库新增加一条记录:
SQL> insert into wwf_test values(15);
已创建 1 行。
SQL> commit;
提交完成。
SQL> select * from wwf_test;
A
----------
10
11
12
13
14
15
已选择6行。
执行切换日志操作:
SQL> alter system switch logfile;
在从库上,我们查询:
SEQUENCE# APP
---------- ---
123 YES
124 YES
125 YES
126 YES
127 YES
128 YES
129 YES
130 YES
131 YES
发现,日志131已经被应用。我们以只读方式打开从库,
SQL> alter database recover managed standby database cancel;
数据库已更改。
SQL> alter database open read only;
数据库已更改。
然后,我们查询wwf_test表:
SQL> select * from wwf_test;
A
----------
10
11
12
13
14
15
已选择6行。
可见,刚才的主库上的修改已经成功地应用到从库。
再次置从库于恢复模式:
ALTER DATABASE RECOVER MANAGED STANDBY DATABASE disconnect from session;