Data Guard 环境:
操作系统: redhat 4.7
Primary数据库:
IP地址:10.85.10.1
数据库SID:orcl
DB_UNIQUE_NAME:orcl_pd
Standby数据库:
IP地址:10.85.10.2
数据库SID:orcl
DB_UNIQUE_NAME:orcl_st
Oracle Data Guard 理论知识
http://blog.csdn.net/tianlesoftware/archive/2010/04/22/5514082.aspx
Oracle Data Gurad Physical Standby 相关说明
http://blog.csdn.net/tianlesoftware/archive/2010/05/04/5557410.aspx
Oracle Data Guard Linux 平台 Physical Standby 搭建实例
http://blog.csdn.net/tianlesoftware/archive/2010/04/30/5547565.aspx
之前也做过相关实验, 今天这个测试主要是用RMAN 来复制备份, 之前一直是直接copy 文件的。 实验步骤都差不多。
一. Primary 端的配置
1. 主库设置为force logging 模式
SQL> alter database force logging;
2. 主库设为归档模式
SQL> archive log list;
SQL> shutdown immediate
SQL> startup mount
SQL> alter database archivelog;
SQL> archive log list;
3. 创建备库的口令文件
[oracle@localhost dbs]$ orapwd file=/u02/orapworcl password=admin
如果已经存在,就不用创建了。 缺省情况下,win下口令文件的格式是pwdsid.ora,unix下的格式是orapwSID(大小写敏感)。 Linux默认位置为$ORACLE_HOME/dbs目录下下,Windows 默认为$ORACLE_HOME/database目录。
用SCP命令将这个口令文件传输到备库的对应位置:
[oracle@db1 dbs]$ scp orapworcl 10.85.10.2:/u01/app/oracle/product/10.2.0/db_1/dbs
The authenticity of host '10.85.10.2 (10.85.10.2)' can't be established.
RSA key fingerprint is 1a:20:7a:05:bd:e0:ac:04:21:02:b1:72:01:69:40:d6.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.85.10.2' (RSA) to the list of known hosts.
oracle@10.85.10.2's password:
orapworcl 100% 1536 1.5KB/s 00:00
注意,如果不用RMAN 复制的话,还需要创建备库的控制文件。 RMAN复制的话,自己会创建控制文件。
4. 修改初始化参数文件
Pfile 默认位置在$ORACLE_HOME/dbs目录下,也可以自己指定位置:
SQL> create pfile from spfile;
在initorcl.ora 添加如下内容:
*.DB_UNIQUE_NAME='orcl_pd'
*.log_archive_dest_1='location=/u02/archivelog'
*.log_archive_dest_2='SERVICE=orcl_st'
*.LOG_ARCHIVE_DEST_STATE_1=ENABLE
*.LOG_ARCHIVE_DEST_STATE_2=ENABLE
*.standby_file_management='AUTO'
*.standby_archive_dest='/u02/archivelog'
*.FAL_SERVER='orcl_st'
*.FAL_CLIENT='orcl_pd'
如果主库和备库的数据文件位置不同,还需要加如下2个参数:
*.log_file_name_convert='/u02/oradata/orcl/','/u03/oradata/orcl/'
*.db_file_name_convert='/u02/oradata/orcl/','/u03/oradata/orcl/'
-- 注意:orcl_st,orcl_pd 是在tnsnames文件中配置的
用刚修改的pfile 启动数据库,并生成spfile。
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup pfile=?/dbs/initorcl.ora
ORACLE instance started.
Total System Global Area 247463936 bytes
Fixed Size 1218748 bytes
Variable Size 75499332 bytes
Database Buffers 163577856 bytes
Redo Buffers 7168000 bytes
Database mounted.
Database opened.
SQL> !echo $ORACLE_HOME
/u01/app/oracle/product/10.2.0/db_1
SQL> create spfile from pfile='/u01/app/oracle/product/10.2.0/db_1/dbs/initorcl.ora';
File created.
5. 修改listener.ora 和tnsnames.ora 文件
Listener.ora 文件:
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(SID_NAME = PLSExtProc)
(ORACLE_HOME = /u01/app/oracle/product/10.2.0/db_1)
(PROGRAM = extproc)
)
(SID_DESC =
(GLOBAL_DBNAME = orcl)
(ORACLE_HOME = /u01/app/oracle/product/10.2.0/db_1)
(SID_NAME = orcl)
)
)
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost.localdomain)(PORT = 1521))
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC0))
)
)
注意:SID_LIST_LISTENER 配置的是静态注册,如果没有该参数,而且Data Guard 启动顺序又不正确,那么在主库可能会报 PING[ARC1]: Heartbeat failed to connect to standby 'orcl_st'. Error is 12514. 错误,导致归档无法完成。
Oracle Listener 动态注册 与 静态注册
http://blog.csdn.net/tianlesoftware/archive/2010/04/30/5543166.aspx
Tnsnames.ora 文件
ORCL_ST =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.85.10.2)(PORT = 1521))
)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl)
)
)
ORCL_PD =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.85.10.1)(PORT = 1521))
)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl)
)
)
二. Standby 端配置
1. 创建备库存放数据文件和后台跟踪目录。
这个目录可以和主库相同, 如果不同,就需要在主库的初始化文件中进行转换。 如:
*.log_file_name_convert='/u02/oradata/orcl/','/u03/oradata/orcl/'
*.db_file_name_convert='/u02/oradata/orcl/','/u03/oradata/orcl/'
$ORACLE_BASE/ORADATA/ORCL
$ORACLE_BASE/admin/orcl
$ORACLE_BASE/admin/orcl/adump
$ORACLE_BASE/admin/orcl/bdump
$ORACLE_BASE/admin/orcl/cdump
$ORACLE_BASE/admin/orcl/dpdump
$ORACLE_BASE/admin/orcl/pfile
$ORACLE_BASE/admin/orcl/udump
$ORACLE_BASE/admin/orcl/
2. 修改初始化参数文件
从主库copy过来,修改如下:
*.DB_UNIQUE_NAME='orcl_st'
*.log_archive_dest_1='location=/u01/archivelog'
*.log_archive_dest_2='SERVICE=orcl_pd'
*.LOG_ARCHIVE_DEST_STATE_1=ENABLE
*.LOG_ARCHIVE_DEST_STATE_2=ENABLE
*.FAL_SERVER='orcl_pd'
*.FAL_CLIENT='orcl_st'
*.standby_file_management='AUTO'
*.standby_archive_dest='/u01/archivelog'
log_file_name_convert =('orcl','orcl') -- 该参数用于RMAN复制时重建redo 文件。
4. 修改listener.ora 和 tnsnames.ora 文件,同主库。如果不存在,就从主库上copy 过去。
5. RMAN 复制数据库
5.1 备份主库
[oracle@db1 backup]$ rman target sys/admin@orcl
Recovery Manager: Release 10.2.0.1.0 - Production on Sun Jul 18 18:26:16 2010
Copyright (c) 1982, 2005, Oracle. All rights reserved.
connected to target database: ORCL (DBID=1248423599)
RMAN>RUN {
allocate channel c1 type disk;
allocate channel c2 type disk;
sql 'alter system archive log current';
backup current controlfile for standby format='/u02/backup/control_%U';
BACKUP FORMAT '/u02/backup/orcl_%U_%T' skip inaccessible filesperset 5 DATABASE ;
sql 'alter system archive log current';
BACKUP FORMAT '/u02/backup/arch_%U_%T' skip inaccessible filesperset 5 ARCHIVELOG ALL DELETE INPUT;
release channel c2;
release channel c1;
}
backup full database include current controlfile for standby plus archivelog FORMAT '/u02/backup/orcl_%U_%T' skip inaccessible filesperset 5 ;
备份脚本,具体参考:
Linux 平台下 RMAN 全备 和 增量备份 shell 脚本
http://blog.csdn.net/tianlesoftware/archive/2010/07/16/5740630.aspx
5.2 用SCP将备份集复制到备库相同的位置,或者直接用NFS 挂载到相同位置。 这些位置写入了控制文件,所以位置必须相同。
Linux NFS 和 Samba 共享配置
http://blog.csdn.net/tianlesoftware/archive/2010/07/21/5752092.aspx
如果出现:ORA-27054 NFS file system where the file is created or resides is not mounted with correct options的错误,可能是NFS 挂载的参数使用不正确。
mount -t nfs 192.168.19.219:/u01/rman -o hard,rw,noac,rsize=32768,wsize=32768,suid,proto=tcp,vers=3 /u01/rman
如果用SCP复制,复制结束要后注意文件用户和权限。
5.3 用pfile文件将备库启动到nomout状态
[oracle@localhost u01]$ sqlplus /nolog
SQL*Plus: Release 10.2.0.1.0 - Production on Wed Jul 21 09:35:07 2010
Copyright (c) 1982, 2005, Oracle. All rights reserved.
SQL> conn / as sysdba
Connected to an idle instance.
SQL> startup nomount pfile=?/dbs/initorcl.ora
ORACLE instance started.
Total System Global Area 247463936 bytes
Fixed Size 1218772 bytes
Variable Size 79693612 bytes
Database Buffers 163577856 bytes
Redo Buffers 2973696 bytes
5.4 执行duplicate 复制standby库
[oracle@db1 dbs]$ rman target / auxiliary sys/admin@orcl_st;
Recovery Manager: Release 10.2.0.1.0 - Production on Tue Jul 20 22:32:59 2010
Copyright (c) 1982, 2005, Oracle. All rights reserved.
connected to target database: ORCL (DBID=1248423599)
connected to auxiliary database: ORCL (not mounted)
RMAN> duplicate target database for standby nofilenamecheck dorecover;
Starting Duplicate Db at 21-JUL-10
using target database control file instead of recovery catalog
allocated channel: ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: sid=155 devtype=DISK
contents of Memory Script:
{
set until scn 800873;
restore clone standby controlfile;
sql clone 'alter database mount standby database';
}
executing Memory Script
executing command: SET until clause
Starting restore at 21-JUL-10
using channel ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: starting datafile backupset restore
channel ORA_AUX_DISK_1: restoring control file
channel ORA_AUX_DISK_1: reading from backup piece /u02/backup/control_6mljbc6r_1_1
channel ORA_AUX_DISK_1: restored backup piece 1
piece handle=/u02/backup/control_6mljbc6r_1_1 tag=TAG20100721T115609
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:02
output filename=/u01/app/oracle/oradata/orcl/control01.ctl
output filename=/u01/app/oracle/oradata/orcl/control02.ctl
output filename=/u01/app/oracle/oradata/orcl/control03.ctl
Finished restore at 21-JUL-10
sql statement: alter database mount standby database
released channel: ORA_AUX_DISK_1
contents of Memory Script:
{
set until scn 800873;
set newname for tempfile 1 to "/u01/app/oracle/oradata/orcl/temp01.dbf";
switch clone tempfile all;
set newname for datafile 1 to "/u01/app/oracle/oradata/orcl/system01.dbf";
set newname for datafile 2 to "/u01/app/oracle/oradata/orcl/undotbs01.dbf";
set newname for datafile 3 to "/u01/app/oracle/oradata/orcl/sysaux01.dbf";
set newname for datafile 4 to "/u01/app/oracle/oradata/orcl/users01.dbf";
set newname for datafile 5 to "/u01/app/oracle/oradata/orcl/example01.dbf";
restore
check readonly
clone database
;
}
executing Memory Script
executing command: SET until clause
executing command: SET NEWNAME
renamed temporary file 1 to /u01/app/oracle/oradata/orcl/temp01.dbf 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 21-JUL-10
allocated channel: ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: sid=155 devtype=DISK
channel ORA_AUX_DISK_1: starting datafile backupset restore
channel ORA_AUX_DISK_1: specifying datafile(s) to restore from backup set
restoring datafile 00002 to /u01/app/oracle/oradata/orcl/undotbs01.dbf
restoring datafile 00003 to /u01/app/oracle/oradata/orcl/sysaux01.dbf
restoring datafile 00005 to /u01/app/oracle/oradata/orcl/example01.dbf
channel ORA_AUX_DISK_1: reading from backup piece /u02/backup/orcl_6oljbc7f_1_1_20100721
channel ORA_AUX_DISK_1: restored backup piece 1
piece handle=/u02/backup/orcl_6oljbc7f_1_1_20100721 tag=TAG20100721T115629
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:39
channel ORA_AUX_DISK_1: starting datafile backupset restore
channel ORA_AUX_DISK_1: specifying datafile(s) to restore from backup set
restoring datafile 00001 to /u01/app/oracle/oradata/orcl/system01.dbf
restoring datafile 00004 to /u01/app/oracle/oradata/orcl/users01.dbf
channel ORA_AUX_DISK_1: reading from backup piece /u02/backup/orcl_6nljbc7e_1_1_20100721
channel ORA_AUX_DISK_1: restored backup piece 1
piece handle=/u02/backup/orcl_6nljbc7e_1_1_20100721 tag=TAG20100721T115629
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:01:06
Finished restore at 21-JUL-10
contents of Memory Script:
{
switch clone datafile all;
}
executing Memory Script
datafile 1 switched to datafile copy
input datafile copy recid=10 stamp=724981117 filename=/u01/app/oracle/oradata/orcl/system01.dbf
datafile 2 switched to datafile copy
input datafile copy recid=11 stamp=724981117 filename=/u01/app/oracle/oradata/orcl/undotbs01.dbf
datafile 3 switched to datafile copy
input datafile copy recid=12 stamp=724981117 filename=/u01/app/oracle/oradata/orcl/sysaux01.dbf
datafile 4 switched to datafile copy
input datafile copy recid=13 stamp=724981118 filename=/u01/app/oracle/oradata/orcl/users01.dbf
datafile 5 switched to datafile copy
input datafile copy recid=14 stamp=724981118 filename=/u01/app/oracle/oradata/orcl/example01.dbf
contents of Memory Script:
{
set until scn 800873;
recover
standby
clone database
delete archivelog
;
}
executing Memory Script
executing command: SET until clause
Starting recover at 21-JUL-10
using channel ORA_AUX_DISK_1
starting media recovery
channel ORA_AUX_DISK_1: starting archive log restore to default destination
channel ORA_AUX_DISK_1: restoring archive log
archive log thread=1 sequence=74
channel ORA_AUX_DISK_1: reading from backup piece /u02/backup/arch_6sljbcd4_1_1_20100721
channel ORA_AUX_DISK_1: restored backup piece 1
piece handle=/u02/backup/arch_6sljbcd4_1_1_20100721 tag=TAG20100721T115930
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:04
archive log filename=/u01/archivelog/1_74_720642866.dbf thread=1 sequence=74
channel clone_default: deleting archive log(s)
archive log filename=/u01/archivelog/1_74_720642866.dbf recid=1 stamp=724981127
channel ORA_AUX_DISK_1: starting archive log restore to default destination
channel ORA_AUX_DISK_1: restoring archive log
archive log thread=1 sequence=75
channel ORA_AUX_DISK_1: reading from backup piece /u02/backup/arch_6vljbcd7_1_1_20100721
channel ORA_AUX_DISK_1: restored backup piece 1
piece handle=/u02/backup/arch_6vljbcd7_1_1_20100721 tag=TAG20100721T115930
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:04
archive log filename=/u01/archivelog/1_75_720642866.dbf thread=1 sequence=75
channel clone_default: deleting archive log(s)
archive log filename=/u01/archivelog/1_75_720642866.dbf recid=2 stamp=724981133
media recovery complete, elapsed time: 00:00:03
Finished recover at 21-JUL-10
Finished Duplicate Db at 21-JUL-10
RMAN>
6. 在备库添加redo log file
如果主库没有添加redo log file,可以先用copy 过来的初始化文件将数据库启动到mount 状态。在创建个spfile,最后添加redo log。
SQL> create spfile from pfile='/u01/app/oracle/product/10.2.0/db_1/dbs/initorcl.ora';
添加一个新的Standby Redologs组(注意组号不要与当前存在的Online Redologs组重复),并为该组指定一个成员:
SQL> ALTER DATABASE ADD STANDBY LOGFILE GROUP 4 ('/u01/app/oracle/oradata/orcl/redo04.log') size 50M;
SQL> ALTER DATABASE ADD STANDBY LOGFILE GROUP 5 ('/u01/app/oracle/oradata/orcl/redo05.log') size 50M;
SQL> ALTER DATABASE ADD STANDBY LOGFILE GROUP 6 ('/u01/app/oracle/oradata/orcl/redo06.log') size 50M;
SQL> ALTER DATABASE ADD STANDBY LOGFILE GROUP 7 ('/u01/app/oracle/oradata/orcl/redo07.log') size 50M;
提示,由于从Primary数据库复制文件时并没有复制Online Redologs,因此物理Standby数据库在第一次启动REDO应用时,会在Alert文件中报Online Redo Logfile文件不存在,没有关系,物理Standby会自动重建这批文件,同时你也不用担心会丢失数据,Online Redologs中的数据会以归档文件的形式从Primary端接收。
至此,Data Guard 的操作已经完成,下面来开始验证。
注意Data Guard 启动顺序:
启动顺序:先standby ,后primary;
闭顺序:先primary 后standby;
在备库将实例启动到mount 状态:
SQL> startup nomount;
SQL>alter database mount standby database ;
SQL>ALTER DATABASE RECOVER MANAGED STANDBY DATABASE CANCEL;
SQL>alter database recover managed standby database disconnect from session;
在备库启动监听:
$lsnrctl start
在主库启动实例:
SQL> startup;
在主库启动监听:
$lsnrctl start
在主库验证归档目录是否有效:
SQL> SELECT STATUS,DESTINATION, ERROR FROM V$ARCHIVE_DEST;
如果有错误,要排查原因。
SQL> alter system switch logfile;
SQL> select max(sequence#) from v$archived_log;
MAX(SEQUENCE#)
--------------
70
主备查询结果一致,Data Guard 搭建结束。
注意:如果在主库执行 alter database clear unarchived logfile或alter database open resetlogs , 则dataguard要重建。
其他如DG 模式切换,主备库切换等操作,参考如下blog:
Oracle Data Guard Linux 平台 Physical Standby 搭建实例
http://blog.csdn.net/tianlesoftware/archive/2010/04/30/5547565.aspx
------------------------------------------------------------------------------
Blog: http://blog.csdn.net/tianlesoftware
网上资源: http://tianlesoftware.download.csdn.net
相关视频:http://blog.csdn.net/tianlesoftware/archive/2009/11/27/4886500.aspx
DBA1 群:62697716(满); DBA2 群:62697977
DBA3 群:63306533; 聊天 群:40132017