Oracle 10gR2 Dataguard搭建(非duplicate方式)

我的实验环境:
源生产库(主库):
IP地址:192.168.1.30
Oracle 10.2.0.5 单实例

新DG库(备库):
IP地址:192.168.1.31
Oracle 10.2.0.5 单实例

1.源生产库开启归档

部署Dataguard环境,要求主库必须开启归档模式;如果没有开启,需要先申请停机开启归档。

shutdown immediate
startup mount
alter database archivelog;
alter database open;
archive log list;

注意:归档日志存放位置,并制定归档日志删除策略;

归档日志删除举例(删除7天前归档):

vi delarch.sql
crosscheck archivelog all;
delete noprompt archivelog all completed before "sysdate-7";

vi delarch.sh
#!/bin/bash
#Oracle ENV (you may need to change it.)
export ORACLE_BASE=/u01/app/oracle
export ORACLE_HOME=/u01/app/oracle/product/10.2.0/db_1
export ORACLE_SID=ora10
export PATH=$ORACLE_HOME/bin:$PATH
rman target / @/home/oracle/rman/delarch.sql log=/home/oracle/rman/delarch.log 

如果之前主库开启了归档,且有合理的备份策略;则实际上很可能不需要此步骤单独清除归档。

2.rman备份源生产库

将主库rman全备,视具体情况选择是否压缩备份集,我这里空间有限选择压缩备份集。

备份脚本:

[[email protected] rman]$ cat backup.sql
run {
allocate channel d1 type disk;
allocate channel d2 type disk;
backup as compressed backupset database format ‘/orabak/rman/data_%d_%T_%s.bak‘ plus archivelog format ‘/orabak/rman/log_%d_%T_%s.bak‘;
release channel d1;
release channel d2;
}

[[email protected] rman]$ cat backup.sh
#!/bin/bash
export ORACLE_BASE=/u01/app/oracle
export ORACLE_HOME=/u01/app/oracle/product/10.2.0/db_1
export ORACLE_SID=ora10
export PATH=$ORACLE_HOME/bin:$PATH
rman target / @backup.sql log backup.log

执行备份:
[[email protected] rman]$ nohup sh backup.sh &

[[email protected] rman]$ tail -200f backup.log 

Recovery Manager: Release 10.2.0.5.0 - Production on Wed Jan 4 21:47:23 2017

Copyright (c) 1982, 2007, Oracle.  All rights reserved.

connected to target database: ORA10 (DBID=914008358)

RMAN> run {
2> allocate channel d1 type disk;
3> allocate channel d2 type disk;
4> backup as compressed backupset database format ‘/orabak/rman/data_%d_%T_%s.bak‘ plus archivelog format ‘/orabak/rman/log_%d_%T_%s.bak‘;
5> release channel d1;
6> release channel d2;
7> }
8>
using target database control file instead of recovery catalog
allocated channel: d1
channel d1: sid=143 devtype=DISK

allocated channel: d2
channel d2: sid=142 devtype=DISK

Starting backup at 04-JAN-17
current log archived
channel d1: starting compressed archive log backupset
channel d1: specifying archive log(s) in backup set
input archive log thread=1 sequence=46 recid=45 stamp=932415090
input archive log thread=1 sequence=47 recid=46 stamp=932420845
channel d1: starting piece 1 at 04-JAN-17
channel d2: starting compressed archive log backupset
channel d2: specifying archive log(s) in backup set
input archive log thread=1 sequence=43 recid=42 stamp=932414314
input archive log thread=1 sequence=44 recid=43 stamp=932414362
input archive log thread=1 sequence=45 recid=44 stamp=932415036
channel d2: starting piece 1 at 04-JAN-17
channel d1: finished piece 1 at 04-JAN-17
piece handle=/orabak/rman/log_ORA10_20170104_102.bak tag=TAG20170104T214725 comment=NONE
channel d1: backup set complete, elapsed time: 00:00:02
channel d2: finished piece 1 at 04-JAN-17
piece handle=/orabak/rman/log_ORA10_20170104_103.bak tag=TAG20170104T214725 comment=NONE
channel d2: backup set complete, elapsed time: 00:00:02
Finished backup at 04-JAN-17

Starting backup at 04-JAN-17
channel d1: starting compressed full datafile backupset
channel d1: specifying datafile(s) in backupset
input datafile fno=00001 name=/oradata/ora10/ORA10/datafile/o1_mf_system_d5tw48bw_.dbf
input datafile fno=00002 name=/oradata/ora10/ORA10/datafile/o1_mf_undotbs1_d5tw48d8_.dbf
input datafile fno=00004 name=/oradata/ora10/ORA10/datafile/o1_mf_users_d5tw48dg_.dbf
channel d1: starting piece 1 at 04-JAN-17
channel d2: starting compressed full datafile backupset
channel d2: specifying datafile(s) in backupset
input datafile fno=00003 name=/oradata/ora10/ORA10/datafile/o1_mf_sysaux_d5tw48c3_.dbf
input datafile fno=00007 name=/oradata/ora10/ORA10/datafile/o1_mf_forhapoc_d5x5bm2b_.dbf
input datafile fno=00005 name=/oradata/ora10/ORA10/datafile/o1_mf_dbs_d_ji_d5x45hbj_.dbf
input datafile fno=00006 name=/oradata/ora10/ORA10/datafile/o1_mf_dbs_i_ji_d5x45jd9_.dbf
channel d2: starting piece 1 at 04-JAN-17
channel d2: finished piece 1 at 04-JAN-17
piece handle=/orabak/rman/data_ORA10_20170104_105.bak tag=TAG20170104T214728 comment=NONE
channel d2: backup set complete, elapsed time: 00:00:35
channel d1: finished piece 1 at 04-JAN-17
piece handle=/orabak/rman/data_ORA10_20170104_104.bak tag=TAG20170104T214728 comment=NONE
channel d1: backup set complete, elapsed time: 00:00:42
Finished backup at 04-JAN-17

Starting backup at 04-JAN-17
current log archived
channel d1: starting compressed archive log backupset
channel d1: specifying archive log(s) in backup set
input archive log thread=1 sequence=48 recid=47 stamp=932420890
channel d1: starting piece 1 at 04-JAN-17
channel d1: finished piece 1 at 04-JAN-17
piece handle=/orabak/rman/log_ORA10_20170104_106.bak tag=TAG20170104T214810 comment=NONE
channel d1: backup set complete, elapsed time: 00:00:02
Finished backup at 04-JAN-17

Starting Control File and SPFILE Autobackup at 04-JAN-17
piece handle=/orabak/rman/20170104/controlfilec-914008358-20170104-06 comment=NONE
Finished Control File and SPFILE Autobackup at 04-JAN-17

released channel: d1

released channel: d2

Recovery Manager complete.

[1]+  Done                    nohup sh backup.sh
[[email protected] rman]$ 

备份完成后,将备份集拷贝到备机。

3.修改源生产库参数

查看主库文件(数据文件、临时文件、重做日志文件)存放目录:

select name from v$datafile union all
select name from v$tempfile union all
select member from v$logfile;

修改源生产库参数:

--设置convert参数(我这里源端和目标端都设置了db_create_file_dest,没有设置这两个参数)
alter system set log_file_name_convert=‘‘,‘‘ scope=spfile;
alter system set db_file_name_convert=‘‘,‘‘ scope=spfile;
--设置数据库为force logging
alter database force logging;
--设置db_unique_name, log_archive_config(这里主库的db_unique_name,出于对现有的生产环境最小影响考虑没有改)
alter system set db_unique_name=‘ora10‘ scope=spfile;
alter system set log_archive_config=‘DG_CONFIG=(ora10,ora10dg)‘;
--归档日志目录
alter system set log_archive_dest_1=‘LOCATION=/orabak/arch VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=ora10‘;
alter system set log_archive_dest_2=‘SERVICE=ora10dg ASYNC LGWR VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=ora10dg‘;
--归档日志文件命名规则
alter system set log_archive_format=‘arch_%r_%t_%s.arc‘ scope=spfile;
--DG的fal_server和fal_client
alter system set fal_server=‘ora10dg‘;
alter system set fal_client=‘ora10‘;
--设置standby_file_management为自动
alter system set standby_file_management=AUTO;
--设置备库日志文件组,数量一般为目标库日志文件组+1
alter database add standby logfile group 11 size 52428800;
alter database add standby logfile group 12 size 52428800;
alter database add standby logfile group 13 size 52428800;
alter database add standby logfile group 14 size 52428800; 

4.配置tnsnames.ora

Dataguard环境,log_archive_config的配置,就是用到tnsnames.ora配置文件中的别名。

--配置tnsnames.ora
在主库所有节点上的tnsnames.ora添加相应的连接串,并传给备库。
---主库tnsnames.ora添加
vi $ORACLE_HOME/network/admin/tnsnames.ora

ORA10 =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.30)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = ora10)
    )
  )

ORA10DG =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.31)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = ora10dg)
    )
  )  

将tnsnames.ora上传到备库,如果之前tnsnames有其他内容,可以选择添加内容,只要最终保证主备库可以互相访问即可。

5.同步密码文件

--密码文件从主库拷贝到备库
[[email protected] dbs]$ scp orapwora10 192.168.1.31:/u01/app/oracle/product/10.2.0/db_1/dbs/
[email protected]‘s password:
orapwora10                                                                                                                                                  100% 1536     1.5KB/s   00:00
[[email protected] dbs]$ 

6.配置pfile文件

Dataguard环境下,需要将主库的参数文件传到备库,进行修改,尤其注意路径在备库都存在且有相应权限。

--创建pfile文件

从主库的spfile中导出pfile文件,上传到备库,并做适当的修改调整。
--在主库上创建pfile文件,并上传到备库
create pfile=‘/tmp/init.ora‘ from spfile;

SQL> create pfile=‘/tmp/init.ora‘ from spfile;

File created.

SQL> exit
Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.5.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
[[email protected] dbs]$ scp /tmp/init.ora 192.168.1.31:/tmp/
reverse mapping checking getaddrinfo for bogon failed - POSSIBLE BREAK-IN ATTEMPT!
[email protected]‘s password:
init.ora                                                                                                                                                    100% 1430     1.4KB/s   00:00
[[email protected] dbs]$ 

--备库最终pfile参数文件
[[email protected] admin]$ vi /tmp/init.ora

*.audit_file_dest=‘/u01/app/oracle/admin/ora10/adump‘
*.background_dump_dest=‘/u01/app/oracle/admin/ora10/bdump‘
*.compatible=‘10.2.0.5.0‘
*.core_dump_dest=‘/u01/app/oracle/admin/ora10/cdump‘
*.db_block_size=8192
*.db_create_file_dest=‘/oradata‘
*.db_domain=‘‘
*.db_file_multiblock_read_count=16
*.db_name=‘ora10‘
*.db_recovery_file_dest=‘/orabak/flash_recovery_area‘
*.db_recovery_file_dest_size=2147483648
*.db_unique_name=‘ora10dg‘
*.dispatchers=‘(PROTOCOL=TCP) (SERVICE=ora10XDB)‘
*.fal_client=‘ora10dg‘
*.fal_server=‘ora10‘
*.job_queue_processes=10
*.log_archive_config=‘DG_CONFIG=(ora10dg,ora10)‘
*.log_archive_dest_1=‘LOCATION=/orabak/arch VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=ora10dg‘
*.log_archive_dest_2=‘SERVICE=ora10 ASYNC LGWR VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=ora10‘
*.log_archive_format=‘arch_%r_%t_%s.arc‘
*.open_cursors=300
*.pga_aggregate_target=96468992
*.processes=150
*.remote_login_passwordfile=‘EXCLUSIVE‘
*.sga_target=290455552
*.standby_file_management=‘AUTO‘
*.undo_management=‘AUTO‘
*.undo_tablespace=‘UNDOTBS1‘
*.user_dump_dest=‘/u01/app/oracle/admin/ora10/udump‘

启动备库到nomount状态:

SQL> startup nomount pfile=‘/tmp/init.ora‘;
ORA-01261: Parameter db_recovery_file_dest destination string cannot be translated
ORA-01262: Stat failed on a file destination directory
Linux-x86_64 Error: 2: No such file or directory
SQL> exit
Disconnected
[[email protected] admin]$ mkdir -p /orabak/flash_recovery_area
[[email protected] admin]$ sqlplus / as sysdba

SQL*Plus: Release 10.2.0.5.0 - Production on Wed Jan 4 23:24:25 2017

Copyright (c) 1982, 2010, Oracle.  All Rights Reserved.

Connected to an idle instance.

SQL>  startup nomount pfile=‘/tmp/init.ora‘;
ORACLE instance started.

Total System Global Area  293601280 bytes
Fixed Size                  2095768 bytes
Variable Size             146802024 bytes
Database Buffers          138412032 bytes
Redo Buffers                6291456 bytes
SQL> exit

7.创建备库控制文件

Dataguard环境下,需要在主库创建备库的控制文件并传输到备库。

--创建控制文件
主库执行,创建备库控制文件

alter database create standby controlfile as ‘/tmp/control01.ctlbak‘;

[[email protected] dbs]$ scp /tmp/control01.ctlbak 192.168.1.31:/tmp/
reverse mapping checking getaddrinfo for bogon failed - POSSIBLE BREAK-IN ATTEMPT!
[email protected]‘s password:
control01.ctlbak                                                                                                                                            100% 6928KB   6.8MB/s   00:00    

8.还原备库

8.1 在备库上启动数据库到nomount状态

#创建SPFILE
SQL>create SPFILE from pfile=‘/tmp/init.ora‘;

#启动到nomount状态
startup nomount

8.2 启动数据库到mount状态

restore standby controlfile from ‘/tmp/control01.ctlbak‘;
alter database mount;

可能需要手工注册备份集;
crosscheck backupset;
catalog start with ‘/orabak/rman/‘;

8.3 还原备份

vi /home/oracle/scripts/restore.sh

rman target / <<EOF! > ora10_restore.log
run {
allocate channel d1 type disk;
allocate channel d2 type disk;
restore database;
release channel d1;
release channel d2;
}
exit;
EOF!

nohup sh restore.sh &

vi /home/oracle/scripts/recover.sh

rman target / <<EOF! > ora10_recover.log
run {
allocate channel d1 type disk;
allocate channel d2 type disk;
recover database;
release channel d1;
release channel d2;
}
exit;
EOF!

nohup sh recover.sh &

9.开启日志应用

备库开启日志应用

SQL> alter database recover managed standby database using current logfile disconnect from session;

10. switchover测试

switchover,主备角色互换。
注意,主机的crontab 定时任务确认也正确迁移。

--源生产库:
SELECT SWITCHOVER_STATUS FROM V$DATABASE;
alter database commit to switchover to physical standby with session shutdown;

--备库(switchover为新生产库):
SELECT SWITCHOVER_STATUS FROM V$DATABASE;
ALTER DATABASE COMMIT TO SWITCHOVER TO PRIMARY WITH SESSION SHUTDOWN;
ALTER DATABASE OPEN;

--返回源生产库执行(switchover为新备库):
shutdown immediate
startup mount
ALTER DATABASE RECOVER MANAGED STANDBY DATABASE USING CURRENT LOGFILE DISCONNECT;

如果发现备库没有实时恢复,重置链路的状态再次切换日志试试:

SQL> alter system set log_archive_dest_state_2=defer;
SQL> alter system set log_archive_dest_state_2=enable;
SQL> alter system switch logfile;

11. 创建还原点,激活备库测试

11.1 创建restore point

1) 在备库上设置flashback区域及大小

SQL> alter system set db_recovery_file_dest=‘/orabak/flash_recovery_area‘;
SQL> alter system set db_recovery_file_dest_size=1000g;
SQL> select current_scn||‘‘ from v$database;

2) Standby库取消redo日志应用并创建一个数据库闪回点

SQL> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE CANCEL;
--这里注意下,如果你的备库环境是RAC,需要关闭其他节点,然后才可以创建restore point:
SQL> drop restore point before_application_test;
SQL> CREATE RESTORE POINT before_application_test GUARANTEE FLASHBACK DATABASE;
Restore point created.

11.2 激活备库

1) 停止主库的日志传到备库

SQL> ALTER SYSTEM SET LOG_ARCHIVE_DEST_STATE_2=defer;

2) 激活备库

SQL> alter system set job_queue_processes = 0 scope=both sid=‘*‘;
SQL> ALTER DATABASE ACTIVATE STANDBY DATABASE;
SQL> shutdown immediate
SQL> startup

3) 检查dblinks
select * from dba_db_links;
如果有dblink需要删除或其他方式禁用;

4)通知新的应用IP,开始测试应用工作

注意:为了预防测试过程中有job或者dblink修改了其他数据库信息,一般要修改job_queue_processes参数,删除dblink。
--参数
alter system set job_queue_processes = 0 scope=both sid=‘‘;
--dblinks
select 
from dba_db_links;

11.3 闪回恢复备库

1) 恢复备库

SQL> shutdown immediate
SQL> startup mount;
SQL> FLASHBACK DATABASE TO RESTORE POINT before_application_test;
SQL> ALTER DATABASE CONVERT TO PHYSICAL STANDBY;
SQL> shutdown immediate
SQL> startup mount
SQL> alter database recover managed standby database using current logfile disconnect;
恢复参数job_queue_processes设置值:
alter system set job_queue_processes = 10 scope=both sid=‘*‘;
恢复参数db_recovery_file_dest_size设置值:
alter system set db_recovery_file_dest_size=2g;

2) 恢复日志传输
恢复主库的日志传到备库

ALTER SYSTEM SET LOG_ARCHIVE_DEST_STATE_2=enable;

12. failover测试

failover,源生产库不再可用,备库强制激活为主库;

#取消DG应用
ALTER DATABASE RECOVER MANAGED STANDBY DATABASE CANCEL;
#重启下数据库(建议)
shutdown immediate;
startup
#操作不可逆,确定实际情况需要failover
ALTER DATABASE RECOVER MANAGED STANDBY DATABASE FINISH force;
SELECT OPEN_MODE, DATABASE_ROLE, SWITCHOVER_STATUS, FORCE_LOGGING, DATAGUARD_BROKER, GUARD_STATUS FROM V$DATABASE;
#尝试常规切换为主库
ALTER DATABASE COMMIT TO SWITCHOVER TO PRIMARY WITH SESSION SHUTDOWN;
如果这一步的常规切换失败,提示需要介质恢复,那么:
 1)恢复备库 recover standby database until cancel;
 2)激活备库 alter database activate standby database;
#最后重新启动数据库
shutdown immediate;
startup

failover之后,即便原主库恢复也要重新搭建DG了。

时间: 2024-10-09 05:08:59

Oracle 10gR2 Dataguard搭建(非duplicate方式)的相关文章

11g使用非duplicate方式创建物理standby要注意的问题总结

在上篇博文中,使用了duplicate方式来创建物理standby http://blog.csdn.net/aaron8219/article/details/38434579 今天来说说在11g中採用非duplicate方式创建备库碰到的一些问题,并做个总结. 在10g中,通常能够使下面几种方法创建备库控制文件 RMAN> backup current controlfile for standby format 'c:\ctl_%U'; RMAN> backup full databas

Oracle 10G DataGuard搭建

前面我们有讲到Oracle 11G DataGuard的搭建,10G的搭建也是大同小异.不过Oracle 10G不支持Standby open 环境: 角色 主机名 IP 数据库版本 操作系统版本 Primary fdb1 192.168.10.8 10.2.0.1 CentOS 5.11 x86_64 Standby fdb2 192.168.10.9 10.2.0.1 CentOS 5.11 x86_64 在fdb1的/etc/hosts中加入(fdb1) 127.0.0.1 fdb1 19

Oracle 11g DataGuard搭建(一) - 单节点到单节点

(一)DataGuard概要 DataGuard中文称为”数据卫士“,提供了数据库高可用性.数据保护和灾难恢复的功能.DataGuard通过建立primary数据库和standby数据库来确立参照关系,DataGuard将主库(primary)的redo日志传递给备库(standby),然后在备库中应用redo进行同步. 备库又分为2种类型:物理备库和逻辑备库 物理standby是通过块拷贝方式同步,通过接受并应用primary数据库的redo log,以介质恢复的方式同步.在物理备库中,数据是

ORACLE 11G 之DATAGUARD搭建逻辑standby

Oracle Data Guard逻辑备库是利用主库的一个备份首先建立一个物理备库,然后再将其转换为逻辑备库.这之后主库将日志传递到备库,备库利用logminer从主库的日志中解析出主库所执行过的SQL,在备库上重新执行一遍,从而保证与主库的数据在逻辑上保持一致.与物理备库相对应的是,物理备库使用的是redo apply,逻辑备库使用的是sql apply.因此逻辑备库仅仅保证数据与主库是在逻辑上是一致的,从而逻辑备库可以处于open状态下并进行相应的DML操作. 在转换physical备库到l

CentOS 5.11下Oracle 11G R2 Dataguard搭建

Datagard算是Oracle企业版的一种容灾方案,在企业中广泛应用,我就将搭建过程记录下来以作备用. 主机名    数据库版本    实例名    IP db1    Oracle 11G R2    member    172.16.1.250 db2    Oracle 11G R2    member    172.16.1.251 默认情况下以上都已经安装好了Oracle数据库,但是只在db1上建立了数据库和监听,db2只安装Oracle软件不建库,不建监听. 目录: 打开强制归档日

ORACLE 11G DataGuard的一些高级管理案例研究

搭建完了ORACLE 11G dataguard后,也做了角色切换的实验,有switchover已经failover,感觉受益颇多,而后继续研究了下dataguard的一些高级管理功能,所谓冰山一角,ORACLE果然博大精深,总结记录如下:1,ORACLE 11G dataguard的高级管理1.1.READ ONLY/WRITE模式打开物理STANDBY一般standby都是可以设置为mount状态的,于物理standby 可以有效分担primary 数据库压力,提升资源利用,实际上说的就是这

Dataguard搭建灾备库操作手册

数据库:Oracle11gr2 主库 alter database force logging; alter system set db_unique_name='erpdb' scope=spfile;  --我们让主库db_name=db_unique_name alter system set REMOTE_LOGIN_PASSWORDFILE=EXCLUSIVE scope=spfile; alter system set LOG_ARCHIVE_FORMAT='%t_%s_%r.arc

Dataguard搭建灾备库操作

DJI erpdb库搭建DG 数据库:Oracle11gr2 主库 (下面打井号的不用执行) alter database force logging; alter system set db_unique_name='erp' scope=spfile; --我们让主库db_name=db_unique_name alter system set REMOTE_LOGIN_PASSWORDFILE=EXCLUSIVE scope=spfile; alter system set LOG_ARC

Oracle 11g RAC搭建(VMware环境)

Oracle 11g RAC搭建(VMware环境) Oracle 11g RAC搭建VMware环境 安装环境与网络规划 安装环境 网络规划 环境配置 通过SecureCRT建立命令行连接 关闭防火墙 创建必要的用户组和目录并授权 节点配置检查 系统文件设置 配置IP和hostshostname 配置grid和oracle用户环境变量 配置oracle用户ssh互信 配置裸盘 配置grid用户ssh互信 挂载安装软件文件夹 安装用于Linux的cvuqdisk 手动运行cvu使用验证程序验证O