实战篇:OracleDataGuard出现GAP修复完整步骤前⾔
DG GAP 顾名思义就是:DG不同步,当备库不能接受到⼀个或多个主库的归档⽇志⽂件时候,就发⽣了 GAP。
那么,如果遇到GAP如何修复呢?且听我细细道来~
⼀、介绍
DG GAP 主要分为以下两类情况:
1、主库归档⽇志存在,可以通过配置 Fetch Archive Log(FAL) 参数,⾃动解决归档 GAP。
2、主库归档⽇志丢失,需要⼈⼯⼲预来修复。
不同 Oracle 版本的 GAP 修复⽅式也不尽相同,下⾯分别介绍不同版本的⽅式!
11G的处理步骤:
a.在主库上创建⼀个备库的控制⽂件
b.以备库的当前SCN号为起点,在主库上做⼀个增量备份
c.将增量备份拷贝到备库上
d.使⽤新的控制⽂件将备库启动到mount状态
e.将增量备份注册到RMAN的catalog,取消备库的恢复应⽤,恢复增量备份
f.开启备库的恢复进程
12C的新特性(RECOVER … FROM SERVICE)
18C的新特性(RECOVER STANDBY DATABASE FROM SERVICE)
Oracle随着版本的升级,逐渐将步骤缩减,进⾏封装,18C之后可谓是达到了所谓的⼀键刷新,恢复DG同步。
⼆、实战
下⾯我们通过实验来进⾏演⽰如何修复:
11G常规修复
12C新特性(RECOVER … FROM SERVICE)修复
18C新特性(RECOVER STANDBY DATABASE FROM SERVICE)修复
安装测试环境可以使⽤博主编写的 Oracle ⼀键安装脚本,同时⽀持单机和 RAC 集群模式!
开源项⽬:Install Oracle Databa By Scripts!
更多更详细的脚本使⽤⽅式可以订阅专栏:Oracle⼀键安装脚本。
三、11G常规修复
⾸先,模拟备库断电,主库切⼏个最新的归档,然后⼿⼯删掉,重新开启DG同步。
备库停⽌DG同步进程:
sqlplus / as sysdba
ALTER DATABASE RECOVER MANAGED STANDBY DATABASE CANCEL;
shutdown immediate
主库切换多次归档:
sqlplus / as sysdba
alter system switch logfile;
主库删除最近⼏个归档⽇志:
rm 1_34_1070147137.arc
rm 1_33_1070147137.arc
备库开启同步进程:
startup
ALTER DATABASE RECOVER MANAGED STANDBY DATABASE USING CURRENT LOGFILE DISCONNECT FROM SESSION;
查看GAP:
sqlplus / as sysdba
SELECT * FROM V$ARCHIVE_GAP;
THREAD# LOW_SEQUENCE# HIGH_SEQUENCE#
---------- ------------- --------------
1 3
2 34
SELECT max(quence#) from v$archived_log where applied='YES';
MAX(SEQUENCE#)
--------------
31
注意:当前DG数据库已存在GAP,GAP⽇志为:32—34。
a.在主库上创建⼀个备库的控制⽂件
alter databa create standby controlfile as '/l';
b.以备库的当前SCN号为起点,在主库上做⼀个增量备份
备库查询当前 scn 号:
sqlplus / as sysdba
lect to_char(current_scn) from v$databa;
TO_CHAR(CURRENT_SCN)
----------------------------------------
1086639
确认主备GAP期间是否新增数据⽂件:
sqlplus / as sysdba
lect file# from v$datafile where creation_change# > =1086639;
主库根据备库scn号进⾏增量备份:
rman target /
run{
allocate channel c1 type disk;
allocate channel c2 type disk;
backup INCREMENTAL from scn 1086639 databa format '/tmp/incre_%U';
relea channel c1;
relea channel c2;
}
注意:如果存在新增数据⽂件,备库恢复时需要先restore新添加的数据⽂件。
c.将增量备份和控制⽂件拷贝到备库上
主库拷贝增量备份和控制⽂件你⾄备库:
scp incre_0* oracle@orcl_stby:/home/oracle
l oracle@orcl_stby:/home/oracle
注意:确认备库的磁盘空间是否⾜够存放。
d.使⽤新的控制⽂件将备库启动到mount状态
备库关闭数据库实例,开启⾄nomount状态:
sqlplus / as sysdba
shutdown immediate
startup nomount
备库恢复新的控制⽂件:
rman target /
restore controlfile from '/home/l';
备库开启到mount状态:
alter databa mount;
e.增量备份注册到RMAN的catalog,取消⽇志应⽤,恢复增量备份确认备库已关闭DG同步进程:
sqlplus / as sysdba
ALTER DATABASE RECOVER MANAGED STANDBY DATABASE CANCEL;
备库rman注册增量备份⽂件:
rman target /
catalog start with '/home/oracle/';
YES
备库开启恢复增量备份:
recover databa noredo;
f.开启备库的恢复进程
备库开启⽇志同步进程:
sqlplus / as sysdba
alter databa open read only;
ALTER DATABASE RECOVER MANAGED STANDBY DATABASE USING CURRENT LOGFILE DISCONNECT FROM SESSION;主库重新激活同步:咳嗽不能吃啥
sqlplus / as sysdba
ALTER SYSTEM SET LOG_ARCHIVE_DEST_STATE_2=defer;
ALTER SYSTEM SET LOG_ARCHIVE_DEST_STATE_2=enable;
查询是否存在GAP,确认主备是否同步:
sqlplus / as sysdba
SELECT * FROM V$ARCHIVE_GAP;肺与大肠相表里
SELECT max(quence#) from v$archived_log where applied='YES';
SELECT PROCESS, STATUS, THREAD#, SEQUENCE#, BLOCK#, BLOCKS FROM V$MANAGED_STANDBY;
⾄此,DG GAP已被修复,以上⽅式为常规修复⽅式,各个版本都通⽤。
四、12C新特性修复
⾸先,模拟备库断电,主库切⼏个最新的归档,然后⼿⼯删掉,重新开启DG同步。
模拟GAP期间,有数据⽂件添加的情况:
##主库添加数据⽂件
alter tablespace TEST add datafile '/oradata/ORCL/test02.dbf' size 100M autoextend off;
注意:当前DG数据库已存在GAP,GAP⽇志为:30—31 。
a.记录备库当前SCN号
备库记录当前 scn 号:
sqlplus / as sysdba
SELECT CURRENT_SCN FROM V$DATABASE;
CURRENT_SCN
-----------
2600487
b.使⽤recover standby using rvice恢复
b.使⽤recover standby using rvice恢复
采⽤rman的新功能,recover standby using rvice,通过RMAN连接到target备库,然后⽤主库的rvice执⾏恢复备库命令。
语法:
RECOVER DATABASE FROM SERVICE < PRIMARY DB SERVICE NAME > NOREDO USING COMPRESSED BACKUPSET;
注意:确认主库的TNS已配置,这⾥的< PRIMARY DB SERVICE NAME >即 TNSNAME。
c.备库启动到nomount状态,恢复控制⽂件
备库启动到nomount状态:
sqlplus / as sysdba
shutdown immediate
startup nomount
备库通过from rvice恢复控制⽂件:
rman target /
restore standby controlfile from rvice orcl;
备库开启到mount状态:
sqlplus / as sysdba
alter databa mount;
d.备库恢复,修复GAP
检查主备GAP期间是否添加数据⽂件:
sqlplus / as sysdba
lect file# from v$datafile where creation_change# > =2600487;
FILE#
----------
13
restore 新添加的数据⽂件:
rman target /
run
{
SET NEWNAME FOR DATABASE TO '/oradata/ORCL_STBY/%f_%U';
RESTORE DATAFILE 13 FROM SERVICE orcl;
}
由于主备的数据⽂件⽬录不⼀致,需要修改controlfile中数据⽂件位置:
rman target /
catalog start with '/oradata/ORCL_STBY';
YES
SWITCH DATABASE TO COPY;
将备库⽂件管理⽅式改为⼿动:
sqlplus / as sysdba
alter system t standby_file_management=MANUAL;
重命名 tempfile && logfile:
sqlplus / as sysdba
##logfile
alter databa clear logfile group 1;
alter databa clear logfile group 2;
alter databa clear logfile group 3;
alter databa clear logfile group 4;
alter databa clear logfile group 5;
alter databa clear logfile group 6;
alter databa clear logfile group 7;
alter databa rename file '/oradata/ORCL/redo03.log' to '/oradata/ORCL_STBY/redo03.log';
alter databa rename file '/oradata/ORCL/redo02.log' to '/oradata/ORCL_STBY/redo02.log';
alter databa rename file '/oradata/ORCL/redo01.log' to '/oradata/ORCL_STBY/redo01.log';
alter databa rename file '/oradata/ORCL/standby_redo04.log' to '/oradata/ORCL_STBY/standby_redo04.log';
alter databa rename file '/oradata/ORCL/standby_redo05.log' to '/oradata/ORCL_STBY/standby_redo05.log';
alter databa rename file '/oradata/ORCL/standby_redo06.log' to '/oradata/ORCL_STBY/standby_redo06.log';
alter databa rename file '/oradata/ORCL/standby_redo07.log' to '/oradata/ORCL_STBY/standby_redo07.log';
##tempfile
alter databa rename file '/oradata/ORCL/temp01.dbf' to '/oradata/ORCL_STBY/temp01.dbf';
alter databa rename file '/oradata/ORCL/pdbed/temp012021-04-11_06-13-50-844-AM.dbf' to '/oradata/ORCL_STBY/pdbed/temp012021-04-11_06-13-50-844-AM.dbf';
alter databa rename file '/oradata/ORCL/BFA6BEE45A1E3605E053AC01A8C0DD20/datafile/o1_mf_temp_j749f5fy_.dbf' to '/oradata/ORCL_STBY/BFA6BEE45A1E3605E053AC01A8C0DD20/datafile/o1_mf_temp_j749f5fy_.dbf';备库重命名完后再改为⾃动:
sqlplus / as sysdba
alter system t standby_file_management=AUTO;
熬大米粥的做法恢复主备GAP:
recover databa from rvice orcl noredo using compresd backupt;
注意:如果主备库⽂件⽬录不⼀致,则需要catalog切换控制⽂件中路径,否则报错:
e.开启备库⽇志应⽤,检查同步
检查主备scn是否⼀致
sqlplus / as sysdba
col HXFNM for a100
t line222
lect HXFIL File_num,substr(HXFNM,1,40) HXFNM,fhscn from x$kcvfh;
主库切⼏次归档
sqlplus / as sysdba
ALTER SYSTEM ARCHIVE LOG CURRENT;
ALTER SYSTEM SWITCH LOGFILE;
开启备库应⽤⽇志
sqlplus / as sysdba
alter databa open;
alter pluggable databa all open;
ALTER DATABASE RECOVER MANAGED STANDBY DATABASE USING CURRENT LOGFILE DISCONNECT FROM SESSION;
查看备库同步是否正常
sqlplus / as sysdba
t line222
col member for a60
up#,t1.thread#,t1.bytes/1024/1024,t1.ber from gv$standby_log t1,gv$logfile t2 up#=t2.group#;
主库插⼊数据
sqlplus test/test@pdb01
inrt into test values (999);
commit;
备库查询是否实时同步
alter ssion t container=pdb01;
lect * st;
ID
----------
1
2
999
断壁残垣的意思⾄此,GAP已修复完成,可以发现,12C这个新特性,将⼀些步骤进⾏了省略和封装,进⼀步减少了我们的操作步骤,但是内部的原理仍然是⼀致的。
五、18C新特性恢复
18C 新特性是在 12C 的基础上,将 RECOVER STANDBY DATABASE 命令与 FROM SERVICE ⼦句⼀起使⽤,以通过对主数据库进⾏的更改来刷新物理备⽤数据库。备库可以直接在开启状态进⾏刷新。
语法:
RECOVER STANDBY DATABASE FROM SERVICE primary_db;
⾸先,模拟备库断电,主库切⼏个最新的归档,然后⼿⼯删掉,重新开启DG同步。
模拟GAP期间,有数据⽂件添加的情况:
##主库添加数据⽂件
alter tablespace TEST add datafile '/oradata/ORCL/test02.dbf' size 100M autoextend off;
注意:当前 DG 数据库已存在 GAP,GAP ⽇志为:69—70。
a、执⾏RECOVER STANDBY DATABASE FROM SERVICE刷新备库
下⾯演⽰⼀下,如何使⽤⼀⾏命令在线修复DG GAP:
备库取消⽇志应⽤:
sqlplus / as sysdba
ALTER DATABASE RECOVER MANAGED STANDBY DATABASE CANCEL;
备库执⾏修复命令,开始在线刷新备库:
rman target /
RMAN> RECOVER STANDBY DATABASE FROM SERVICE orcl;
Starting recover at 19-APR-21
using target databa control file instead of recovery catalog
Oracle instance started
Total System Global Area3355441944 bytes
Fixed Size 9141016 bytes
Variable Size671088640 bytes
英雄联盟视角锁定Databa Buffers2667577344 bytes
Redo Buffers 7634944 bytes
contents of Memory Script:
{
restore standby controlfile from rvice 'orcl';
alter databa mount standby databa;
高原大陆alter databa mount standby databa;
}
executing Memory Script
Starting restore at 19-APR-21
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=502 device type=DISK
channel ORA_DISK_1: starting datafile backup t restore
channel ORA_DISK_1: using network backup t from rvice orcl
channel ORA_DISK_1: restoring control file
channel ORA_DISK_1: restore complete, elapd time: 00:00:02
output file name=/oradata/ORCL_l
output file name=/oradata/ORCL_l
Finished restore at 19-APR-21
relead channel: ORA_DISK_1
Statement procesd
Executing: alter system t standby_file_management=manual
contents of Memory Script:
{
t newname for tempfile 1 to
"/oradata/ORCL_STBY/temp01.dbf";
t newname for tempfile 2 to
"/oradata/ORCL_STBY/pdbed/temp012021-04-11_06-13-50-844-AM.dbf";
t newname for tempfile 3 to
"/oradata/ORCL_STBY/BFA6BEE45A1E3605E053AC01A8C0DD20/datafile/o1_mf_temp_j749f5fy_.dbf";
switch tempfile all;
t newname for datafile 1 to
"/oradata/ORCL_STBY/system01.dbf";
t newname for datafile 3 to
"/oradata/ORCL_STBY/sysaux01.dbf";
t newname for datafile 4 to
"/oradata/ORCL_STBY/undotbs01.dbf";
t newname for datafile 5 to
"/oradata/ORCL_STBY/pdbed/system01.dbf";
t newname for datafile 6 to
"/oradata/ORCL_STBY/pdbed/sysaux01.dbf";
t newname for datafile 7 to
"/oradata/ORCL_STBY/urs01.dbf";
t newname for datafile 8 to
"/oradata/ORCL_STBY/pdbed/undotbs01.dbf";
关于未来
t newname for datafile 9 to
"/oradata/ORCL_STBY/PDB01/o1_mf_system_j749f5d5_.dbf";
t newname for datafile 10 to
"/oradata/ORCL_STBY/PDB01/o1_mf_sysaux_j749f5fw_.dbf";
t newname for datafile 11 to
"/oradata/ORCL_STBY/PDB01/o1_mf_undotbs1_j749f5fx_.dbf";
t newname for datafile 12 to
"/oradata/ORCL_STBY/test01.dbf";
t newname for datafile 14 to
"/oradata/ORCL/test02.dbf";
restore from rvice 'orcl' datafile
14;
catalog datafilecopy "/oradata/ORCL_STBY/system01.dbf",
"/oradata/ORCL_STBY/sysaux01.dbf",
"/oradata/ORCL_STBY/undotbs01.dbf",
"/oradata/ORCL_STBY/pdbed/system01.dbf",
"/oradata/ORCL_STBY/pdbed/sysaux01.dbf",
"/oradata/ORCL_STBY/urs01.dbf",
"/oradata/ORCL_STBY/pdbed/undotbs01.dbf",
"/oradata/ORCL_STBY/PDB01/o1_mf_system_j749f5d5_.dbf",
"/oradata/ORCL_STBY/PDB01/o1_mf_sysaux_j749f5fw_.dbf",
"/oradata/ORCL_STBY/PDB01/o1_mf_undotbs1_j749f5fx_.dbf",
"/oradata/ORCL_STBY/test01.dbf",
"/oradata/ORCL/test02.dbf";
switch datafile all;
}
executing Memory Script
executing command: SET NEWNAME
executing command: SET NEWNAME
executing command: SET NEWNAME
到期日
renamed tempfile 1 to /oradata/ORCL_STBY/temp01.dbf in control file
renamed tempfile 2 to /oradata/ORCL_STBY/pdbed/temp012021-04-11_06-13-50-844-AM.dbf in control file
renamed tempfile 3 to /oradata/ORCL_STBY/BFA6BEE45A1E3605E053AC01A8C0DD20/datafile/o1_mf_temp_j749f5fy_.dbf in control file executing command: SET NEWNAME
executing command: SET NEWNAME
executing command: SET NEWNAME
executing command: SET NEWNAME
executing command: SET NEWNAME
executing command: SET NEWNAME
executing command: SET NEWNAME
executing command: SET NEWNAME
executing command: SET NEWNAME
executing command: SET NEWNAME
executing command: SET NEWNAME
executing command: SET NEWNAME
Starting restore at 19-APR-21
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=504 device type=DISK
channel ORA_DISK_1: starting datafile backup t restore
channel ORA_DISK_1: using network backup t from rvice orcl
channel ORA_DISK_1: specifying datafile(s) to restore from backup t
channel ORA_DISK_1: restoring datafile 00014 to /oradata/ORCL/test02.dbf
channel ORA_DISK_1: restore complete, elapd time: 00:00:03
Finished restore at 19-APR-21
cataloged datafile copy
datafile copy file name=/oradata/ORCL_STBY/system01.dbf RECID=4 STAMP=1070263316