一、数据库及操作系统初始环境:

准备已经安装完毕可以正常运行的RAC数据库,以及一台安装完ORACLE软件未创建数据库的。
RAC和单实例安装可以参考链接:
其中RAC做为DG的主库。

1.我的规划配置如下:

RAC   节点1:-IP192.168.1.221 主机名 bysrac1.bys.com ,以及数据库名 bysrac,Database unique name: bysrac
备库  IP 192.168.1.211 主机名racdg.bys.com,实例名 racdg,数据库名 bysrac,Database unique name: racdg
如下:
[grid@bysrac1 ~]$ cat /etc/hosts
127.0.0.1              localhost.localdomain localhost
::1             localhost6.localdomain6localhost6
192.168.1.221  bysrac1  bysrac1.bys.com
192.168.1.226   bysrac1-vip
192.168.1.222   bysrac2  bysrac2.bys.com
192.168.1.227   bysrac2-vip
192.168.10.1    bysrac1-priv
192.168.10.2    bysrac2-priv
192.168.1.228   bysrac-cluster bysrac-cluster-scan
192.168.1.211   racdg  racdg.bys.com
[grid@bysrac1 ~]$ srvctl config database -d bysrac -a
Database unique name: bysrac
Database name: bysrac
Oracle home: /u01/app/oracle/product/11.2.0/dbhome_1
Oracle user: oracle
Spfile: +BYSASMDATA/bysrac/spfilebysrac.ora
Domain:
Start options: open
Stop options: immediate
Database role: PRIMARY
Management policy: AUTOMATIC
Server pools: bysrac
Database instances: bysrac1,bysrac2
Disk Groups: BYSASMDATA,BYSASMDG

备库: /etc/hosts与主库完全相同即可。
[oracle@racdg ~]$ cat .bash_profile
ORACLE_BASE=/u01
ORACLE_HOME=/u01/app/oracle/product/11.2.0/dbhome_1
ORACLE_SID=racdg
PATH=$ORACLE_HOME/bin:$ORACLE_HOME/OPatch:$PATH:$HOME/bin
export PATH ORACLE_BASE ORACLE_HOME ORACLE_SID
alias sqlplus='rlwrap sqlplus'
alias rman='rlwrap rman'

2.RAC数据库中各文件位置--恢复到备库时转化文件名字用

SYS@bysrac1>archive log list
Database log mode             Archive Mode
Automatic archival             Enabled
Archive destination           +BYSASMDATA
Oldest online log sequence     156
Next log sequence to archive   157
Current log sequence           157
SYS@bysrac1>show parameter recovery
NAME                               TYPE        VALUE
------------------------------------ ----------- ------------------------------
db_recovery_file_dest               string      +BYSASMDG
db_recovery_file_dest_size          big integer 4977M
recovery_parallelism                integer     0
SYS@bysrac1>select db_unique_name,name from v$database;
DB_UNIQUE_NAME                NAME
------------------------------ ---------
bysrac                        BYSRAC
SYS@bysrac1>select name from v$datafile;
NAME
----------------------------------------------------------------------------------------------------
+BYSASMDATA/bysrac/datafile/system.259.818615175
+BYSASMDATA/bysrac/datafile/sysaux.260.818615237
+BYSASMDATA/bysrac/datafile/undotbs1.261.818615291
+BYSASMDATA/bysrac/datafile/undotbs2.263.818615365
+BYSASMDATA/bysrac/datafile/users.264.818615419
+BYSASMDATA/bysrac/datafile/test1_undo.dbf
+BYSASMDATA/bysrac/datafile/test1.dbf
BYS@bysrac1>selectname,block_size*file_size_blks/1024/1024 as bytes_m from v$controlfile;
NAME                                                BYTES_M
-------------------------------------------------- ----------
+BYSASMDATA/bysrac/controlfile/current.256.8186151    17.6875
19
+BYSASMDG/bysrac/controlfile/current.256.818615127    17.6875
BYS@bysrac1>select group#,member from v$logfile;
    GROUP# MEMBER
---------- --------------------------------------------------
         1+BYSASMDATA/bysrac/onlinelog/group_1.257.818615137
         1+BYSASMDG/bysrac/onlinelog/group_1.257.818615145
         2+BYSASMDATA/bysrac/onlinelog/group_2.258.818615153
         2+BYSASMDG/bysrac/onlinelog/group_2.258.818615163
         3+BYSASMDATA/bysrac/onlinelog/group_3.265.818619941
         3+BYSASMDG/bysrac/onlinelog/group_3.259.818619949
         4+BYSASMDATA/bysrac/onlinelog/group_4.266.818619961
         4+BYSASMDG/bysrac/onlinelog/group_4.260.818619967

二、修改RAC中初始化参数

3.在RAC主库中修改初始化参数

SQL> alter system set standby_file_management=auto scope=spfile;
SQL> alter system set log_archive_config="DG_CONFIG=(bysrac,racdg)"scope=spfile;
SQL> alter system set log_archive_dest_2="SERVICE=racdg LGWRSYNC  VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE)DB_UNIQUE_NAME=racdg" scope=spfile;

SQL> alter system set fal_server=racdg scope=spfile;
SQL> alter system set fal_client=bysrac;

SQL> alter system setdb_file_name_convert="+BYSASMDATA/bysrac/datafile/","/u01/oradata/racdg",'+BYSASMDATA/bysrac/tempfile/','/u01/oradata/racdg/' scope=spfile;
SQL> alter system set log_file_name_convert="+BYSASMDATA/bysrac/onlinelog/","/u01/oradata/racdg",'+BYSASMDG/bysrac/onlinelog/','/u01/oradata/racdg/'  scope=spfile;
SQL>create pfile='/home/oracle/initaa.ora' from spfile;    ------这里创建的PFILE第五步时候发送到备库racdg。

三、配置双方监听互相通过服务名访问

4.双方监听配置文件示例如下:

RAC的listener.ora 就使用RAC安装好后的不用修改。

[oracle@bysrac1 admin]$ cat  tnsnames.ora 
 BYSRAC =
   (DESCRIPTION =
     (ADDRESS = (PROTOCOL = TCP)(HOST = bysrac-cluster-scan)(PORT =1521))
     (CONNECT_DATA =
       (SERVER = DEDICATED)
       (SERVICE_NAME = bysrac)
     )
   )

 racdg =
   (DESCRIPTION =
     (ADDRESS = (PROTOCOL = TCP)(HOST = racdg)(PORT = 1521))
     (CONNECT_DATA =
       (SERVER = DEDICATED)
       (SERVICE_NAME = racdg)
     )
   )
racdg的tnsnames.ora同上:
 [oracle@racdg admin]$ cat listener.ora 

 LISTENER =
   (DESCRIPTION_LIST =
     (DESCRIPTION =
       (ADDRESS = (PROTOCOL = TCP)(HOST =racdg.bys.com)(PORT = 1521))
     )
   )

 SID_LIST_LISTENER = 
   (SID_LIST = 
      (SID_DESC =
       (GLOBAL_DBNAME = racdg)
       (ORACLE_HOME =/u01/app/oracle/product/11.2.0/dbhome_1)
       (SID_NAME = racdg)
     ) 
   ) 
 ADR_BASE_LISTENER = /u01

5.发密码及初化参数文件到racdg,并修改初始化参数文件

[oracle@bysrac1 dbs]$ ls
hc_bysrac1.dat  initbysrac1.ora  orapwbysrac1   peshm_DBUA0_0
hc_DBUA0.dat    init.ora        peshm_bysrac_1  snapcf_bysrac1.f
[oracle@bysrac1 dbs]$ scp orapwbysrac1  racdg:$ORACLE_HOME/dbs/
oracle@racdg's password:
orapwbysrac1                                    100% 1536     1.5KB/s  00:00   
[oracle@bysrac1 dbs]$ cd
[oracle@bysrac1 ~]$ ls
alert_bysrac1.log  initaa.ora    inittest.ora     oradiag_oracle
Desktop           initaa.orabak  inittest.orabak
[oracle@bysrac1 ~]$ scp inittest.ora  racdg:$ORACLE_HOME/dbs
oracle@racdg's password:
inittest.ora                                    100% 1024     1.0KB/s  00:00

6.在racdg上修改密码及参数文件名,并修改初始化参数

[oracle@racdg dbs]$ ls
inittest.ora  orapwbysrac1
[oracle@racdg dbs]$ mv orapwbysrac1 orapwracdg
[oracle@racdg dbs]$ mv inittest.ora initracdg.ora
[oracle@racdg dbs]$ ls
initracdg.ora  orapwracdg

修改备库初始化参数文件,最终是这样:
注意点有:
db_file_name_convert时,因为使用ASM,DATAFILE 和TEMPFILE的都要写上。
log_file_name_convert时,因为使用了在不同磁盘组的多镜像,也需要都写上。
audit_file_dest指定的目录要手动去创建。
control_files尽量写一个,少出错。可以在STANDBY备库创建好后再增加镜像。
.log_archive_dest_1指定的目录要创建一下。.log_archive_dest_2是为了SWITCHOVER  或FAILOVER时备库切换为主库用的。
db_unique_name需要手动添加上。
[oracle@racdg dbs]$ cat initracdg.ora
*.audit_file_dest='/u01/admin/bysrac/adump'
*.audit_trail='db'
*.compatible='11.2.0.0.0'
*.control_files='/u01/oradata/racdg/control01.ctl'
*.db_block_size=8192
*.db_create_file_dest='/u01/oradata/racdg/'
*.db_domain=''
*.db_file_name_convert='+BYSASMDATA/bysrac/datafile/','/u01/oradata/racdg/','+BYSASMDATA/bysrac/tempfile/','/u01/oradata/racdg/'
*.db_name='bysrac'
*.db_recovery_file_dest='/u01/flash_recovery_area/'
*.db_recovery_file_dest_size=5218762752
*.diagnostic_dest='/u01'
*.fal_server='RACDG'
*.log_archive_config='DG_CONFIG=(bysrac,racdg)'
*.log_archive_dest_1='LOCATION=/u01/archracdg/'
*.log_archive_dest_2='SERVICE=bysrac1 LGWRSYNC  VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=bysrac'
*.log_archive_format='arch_%r_%t_%s.arc'
*.log_file_name_convert='+BYSASMDATA/bysrac/onlinelog/','/u01/oradata/racdg/','+BYSASMDG/bysrac/onlinelog/','/u01/oradata/racdg/'
*.memory_target=741343232
*.open_cursors=300
*.processes=150
*.remote_listener='bysrac-cluster-scan:1521'
*.remote_login_passwordfile='exclusive'
*.standby_file_management='AUTO'
*.undo_tablespace='undotbs1'
*.db_unique_name='racdg'

7.双方互测监听是否正常

racdg 登陆bysrac1
[oracle@racdg admin]$ sqlplus sys/oraclesys@bysrac as sysdba
SYS@bys1>
bysrac1 登陆racdg
[oracle@bysrac1 admin]$ sqlplus sys/oraclesys@racdg as sysdba
Connected to an idle instance.    ---备库实例未启动。
SYS@bysrac1>

四、复制数据库到备库

8.在主库上为备库创建一个standby controlfile

SYS@bysrac1>alter system archive log current;
SYS@bysrac1>alter database create standby controlfile as'/home/oracle/control01.ctl';
发送到备库的相同位置,后面恢复数据库到备库时要用。
[oracle@bysrac1 ~]$ scp control01.ctlracdg:/home/oracle/control01.ctl
oracle@racdg's password:
control01.ctl         100%  18MB  17.7MB/s   00:01  
此时可以在虚拟机关机做快照

9.在主库做备份并把备份集发送到备库

[oracle@bysrac1 ~]$ rman target /
RMAN> backup database format  '/home/oracle/racfull_%s_%p';

[oracle@bysrac1 ~]$ du -sh racfull_*
766M    racfull_5_1
18M     racfull_6_1
[oracle@bysrac1 ~]$ scp racfull_5_1 racfull_6_1  racdg:/home/oracle/
oracle@racdg's password:
racfull_5_1          100%  765MB   5.8MB/s  02:11   
racfull_6_1           100%  18MB   4.5MB/s   00:04

10.在备库用RMAN做复制STANDBY数据库操作:有日志

这里要注意的有要用用户名和密码连接主库和备库。
还有如果使用虚拟机的话,性能也很重要,我在这里就卡死机了一次。

[oracle@racdg ~]$ rman target sys/oraclesys@bysrac auxiliary sys/oraclesys
 Recovery Manager: Release 11.2.0.1.0 - Production on Mon Aug 12 22:13:22 2013
 Copyright (c) 1982, 2009, Oracle and/or its affiliates.  All rightsreserved.
 connected to target database: BYSRAC (DBID=2573560818)
 connected to auxiliary database: BYSRAC (not mounted)

 RMAN> duplicate target database for standby;
 Starting Duplicate Db at 12-AUG-13
 using target database control file instead of recovery catalog
 allocated channel: ORA_AUX_DISK_1
 channel ORA_AUX_DISK_1: SID=19 device type=DISK

 contents of Memory Script:
 {
    restore clone standby controlfile;
 }
 executing Memory Script

 Starting restore at 12-AUG-13
 using channel ORA_AUX_DISK_1

 channel ORA_AUX_DISK_1: restoring control file
 channel ORA_AUX_DISK_1: copied control file copy
 input file name=/home/oracle/control01.ctl
 output file name=/u01/oradata/racdg/control01.ctl
 Finished restore at 12-AUG-13

 contents of Memory Script:
 {
    sql clone 'alter database mount standby database';
 }
 executing Memory Script

 sql statement: alter database mount standby database

 contents of Memory Script:
 {
    set newname for tempfile  1 to 
 "/u01/oradata/racdg/temp.262.818615321";
    switch clone tempfile all;
    set newname for datafile  1 to 
 "/u01/oradata/racdg/system.259.818615175";
    set newname for datafile  2 to 
 "/u01/oradata/racdg/sysaux.260.818615237";
    set newname for datafile  3 to 
 "/u01/oradata/racdg/undotbs1.261.818615291";
    set newname for datafile  4 to 
 "/u01/oradata/racdg/undotbs2.263.818615365";
    set newname for datafile  5 to 
 "/u01/oradata/racdg/users.264.818615419";
    restore
    clone database
    ;
 }
 executing Memory Script
 executing command: SET NEWNAME
 renamed tempfile 1 to /u01/oradata/racdg/temp.262.818615321 in control file
 executing command: SET NEWNAME
 executing command: SET NEWNAME
 executing command: SET NEWNAME
 executing command: SET NEWNAME
 executing command: SET NEWNAME
 Starting restore at 12-AUG-13
 using channel ORA_AUX_DISK_1

 channel ORA_AUX_DISK_1: starting datafile backup set restore
 channel ORA_AUX_DISK_1: specifying datafile(s) to restore from backup set
 channel ORA_AUX_DISK_1: restoring datafile 00001 to/u01/oradata/racdg/system.259.818615175
 channel ORA_AUX_DISK_1: restoring datafile 00002 to/u01/oradata/racdg/sysaux.260.818615237
 channel ORA_AUX_DISK_1: restoring datafile 00003 to/u01/oradata/racdg/undotbs1.261.818615291
 channel ORA_AUX_DISK_1: restoring datafile 00004 to/u01/oradata/racdg/undotbs2.263.818615365
 channel ORA_AUX_DISK_1: restoring datafile 00005 to /u01/oradata/racdg/users.264.818615419
 channel ORA_AUX_DISK_1: reading from backup piece /home/oracle/racfull_5_1
 channel ORA_AUX_DISK_1: piece handle=/home/oracle/racfull_5_1tag=TAG20130812T200604
 channel ORA_AUX_DISK_1: restored backup piece 1
 channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:04:15
 Finished restore at 12-AUG-13

 contents of Memory Script:
 {
    switch clone datafile all;
 }
 executing Memory Script

 datafile 1 switched to datafile copy
 input datafile copy RECID=2 STAMP=823299518 file name=/u01/oradata/racdg/system.259.818615175
 datafile 2 switched to datafile copy
 input datafile copy RECID=3 STAMP=823299518 filename=/u01/oradata/racdg/sysaux.260.818615237
 datafile 3 switched to datafile copy
 input datafile copy RECID=4 STAMP=823299518 file name=/u01/oradata/racdg/undotbs1.261.818615291
 datafile 4 switched to datafile copy
 input datafile copy RECID=5 STAMP=823299518 filename=/u01/oradata/racdg/undotbs2.263.818615365
 datafile 5 switched to datafile copy
 input datafile copy RECID=6 STAMP=823299518 file name=/u01/oradata/racdg/users.264.818615419
Finished Duplicate Db at 12-AUG-13

五、打开DG,检测是否可用

11.打开备库,此时DG处于最大性能模式

可以通过select protection_mode,database_role,open_mode from v$database;在主备库上进行验证当前DG模式
同时可以在备库上alter database recover managed standby database disconnect fromsession;此时DG可以正常运行。
这里就不演示查询结果了,因为我在下面一步要配置DG为最大可用模式。

12.配置为最大可用模式

在备库要创建STANDBY REDO LOGFILE。所要创建在的目录当然要先存在了。

使用的语句如下:

alter database add standby logfile group 5'/u01/oradata/racdg/standbyredo/standbyredo05.log' size 50M;

alter database add standby logfile group 6'/u01/oradata/racdg/standbyredo/standbyredo06.log' size 50M;

alter database add standby logfile group 7'/u01/oradata/racdg/standbyredo/standbyredo07.log' size 50M;

alter database add standby logfile group 8'/u01/oradata/racdg/standbyredo/standbyredo08.log' size 50M;

alter database add standby logfile group 9'/u01/oradata/racdg/standbyredo/standbyredo09.log' size 50M;

BYS@racdg>select member from v$logfile;

MEMBER

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

/u01/oradata/racdg/group_1.257.818615137

/u01/oradata/racdg/group_1.257.818615145

/u01/oradata/racdg/group_2.258.818615153

/u01/oradata/racdg/group_2.258.818615163

/u01/oradata/racdg/group_3.265.818619941

/u01/oradata/racdg/group_3.259.818619949

/u01/oradata/racdg/group_4.266.818619961

/u01/oradata/racdg/group_4.260.818619967

/u01/oradata/racdg/standbyredo/standbyredo05.log

/u01/oradata/racdg/standbyredo/standbyredo06.log

/u01/oradata/racdg/standbyredo/standbyredo07.log

/u01/oradata/racdg/standbyredo/standbyredo08.log

/u01/oradata/racdg/standbyredo/standbyredo09.log

在主库验证DG工作模式并修改保护模式为最大可用

22:44:28  SYS@bysrac1>selectprotection_mode,database_role,open_mode from v$database;

PROTECTION_MODE      DATABASE_ROLE    OPEN_MODE
-------------------- ---------------- --------------------
MAXIMUM PERFORMANCE  PRIMARY         READ WRITE
22:47:13  SYS@bysrac1>show parameter log_archive_dest_2
NAME                        TYPE       VALUE
------------------------------------ ----------- ------------------------------
log_archive_dest_2                  string   SERVICE=racdgLGWR SYNC  VALID
                                       _FOR=(ONLINE_LOGFILES,PRIMARY_
                                       ROLE) DB_UNIQUE_NAME=racdg
22:48:52 SYS@bysrac1>shutdown immediate;
22:50:00 SYS@bysrac1>startup mount;
ORACLE instance started.
Total System Global Area  740724736 bytes
Fixed Size                 1339092 bytes
Variable Size             549454124bytes
Database Buffers          184549376 bytes
Redo Buffers               5382144 bytes
Database mounted.
22:52:31 SYS@bysrac1>alter database set standbydatabase to maximize availability;
Database altered.
22:53:48 SYS@bysrac1>alter database open;
Database altered.
22:55:56 SYS@bysrac1>select protection_mode,database_role,open_mode fromv$database;
PROTECTION_MODE      DATABASE_ROLE    OPEN_MODE
-------------------- ---------------- --------------------
MAXIMUM AVAILABILITY PRIMARY         READ WRITE
###############################

备库上的操作:

22:58:17 SYS@racdg>shutdown immediate;
ORA-01109: database not open

Database dismounted.
ORACLE instance shut down.
22:59:18 SYS@racdg>startup mount;
ORACLE instance started.
Total System Global Area  740724736 bytes
Fixed Size                 1339092 bytes
Variable Size             436207916bytes
Database Buffers          297795584 bytes
Redo Buffers               5382144 bytes
Database mounted.
22:59:45 SYS@racdg>selectprotection_mode,database_role,open_mode from v$database;
PROTECTION_MODE      DATABASE_ROLE    OPEN_MODE
-------------------- ---------------- --------------------
MAXIMUM AVAILABILITY PHYSICAL STANDBY MOUNTED
这里我要使用了11G新特性ACTIVE STANDBY
SYS@racdg>alter database open;
Database altered.
SYS@racdg>set time on
23:01:06 SYS@racdg>select protection_mode,database_role,open_mode fromv$database;
PROTECTION_MODE      DATABASE_ROLE    OPEN_MODE
-------------------- ---------------- --------------------
MAXIMUM AVAILABILITY PHYSICAL STANDBY READ ONLY

使用 using current logfile参数启动实时恢复

23:01:08 SYS@racdg> alter database recover managedstandby database using current logfile disconnect from session;
Database altered.
23:02:05 SYS@racdg>select protection_mode,database_role,open_mode fromv$database;
PROTECTION_MODE      DATABASE_ROLE    OPEN_MODE
-------------------- ---------------- --------------------
MAXIMUM AVAILABILITY PHYSICAL STANDBY READ ONLY WITH APPLY

13.测试DG双方能否同步

切换日志,并做DML操作
SYS@racdg>selectsequence#,status,thread#,block# from v$managed_standby;
SEQUENCE# STATUS          THREAD#    BLOCK#
---------- ------------ ---------- ----------
       168 CLOSING              1          1
       166 CLOSING              1          1
         0 CONNECTED            0          0
       169 CLOSING              1          1
       170 APPLYING_LOG         1         10
         0 IDLE                 0         0
         0 IDLE                 0         0
         0 IDLE                 0         0
       170 IDLE                 1        10
BYS@bysrac1>set time on
23:03:45 BYS@bysrac1>alter system switch logfile;
System altered.
23:03:53 BYS@bysrac1>select sequence#,status,thread#,block# fromv$managed_standby;
SEQUENCE# STATUS          THREAD#    BLOCK#
---------- ------------ ---------- ----------
       166 CLOSING              1       1673
       164 CLOSING              1          1
       169 CLOSING              1          1
       168 CLOSING              1          1
       170 WRITING              1        26

在主库中创建一个新表
23:06:49 BYS@bysrac1>create table test(a varchar2(10));
Table created.
23:07:12 BYS@bysrac1>select * from test;
no rows selected
在备库中立刻就可以查到这个新表已经存在。
23:08:01 SYS@racdg>conn bys/bys
Connected.
23:08:24 BYS@racdg>select * from test;
no rows selected