常用的在线备库搭建有Rman auxiliary 和Rman备份集恢复两种方式。
(一)
在环境配置OK的问题下,使用一条Rman命令就可以完成备库初始化同步,比较简单。
# 登陆
rman target sys/Oracle123@orcl_pri auxiliary sys/Oracle123@orcl_dg
# 开始搭建辅助数据库
RMAN>duplicate target database for standby nofilenamecheck from active database;
(二)
Rman备份集恢复的步骤相对多一些,但也不是很复杂。
(1)规划数据库参数
DB | db_name | db_unique_name | tnsname |
---|---|---|---|
primary | orcl | orcl | orcl_pri |
standby | orcl | orcl_s | orcl_dg |
DB | db_file_base | log_file_base |
---|---|---|
primary | /oradata/orcl | /oradata/log |
standby | /oradata/orcl_dg | /oradata/log_dg |
(2)主库参数配置(不考虑failover,只配置最少的参数)
alter system set log_archive_config = 'dg_config=(orcl,orcl_s)';
alter system set log_archive_dest_2 = 'SERVICE=orcl_dg ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=orcl_s' scope=both;
alter system set log_archive_dest_state_2 = ENABLE;
alter system set standby_file_management = auto;
# 生成pfile,作为备库启动的参数文件
create pfile=/home/oracle/pfile.ora' from spfile;
(3)备库直接修改pfile.ora参数,并启动至nomount状态
*.db_unique_name='center_s'
*.log_archive_dest_1='location=/oradata/log_dg'
*.log_archive_dest_2='SERVICE=orcl_pri ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=orcl'
*.db_file_name_convert='/oradata/orcl','/oradata/orcl_dg'
*.log_file_name_convert='/oradata/log','/oradata/log_dg'
# 启动数据库
create spfile from pfile='/home/oracle/pfile.ora'
startup nomount
(4)tnsnames.ora 和 listener.ora配置
(5)主库备份,上传至备库
# 全备库
RMAN> run {
allocate channel t1 type disk;
backup database FORMAT '/oradata/backup/data_%T_%s';
release channel t1;
}
# 控制文件备份
RMAN> backup current controlfile for standby format '/oradata/backup/control.bak';
(5)备库恢复数据库
# 恢复控制文件
RMAN> RESTORE STANDBY CONTROLFILE FROM '/oradata/backup/control.bak';
# 启动数据库到mount状态
RMAN> sql 'alter database mount'
# 恢复数据库
RMAN> catalog start with '/oradata/backup/';
RMAN> restore database;
RMAN> recover database;
# 恢复有报错,可以忽略
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-03002: failure of recover command at 10/17/2017 09:33:30
RMAN-06054: media recovery requesting unknown archived log for thread 1 with sequence xx and starting SCN of xxxx
(6)创建备库standby日志文件,用于实时同步数据
alter system set standby_file_management = manual;
ALTER DATABASE ADD standby LOGFILE group 11 '/oradata/log_dg/std11.log' size 500m ;
ALTER DATABASE ADD standby LOGFILE group 12 '/oradata/log_dg/std12.log' size 500m ;
ALTER DATABASE ADD standby LOGFILE group 13 '/oradata/log_dg/std13.log' size 500m ;
ALTER DATABASE ADD standby LOGFILE group 14 '/oradata/log_dg/std14.log' size 500m ;
(7)开始实时日志应用并打开数据库
select open_mode ,flashback_on,current_scn from v$database;
# 应用日志
alter database recover managed standby database disconnect from session;
# 查看应用状态,并在主库切换日志,查看序列变化
select max(sequence#),applied,archived from v$archived_log group by applied,archived;
# 打开数据库
alter database recover managed standby database cancel;
alter database open;
alter database recover managed standby database disconnect from session using current logfile;
Oracle DG是数据库架构中最常用的一种,一定要能熟练搭建DG环境和掌握DG failover /switchover 、recovery的方式。