2014-09-09 Created By BaoXinjian
一、摘要
Oracle DataGuard是一种数据库级别的HA方案,最主要功能是冗灾、数据保护、故障恢复等。
在生产数据库的"事务一致性"时,使用生产库的物理全备份(或物理COPY)创建备库,备库会通过生产库传输过来的归档日志(或重做条目)自动维护备用数据库,将重做数据应用到备用库。
本文介绍使用RMAN备份创建备库(DataGuard)。
二、Oracle DataGuard环境概述
1. 软件环境
操作系统Red Linux Enterprise as 5
数据库版本Oracle 10g release 2
2. primary databae
IP: 192.168.37.135
ORACLE_SID=gavinprod
db_unique_name=gavinprod
3. standby database
IP:192.168.37.140
ORACLE_SID=gavinstandby
db_unique_name=gavinstandby
三、主数据库(gavinprod)做准备
1. 设置主数据库为Force logging
SQL> alter database force logging;
SQL> startup ORACLE instance started. Total System Global Area 417546240 bytes Fixed Size 2213936 bytes Variable Size 322963408 bytes Database Buffers 88080384 bytes Redo Buffers 4288512 bytes Database mounted. Database opened.
SQL> alter database force logging; Database altered.
2. 创建密码文件
[oracle@gavinprod dbs]$ cd $ORACLE_HOME/dbs [oracle@gavinprod dbs]$ orapwdfile=orapwgavinprod password=oracle force=y [oracle@gavinprod dbs]$ ls -ltr
3. 修改主库的初始化参数
alter system set log_archive_config='dg_config=(gavinprod,gavinstandby)' scope=both;
alter system set log_archive_dest_1='location=/opt/gavinprod/archivelog' scope=both;
alter system set db_unique_name='gavinprod' scope=both;
SQL> alter system set log_archive_config='dg_config=(gavinprod,gavinstandby)' scope=both; System altered. SQL> alter system set log_archive_dest_1='location=/opt/gavinprod/archivelog' scope=both; System altered. SQL> alter system set db_unique_name='gavinprod' scope=both; alter system set db_unique_name='gavinprod' scope=both * ERROR at line 1: ORA-02095: specified initialization parameter cannot be modified
SQL> select name,log_mode from v$database; NAME LOG_MODE --------- ------------ GAVINPRO ARCHIVELOG
4. 生成数据库备份
RMAN> connect target sys/123456
RMAN> backup database format='/opt/gavinprod/rmanback/%d_%s.dbf' plus archivelog;
[oracle@oracle rmanback]$ ls DB1_1.dbfDB1_2.dbf
[oracle@gavinprod dbs]$ rman target / Recovery Manager: Release 11.2.0.1.0 - Production on Thu Jan 15 18:58:38 2015 Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved. connected to target database: GAVINPRO (DBID=1066005526) RMAN> connect target sys/oracle RMAN-00571: =========================================================== RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS =============== RMAN-00571: =========================================================== RMAN-06167: already connected RMAN> backup database format='/opt/gavinprod/rmanback/%d_%s.dbf' plus archivelog; Starting backup at 15-JAN-15 channel ORA_DISK_1: backup set complete, elapsed time: 00:00:01 Finished backup at 15-JAN-15
5. 生成备库的control file
SQL> alter database create standby controlfile as '/opt/gavinprod/rmanback/contrl01.ctl'; Database altered.
[oracle@gavinprod oracle]$ cd /opt/gavinprod/rmanback [oracle@gavinprod rmanback]$ ls -ltr total 1264500 -rw-r----- 1 oracle oinstall 1273921536 Jan 15 19:02 GAVINPRO_2.dbf -rw-r----- 1 oracle oinstall 9863168 Jan 15 19:02 GAVINPRO_3.dbf -rw-r----- 1 oracle oinstall 9781248 Jan 15 19:05 contrl01.ctl
6. 配置listener.ora 和tnsnames.ora文件
6.1 启动lintener.ora,
[oracle@oracle dbs]$ lsnrctl status
LSNRCTL for Linux: Version 10.2.0.4.0 - Production on 14-JUN-2009 02:54:29 Service "db1" has 1 instance(s).
Instance "db1", status READY, has 1 handler(s) for this service... Service "db1_XPT" has 1 instance(s).
Instance "db1", status READY, has 1 handler(s) for this service... The command completed successfully
6.2 配置rnsnames.ora
vi $ORACLE_HOME/network/admin/tnsnames.ora
GAVINPROD = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = gavinprod.com)(PORT = 1521)) ) (CONNECT_DATA = (SERVICE_NAME = gavinprod) ) ) GAVINSTANDBY = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = gavinstandby.com)(PORT = 1521)) ) (CONNECT_DATA = (SERVICE_NAME = gavinstandby) ) )
四、创建备份数据库(gavinprod_standby)做准备
1.设置环境变量并安装oracle软件
#环境变量设成与主库一样
#只安装软件不安装库
./runInstaller
2.建立相关的目录
cd $ORACLE_HOME/dbs/
orapwdfile=orapwgavinstandby password=oracle force=y
#注要密码要与主库的一样, 否则会归档失败
3.建立密码文件
cd $ORACLE_HOME/dbs/
orapwd file=orapwgavinstandby password=oracle force=y
#注要密码要与主库的一样, 否则会归档失败
4.建立参数文件(pfile)
vi $ORACLE_HOME/dbs/initgavinstandby.ora
db_name =gavinprod
shared_pool_size = 120M
undo_management = AUTO
undo_tablespace = undotbs
sga_max_size = 200M
sga_target = 160M
db_2k_cache_size = 4M
standby_file_management=AUTO
fal_server='gavinprod'
fal_client='gavinprod'
log_archive_dest_1='location=/opt/oracle/product/11.2.0/gavinstandby/archivelog'
log_archive_dest_2='SERVICE=gavinprod REOPEN=300'
log_archive_dest_state_1='ENABLE'
log_archive_dest_state_2='ENABLE'
5.CP主数据库RMAN备份及控制文件到备库
注意:备份存放位置要与primary database RMAN备份文件的位置相同. 控制文件存放位置要与生成standby database controlfile的位置相同
scp /oracle/rmanback/*.dbf [email protected]:/oracle/rmanback/
scp /oracle/rmanback/*.ctl [email protected]:/oracle/oradata/standby/
scp /opt/gavinprod/rmanback/*.dbf [email protected]:/opt/gavinprod/rmanback/ scp /opt/gavinprod/rmanback/*.ctl [email protected]:/opt/gavinprod/rmanback/
scp $ORACLE_HOME/dbs/initgavinprod.ora [email protected]:/opt/gavinprod/rmanback
cp contrl01.ctl /opt/oracle/product/11.2.0/gavinstandby/dbs/cntrlgavinstandby.dbf
6. 利用备用的控制文件,把备用数据库启到mount
SQL>connect / as sysdba
connnpcted to an idle instance.
SQL>startup nomount pfile=$ORACLE_HOME/dbs/initgavinprod.ora
SQL>alter database mount
SQL> startup nomount pfile=$ORACLE_HOME/dbs/initgavinstandby.ora ORACLE instance started. Total System Global Area 208769024 bytes Fixed Size 2211928 bytes Variable Size 176164776 bytes Database Buffers 25165824 bytes Redo Buffers 5226496 bytes SQL> alter database mount; Database altered.
7. 配置listener.ora 和tnsnames.ora文件.
与主库相同启动listener,tnsnames.ora也与主库配置的一样,
当主备库的监听都启动后,进行测试,以例下面能顺利进行
tnsping gaivnprod
tnsping gavinstandby
SQL> sqlplus sys/oracle@gavinprod
SQL> sqlplus sys/oracle@gavinstandby
8. 转储数据库
RMAN>connect target / connected to target database:TEST(DBID=788075692)
RMAN> restore database
9. 恢复数据库。
SQL>recover managed standby database disconnect from session;
#如果有需要应用的日志并想手工应用,可以运行如下命令
SQL>recover automatic standby database;
10. 检查standby database是否创建成功
a.在primary database 上切换日志
SQL> alter system switch logfile
b.在primary database上运行下面的语句
SQL> select max(sequence#) from v$archived_log;
MAX(SEQUENCE#) -------------- 17
c. 在standby database上运行下面的语句
SQL> select sequence# ,applied from v$archived_log order by sequence#;
SEQUENCE# APP --------------------- --- 15YES 16YES 17YES
若在上步中的max sequence#在的的app状态为YES说明standby database 成功创建.
11. 以spfile启动并设为只读
SQL> create spfile from pfile;
SQL> shutdown immedaite;
SQL> startup mount;
SQL> alter database recover managed standby database disconnect from session;
SQL> alter database open read only;
Thanks and Regards
参考: http://blog.chinaunix.net/uid-182041-id-84254.html
参考: 51CTO - http://database.51cto.com/art/200907/133644.htm
http://blog.chinaunix.net/uid-20728886-id-138190.html