一、介绍

DG GAP顾名思义就是:DG不同步,当备库不能接受到一个或多个主库的归档日志文件时候,就发生了GAP。
1、主库归档日志存在,可以通过配置Fetch Archive Log(FAL)参数,自动解决归档GAP。
2、主库归档日志丢失,需要人工干预来修复:
a.11G and before的常规处理步骤:
1.在主库上创建一个备库的控制文件 2.以备库的当前SCN号为起点,在主库上做一个增量备份 3.将增量备份拷贝到备库上 4.使用新的控制文件将备库启动到mount状态 5.将增量备份注册到RMAN的catalog,取消备库的恢复应用,恢复增量备份 6.开启备库的恢复进程
b.12C and later的新特性RECOVER … FROM SERVICE
c.18C and later的新特性RECOVER STANDBY DATABASE FROM SERVICE
Oracle随着版本的升级,逐渐将步骤缩减,进行封装,18C之后可谓是达到了所谓的一键刷新,恢复DG同步。
下面我们通过实验来进行展示:
首先,模拟备库断电,主库切几个最新的归档,然后手工删掉,重新开启DG同步。

  1. ##备库:
  2. ALTER DATABASE RECOVER MANAGED STANDBY DATABASE CANCEL;
  3. shutdown immediate
  4. ##主库:
  5. alter system switch logfile;多次切归档
  6. ##删除最近几个归档日志:
  7. rm 1_34_1070147137.arc
  8. rm 1_33_1070147137.arc
  9. ##备库:
  10. startup
  11. ALTER DATABASE RECOVER MANAGED STANDBY DATABASE USING CURRENT LOGFILE DISCONNECT FROM SESSION;
  12. ##查看GAP
  13. SQL> SELECT * FROM V$ARCHIVE_GAP;
  14. THREAD# LOW_SEQUENCE# HIGH_SEQUENCE#
  15. ---------- ------------- --------------
  16. 1 32 34
  17. SQL> SELECT max(sequence#) from v$archived_log where applied='YES';
  18. MAX(SEQUENCE#)
  19. --------------
  20. 31
  21. SQL> SELECT PROCESS, STATUS, THREAD#, SEQUENCE#, BLOCK#, BLOCKS FROM V$MANAGED_STANDBY;
  22. PROCESS STATUS THREAD# SEQUENCE# BLOCK# BLOCKS
  23. --------- ------------ ---------- ---------- ---------- ----------
  24. ARCH CLOSING 1 38 1 40
  25. ARCH CLOSING 1 39 1 2
  26. ARCH CONNECTED 0 0 0 0
  27. ARCH CLOSING 1 37 1 2
  28. RFS IDLE 0 0 0 0
  29. RFS IDLE 0 0 0 0
  30. RFS IDLE 1 40 83 1
  31. MRP0 APPLYING_LOG 1 33 1 245760
  32. 8 rows selected.
  33. -----------------------------------

二、11G and Before

当前GAP:32—-34

1.在主库上创建一个备库的控制文件

  1. SQL> alter database create standby controlfile as '/tmp/standby.ctl';
  2. Database altered.

2.以备库的当前SCN号为起点,在主库上做一个增量备份

  1. --备库
  2. SQL> select to_char(current_scn) from v$database;
  3. TO_CHAR(CURRENT_SCN)
  4. ----------------------------------------
  5. 1086639
  6. --主库
  7. rman target /
  8. run{
  9. allocate channel c1 type disk;
  10. allocate channel c2 type disk;
  11. backup INCREMENTAL from scn 1086639 database format '/tmp/incre_%U';
  12. release channel c1;
  13. release channel c2;
  14. }
  15. -----------------------------------

3.将增量备份拷贝到备库上

  1. --主库
  2. [oracle@orcl:/tmp]$ scp incre_0* oracle@orcl_stby:/home/oracle
  3. oracle@orcl_stby's password:
  4. incre_0cvsjs8b_1_1 100% 144KB 144.0KB/s 00:00
  5. incre_0dvsjs9a_1_1 100% 416KB 416.0KB/s 00:00
  6. incre_0evsjs9a_1_1 100% 144KB 144.0KB/s 00:00
  7. incre_0fvsjs9b_1_1 100% 9856KB 9.6MB/s 00:00
  8. [oracle@orcl:/tmp]$ scp standby.ctl oracle@orcl_stby:/home/oracle
  9. oracle@orcl_stby's password:
  10. standby.ctl
  11. -----------------------------------

4.使用新的控制文件将备库启动到mount状态

  1. shutdown immediate
  2. startup nomount
  3. rman target /
  4. RMAN> restore controlfile from '/home/oracle/standby.ctl';
  5. Starting restore at 18-APR-21
  6. using target database control file instead of recovery catalog
  7. allocated channel: ORA_DISK_1
  8. channel ORA_DISK_1: SID=19 device type=DISK
  9. channel ORA_DISK_1: copied control file copy
  10. output file name=/oradata/orcl/control01.ctl
  11. output file name=/u01/app/oracle/fast_recovery_area/orcl/control02.ctl
  12. Finished restore at 18-APR-21
  13. alter database mount;

5.增量备份注册到RMAN的catalog,取消日志应用,恢复增量备份

  1. --备库
  2. ALTER DATABASE RECOVER MANAGED STANDBY DATABASE CANCEL;
  3. rman target /
  4. RMAN> catalog start with '/home/oracle/';
  5. YES
  6. RMAN> recover database noredo;
  7. Starting recover at 18-APR-21
  8. allocated channel: ORA_DISK_1
  9. channel ORA_DISK_1: SID=29 device type=DISK
  10. channel ORA_DISK_1: starting incremental datafile backup set restore
  11. channel ORA_DISK_1: specifying datafile(s) to restore from backup set
  12. destination for restore of datafile 00001: /oradata/orcl/system01.dbf
  13. destination for restore of datafile 00004: /oradata/orcl/users01.dbf
  14. destination for restore of datafile 00006: /oradata/orcl/test01.dbf
  15. channel ORA_DISK_1: reading from backup piece /home/oracle/incre_0evsjs9a_1_1
  16. channel ORA_DISK_1: piece handle=/home/oracle/incre_0evsjs9a_1_1 tag=TAG20210418T133122
  17. channel ORA_DISK_1: restored backup piece 1
  18. channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
  19. channel ORA_DISK_1: starting incremental datafile backup set restore
  20. channel ORA_DISK_1: specifying datafile(s) to restore from backup set
  21. destination for restore of datafile 00002: /oradata/orcl/sysaux01.dbf
  22. destination for restore of datafile 00003: /oradata/orcl/undotbs01.dbf
  23. destination for restore of datafile 00005: /oradata/orcl/example01.dbf
  24. channel ORA_DISK_1: reading from backup piece /home/oracle/incre_0dvsjs9a_1_1
  25. channel ORA_DISK_1: piece handle=/home/oracle/incre_0dvsjs9a_1_1 tag=TAG20210418T133122
  26. channel ORA_DISK_1: restored backup piece 1
  27. channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
  28. Finished recover at 18-APR-21
  29. -----------------------------------

6.开启备库的恢复进程

  1. alter database open read only;
  2. ALTER DATABASE RECOVER MANAGED STANDBY DATABASE USING CURRENT LOGFILE DISCONNECT FROM SESSION;
  3. --GAP已修复
  4. SQL> SELECT * FROM V$ARCHIVE_GAP;
  5. no rows selected
  6. SQL> SELECT max(sequence#) from v$archived_log where applied='YES';
  7. MAX(SEQUENCE#)
  8. --------------
  9. 41
  10. SQL> SELECT PROCESS, STATUS, THREAD#, SEQUENCE#, BLOCK#, BLOCKS FROM V$MANAGED_STANDBY;
  11. PROCESS STATUS THREAD# SEQUENCE# BLOCK# BLOCKS
  12. --------- ------------ ---------- ---------- ---------- ----------
  13. ARCH CONNECTED 0 0 0 0
  14. ARCH CONNECTED 0 0 0 0
  15. ARCH CONNECTED 0 0 0 0
  16. ARCH CLOSING 1 41 1 257
  17. RFS IDLE 0 0 0 0
  18. RFS IDLE 0 0 0 0
  19. RFS IDLE 1 42 19969 1
  20. MRP0 APPLYING_LOG 1 42 19969 245760
  21. 8 rows selected.
  22. -----------------------------------

三、12C and Later

  1. SQL> SELECT * FROM V$ARCHIVE_GAP;
  2. THREAD# LOW_SEQUENCE# HIGH_SEQUENCE# CON_ID
  3. ---------- ------------- -------------- ----------
  4. 1 30 31 1
  5. SQL> SELECT max(sequence#) from v$archived_log where applied='YES';
  6. MAX(SEQUENCE#)
  7. --------------
  8. 29
  9. SQL> SELECT PROCESS, STATUS, THREAD#, SEQUENCE#, BLOCK#, BLOCKS FROM V$MANAGED_STANDBY;
  10. PROCESS STATUS THREAD# SEQUENCE# BLOCK# BLOCKS
  11. --------- ------------ ---------- ---------- ---------- ----------
  12. ARCH CONNECTED 0 0 0 0
  13. DGRD ALLOCATED 0 0 0 0
  14. DGRD ALLOCATED 0 0 0 0
  15. ARCH CONNECTED 0 0 0 0
  16. ARCH CLOSING 1 29 1 268
  17. ARCH CONNECTED 0 0 0 0
  18. RFS IDLE 1 34 184 1
  19. RFS IDLE 1 0 0 0
  20. RFS IDLE 0 0 0 0
  21. RFS IDLE 0 0 0 0
  22. RFS IDLE 0 0 0 0
  23. MRP0 WAIT_FOR_GAP 1 30 0 0
  24. DGRD ALLOCATED 0 0 0 0
  25. 13 rows selected.
  26. [oracle@orcl_stby:/archivelog]$ ll
  27. total 11508
  28. -rw-r-----. 1 oracle oinstall 11634176 Apr 19 02:05 1_28_1069567741.arc
  29. -rw-r-----. 1 oracle oinstall 137728 Apr 19 02:08 1_29_1069567741.arc
  30. -rw-r-----. 1 oracle oinstall 1536 Apr 19 02:08 1_32_1069567741.arc
  31. -rw-r-----. 1 oracle oinstall 3584 Apr 19 02:08 1_33_1069567741.arc

当前GAP:30—-31,缺少归档30,31

1.记录主库和备库当前SCN号

  1. --主库
  2. SQL> set line222
  3. SQL> col HXFNM for a100
  4. SQL> select HXFIL File_num,substr(HXFNM,1,40) HXFNM,fhscn from x$kcvfh;
  5. FILE_NUM HXFNM FHSCN
  6. ---------- ---------------------------------------------------------------------------------------------------- --------------------
  7. 1 /oradata/ORCL/system01.dbf 2600522
  8. 3 /oradata/ORCL/sysaux01.dbf 2600522
  9. 4 /oradata/ORCL/undotbs01.dbf 2600522
  10. 5 /oradata/ORCL/pdbseed/system01.dbf 2155383
  11. 6 /oradata/ORCL/pdbseed/sysaux01.dbf 2155383
  12. 7 /oradata/ORCL/users01.dbf 2600522
  13. 8 /oradata/ORCL/pdbseed/undotbs01.dbf 2155383
  14. 9 /oradata/ORCL/BFA6BEE45A1E3605E053AC01A8 2600522
  15. 10 /oradata/ORCL/BFA6BEE45A1E3605E053AC01A8 2600522
  16. 11 /oradata/ORCL/BFA6BEE45A1E3605E053AC01A8 2600522
  17. 12 /oradata/ORCL/test01.dbf 2600522
  18. --备库
  19. SQL> set line222
  20. SQL> col HXFNM for a100
  21. SQL> select HXFIL File_num,substr(HXFNM,1,40) HXFNM,fhscn from x$kcvfh;
  22. FILE_NUM HXFNM FHSCN
  23. ---------- ---------------------------------------------------------------------------------------------------- --------------------
  24. 1 /oradata/ORCL_STBY/system01.dbf 2600488
  25. 3 /oradata/ORCL_STBY/sysaux01.dbf 2600488
  26. 4 /oradata/ORCL_STBY/undotbs01.dbf 2600488
  27. 5 /oradata/ORCL_STBY/pdbseed/system01.dbf 2155383
  28. 6 /oradata/ORCL_STBY/pdbseed/sysaux01.dbf 2155383
  29. 7 /oradata/ORCL_STBY/users01.dbf 2600488
  30. 8 /oradata/ORCL_STBY/pdbseed/undotbs01.dbf 2155383
  31. 9 /oradata/ORCL_STBY/PDB01/o1_mf_system_j7 2600488
  32. 10 /oradata/ORCL_STBY/PDB01/o1_mf_sysaux_j7 2600488
  33. 11 /oradata/ORCL_STBY/PDB01/o1_mf_undotbs1_ 2600488
  34. 12 /oradata/ORCL_STBY/test01.dbf 2600488
  35. SQL> SELECT CURRENT_SCN FROM V$DATABASE;
  36. CURRENT_SCN
  37. -----------
  38. 2600487

2.使用recover standby using service恢复

采用rman的新功能,recover standby using service,通过RMAN连接到target备库,然后用主库的service执行恢复备库命令。
语法:

  1. RECOVER DATABASE FROM SERVICE < PRIMARY DB SERVICE NAME > NOREDO USING COMPRESSED BACKUPSET;

模拟GAP期间,有数据文件添加的情况:

  1. -主库添加数据文件
  2. SQL> alter tablespace TEST add datafile '/oradata/ORCL/test02.dbf' size 100M autoextend off;
  3. Tablespace altered.
  4. --检查备库scn是否添加数据文件
  5. SQL> select file# from v$datafile where creation_change# > =2600487;
  6. FILE#
  7. ----------
  8. 13
  9. --备库启动到nomount状态
  10. shutdown immediate
  11. startup nomount
  12. --rman恢复
  13. rman target /
  14. ##恢复控制文件
  15. RMAN> restore standby controlfile from service orcl;
  16. Starting restore at 19-APR-21
  17. using target database control file instead of recovery catalog
  18. allocated channel: ORA_DISK_1
  19. channel ORA_DISK_1: SID=739 device type=DISK
  20. channel ORA_DISK_1: starting datafile backup set restore
  21. channel ORA_DISK_1: using network backup set from service orcl
  22. channel ORA_DISK_1: restoring control file
  23. channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
  24. output file name=/oradata/ORCL_STBY/control01.ctl
  25. output file name=/oradata/ORCL_STBY/control02.ctl
  26. Finished restore at 19-APR-21
  27. ##mount数据库
  28. alter database mount;
  29. ##restore新添加的数据文件
  30. run
  31. {
  32. SET NEWNAME FOR DATABASE TO '/oradata/ORCL_STBY/%f_%U';
  33. RESTORE DATAFILE 13 FROM SERVICE orcl;
  34. }
  35. executing command: SET NEWNAME
  36. Starting restore at 19-APR-21
  37. allocated channel: ORA_DISK_1
  38. channel ORA_DISK_1: SID=503 device type=DISK
  39. channel ORA_DISK_1: starting datafile backup set restore
  40. channel ORA_DISK_1: using network backup set from service orcl
  41. channel ORA_DISK_1: specifying datafile(s) to restore from backup set
  42. channel ORA_DISK_1: restoring datafile 00013 to /oradata/ORCL_STBY/13_data_D-ORCL_TS-TEST_FNO-13
  43. channel ORA_DISK_1: restore complete, elapsed time: 00:00:03
  44. Finished restore at 19-APR-21
  45. ##由于主备的数据文件目录不一致,需要修改controlfile中数据文件位置
  46. catalog start with '/oradata/ORCL_STBY';
  47. YES
  48. SWITCH DATABASE TO COPY;
  49. ##rename tempfile && logfile
  50. alter system set standby_file_management=MANUAL;
  51. ##logfile
  52. alter database clear logfile group 1;
  53. alter database clear logfile group 2;
  54. alter database clear logfile group 3;
  55. alter database clear logfile group 4;
  56. alter database clear logfile group 5;
  57. alter database clear logfile group 6;
  58. alter database clear logfile group 7;
  59. alter database rename file '/oradata/ORCL/redo03.log' to '/oradata/ORCL_STBY/redo03.log';
  60. alter database rename file '/oradata/ORCL/redo02.log' to '/oradata/ORCL_STBY/redo02.log';
  61. alter database rename file '/oradata/ORCL/redo01.log' to '/oradata/ORCL_STBY/redo01.log';
  62. alter database rename file '/oradata/ORCL/standby_redo04.log' to '/oradata/ORCL_STBY/standby_redo04.log';
  63. alter database rename file '/oradata/ORCL/standby_redo05.log' to '/oradata/ORCL_STBY/standby_redo05.log';
  64. alter database rename file '/oradata/ORCL/standby_redo06.log' to '/oradata/ORCL_STBY/standby_redo06.log';
  65. alter database rename file '/oradata/ORCL/standby_redo07.log' to '/oradata/ORCL_STBY/standby_redo07.log';
  66. ##tempfile
  67. alter database rename file '/oradata/ORCL/temp01.dbf' to '/oradata/ORCL_STBY/temp01.dbf';
  68. alter database rename file '/oradata/ORCL/pdbseed/temp012021-04-11_06-13-50-844-AM.dbf' to '/oradata/ORCL_STBY/pdbseed/temp012021-04-11_06-13-50-844-AM.dbf';
  69. alter database rename file '/oradata/ORCL/BFA6BEE45A1E3605E053AC01A8C0DD20/datafile/o1_mf_temp_j749f5fy_.dbf' to '/oradata/ORCL_STBY/BFA6BEE45A1E3605E053AC01A8C0DD20/datafile/o1_mf_temp_j749f5fy_.dbf';
  70. alter system set standby_file_management=AUTO;
  71. ##恢复数据库
  72. RMAN> recover database from service orcl noredo using compressed backupset;
  73. Starting recover at 19-APR-21
  74. allocated channel: ORA_DISK_1
  75. channel ORA_DISK_1: SID=2 device type=DISK
  76. skipping datafile 5; already restored to SCN 2155383
  77. skipping datafile 6; already restored to SCN 2155383
  78. skipping datafile 8; already restored to SCN 2155383
  79. channel ORA_DISK_1: starting incremental datafile backup set restore
  80. channel ORA_DISK_1: using compressed network backup set from service orcl
  81. destination for restore of datafile 00001: /oradata/ORCL_STBY/system01.dbf
  82. channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
  83. channel ORA_DISK_1: starting incremental datafile backup set restore
  84. channel ORA_DISK_1: using compressed network backup set from service orcl
  85. destination for restore of datafile 00003: /oradata/ORCL_STBY/sysaux01.dbf
  86. channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
  87. channel ORA_DISK_1: starting incremental datafile backup set restore
  88. channel ORA_DISK_1: using compressed network backup set from service orcl
  89. destination for restore of datafile 00004: /oradata/ORCL_STBY/undotbs01.dbf
  90. channel ORA_DISK_1: restore complete, elapsed time: 00:00:02
  91. channel ORA_DISK_1: starting incremental datafile backup set restore
  92. channel ORA_DISK_1: using compressed network backup set from service orcl
  93. destination for restore of datafile 00007: /oradata/ORCL_STBY/users01.dbf
  94. channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
  95. channel ORA_DISK_1: starting incremental datafile backup set restore
  96. channel ORA_DISK_1: using compressed network backup set from service orcl
  97. destination for restore of datafile 00009: /oradata/ORCL_STBY/PDB01/o1_mf_system_j749f5d5_.dbf
  98. channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
  99. channel ORA_DISK_1: starting incremental datafile backup set restore
  100. channel ORA_DISK_1: using compressed network backup set from service orcl
  101. destination for restore of datafile 00010: /oradata/ORCL_STBY/PDB01/o1_mf_sysaux_j749f5fw_.dbf
  102. channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
  103. channel ORA_DISK_1: starting incremental datafile backup set restore
  104. channel ORA_DISK_1: using compressed network backup set from service orcl
  105. destination for restore of datafile 00011: /oradata/ORCL_STBY/PDB01/o1_mf_undotbs1_j749f5fx_.dbf
  106. channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
  107. channel ORA_DISK_1: starting incremental datafile backup set restore
  108. channel ORA_DISK_1: using compressed network backup set from service orcl
  109. destination for restore of datafile 00012: /oradata/ORCL_STBY/test01.dbf
  110. channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
  111. Finished recover at 19-APR-21

Notes:如果主备库文件目录不一致,则需要catalog切换控制文件中路径,否则报错:

  1. RMAN> recover database from service orcl noredo using compressed backupset;
  2. Starting recover at 19-APR-21
  3. using channel ORA_DISK_1
  4. channel ORA_DISK_1: starting incremental datafile backup set restore
  5. channel ORA_DISK_1: using compressed network backup set from service orcl
  6. RMAN-00571: ===========================================================
  7. RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
  8. RMAN-00571: ===========================================================
  9. RMAN-03002: failure of recover command at 04/19/2021 02:25:29
  10. ORA-19625: error identifying file /oradata/ORCL/system01.dbf
  11. ORA-27037: unable to obtain file status
  12. Linux-x86_64 Error: 2: No such file or directory
  13. Additional information: 7
  14. -----------------------------------

3.开启备库日志应用

  1. --此时主备库的sch已恢复一致
  2. SQL> col HXFNM for a100
  3. SQL> set line222
  4. SQL> select HXFIL File_num,substr(HXFNM,1,40) HXFNM,fhscn from x$kcvfh;
  5. FILE_NUM HXFNM FHSCN
  6. ---------- ---------------------------------------------------------------------------------------------------- --------------------
  7. 1 /oradata/ORCL_STBY/system01.dbf 2603512
  8. 3 /oradata/ORCL_STBY/sysaux01.dbf 2603514
  9. 4 /oradata/ORCL_STBY/undotbs01.dbf 2603516
  10. 5 /oradata/ORCL_STBY/pdbseed/system01.dbf 2155383
  11. 6 /oradata/ORCL_STBY/pdbseed/sysaux01.dbf 2155383
  12. 7 /oradata/ORCL_STBY/users01.dbf 2603518
  13. 8 /oradata/ORCL_STBY/pdbseed/undotbs01.dbf 2155383
  14. 9 /oradata/ORCL_STBY/PDB01/o1_mf_system_j7 2603521
  15. 10 /oradata/ORCL_STBY/PDB01/o1_mf_sysaux_j7 2603524
  16. 11 /oradata/ORCL_STBY/PDB01/o1_mf_undotbs1_ 2603527
  17. 12 /oradata/ORCL_STBY/test01.dbf 2603530
  18. 11 rows selected.
  19. --主库需要切几次归档
  20. ALTER SYSTEM ARCHIVE LOG CURRENT;
  21. 或者
  22. ALTER SYSTEM SWITCH LOGFILE;
  23. --开启备库应用日志
  24. alter database open;
  25. alter pluggable database all open;
  26. ALTER DATABASE RECOVER MANAGED STANDBY DATABASE USING CURRENT LOGFILE DISCONNECT FROM SESSION;

4、测试同步情况

  1. --查看standby日志是否正常
  2. set line222
  3. col member for a60
  4. select t1.group#,t1.thread#,t1.bytes/1024/1024,t1.status,t2.member from gv$standby_log t1,gv$logfile t2 where t1.group#=t2.group#;
  5. GROUP# THREAD# T1.BYTES/1024/1024 STATUS MEMBER
  6. ---------- ---------- ------------------ ---------- ------------------------------------------------------------
  7. 4 1 120 ACTIVE /oradata/ORCL/standby_redo04.log
  8. 5 1 120 UNASSIGNED /oradata/ORCL/standby_redo05.log
  9. 6 1 120 UNASSIGNED /oradata/ORCL/standby_redo06.log
  10. 7 1 120 UNASSIGNED /oradata/ORCL/standby_redo07.log
  11. --主库插入数据
  12. sqlplus test/test@pdb01
  13. insert into test values (999);
  14. commit;
  15. --备库查询
  16. SQL> alter session set container=pdb01;
  17. SQL> select * from test.test;
  18. ID
  19. ----------
  20. 1
  21. 2
  22. 999
  23. --备库已同步

三、18C and Later

将RECOVER STANDBY DATABASE命令与FROM SERVICE子句一起使用,以通过对主数据库进行的更改来刷新物理备用数据库。
备库可以直接在开启状态进行刷新。
语法:

  1. RECOVER STANDBY DATABASE FROM SERVICE primary_db;

模拟GAP期间,有数据文件添加的情况:

  1. --备库存在GAP
  2. SQL> SELECT PROCESS, STATUS, THREAD#, SEQUENCE#, BLOCK#, BLOCKS FROM V$MANAGED_STANDBY;
  3. PROCESS STATUS THREAD# SEQUENCE# BLOCK# BLOCKS
  4. --------- ------------ ---------- ---------- ---------- ----------
  5. ARCH CONNECTED 0 0 0 0
  6. DGRD ALLOCATED 0 0 0 0
  7. DGRD ALLOCATED 0 0 0 0
  8. ARCH CONNECTED 0 0 0 0
  9. ARCH CONNECTED 0 0 0 0
  10. ARCH CONNECTED 0 0 0 0
  11. RFS IDLE 1 0 0 0
  12. RFS IDLE 1 72 119 1
  13. RFS IDLE 0 0 0 0
  14. RFS IDLE 0 0 0 0
  15. MRP0 WAIT_FOR_GAP 1 70 0 0
  16. 11 rows selected.
  17. SQL> SELECT max(sequence#) from v$archived_log where applied='YES';
  18. MAX(SEQUENCE#)
  19. --------------
  20. 69
  21. SQL> select * from v$archive_gap;
  22. THREAD# LOW_SEQUENCE# HIGH_SEQUENCE# CON_ID
  23. ---------- ------------- -------------- ----------
  24. 1 70 70 1
  25. --主库添加数据文件
  26. SQL> alter tablespace TEST add datafile '/oradata/ORCL/test02.dbf' size 100M autoextend off;
  27. Tablespace altered.

1、执行RECOVER STANDBY DATABASE FROM SERVICE刷新备库:

通过执行过程可以发现:
RECOVER STANDBY DATABASE命令重新启动备用实例,从主数据库刷新控制文件,并自动重命名数据文件,临时文件和联机日志。 它可以还原添加到主数据库中的新数据文件,并还原到当前时间的备用数据库。

  1. ##备库
  2. ##取消日志应用
  3. ALTER DATABASE RECOVER MANAGED STANDBY DATABASE CANCEL;
  4. ##开始刷新备库
  5. rman target /
  6. RMAN> RECOVER STANDBY DATABASE FROM SERVICE orcl;
  7. Starting recover at 19-APR-21
  8. using target database control file instead of recovery catalog
  9. Oracle instance started
  10. Total System Global Area 3355441944 bytes
  11. Fixed Size 9141016 bytes
  12. Variable Size 671088640 bytes
  13. Database Buffers 2667577344 bytes
  14. Redo Buffers 7634944 bytes
  15. contents of Memory Script:
  16. {
  17. restore standby controlfile from service 'orcl';
  18. alter database mount standby database;
  19. }
  20. executing Memory Script
  21. Starting restore at 19-APR-21
  22. allocated channel: ORA_DISK_1
  23. channel ORA_DISK_1: SID=502 device type=DISK
  24. channel ORA_DISK_1: starting datafile backup set restore
  25. channel ORA_DISK_1: using network backup set from service orcl
  26. channel ORA_DISK_1: restoring control file
  27. channel ORA_DISK_1: restore complete, elapsed time: 00:00:02
  28. output file name=/oradata/ORCL_STBY/control01.ctl
  29. output file name=/oradata/ORCL_STBY/control02.ctl
  30. Finished restore at 19-APR-21
  31. released channel: ORA_DISK_1
  32. Statement processed
  33. Executing: alter system set standby_file_management=manual
  34. contents of Memory Script:
  35. {
  36. set newname for tempfile 1 to
  37. "/oradata/ORCL_STBY/temp01.dbf";
  38. set newname for tempfile 2 to
  39. "/oradata/ORCL_STBY/pdbseed/temp012021-04-11_06-13-50-844-AM.dbf";
  40. set newname for tempfile 3 to
  41. "/oradata/ORCL_STBY/BFA6BEE45A1E3605E053AC01A8C0DD20/datafile/o1_mf_temp_j749f5fy_.dbf";
  42. switch tempfile all;
  43. set newname for datafile 1 to
  44. "/oradata/ORCL_STBY/system01.dbf";
  45. set newname for datafile 3 to
  46. "/oradata/ORCL_STBY/sysaux01.dbf";
  47. set newname for datafile 4 to
  48. "/oradata/ORCL_STBY/undotbs01.dbf";
  49. set newname for datafile 5 to
  50. "/oradata/ORCL_STBY/pdbseed/system01.dbf";
  51. set newname for datafile 6 to
  52. "/oradata/ORCL_STBY/pdbseed/sysaux01.dbf";
  53. set newname for datafile 7 to
  54. "/oradata/ORCL_STBY/users01.dbf";
  55. set newname for datafile 8 to
  56. "/oradata/ORCL_STBY/pdbseed/undotbs01.dbf";
  57. set newname for datafile 9 to
  58. "/oradata/ORCL_STBY/PDB01/o1_mf_system_j749f5d5_.dbf";
  59. set newname for datafile 10 to
  60. "/oradata/ORCL_STBY/PDB01/o1_mf_sysaux_j749f5fw_.dbf";
  61. set newname for datafile 11 to
  62. "/oradata/ORCL_STBY/PDB01/o1_mf_undotbs1_j749f5fx_.dbf";
  63. set newname for datafile 12 to
  64. "/oradata/ORCL_STBY/test01.dbf";
  65. set newname for datafile 14 to
  66. "/oradata/ORCL/test02.dbf";
  67. restore from service 'orcl' datafile
  68. 14;
  69. catalog datafilecopy "/oradata/ORCL_STBY/system01.dbf",
  70. "/oradata/ORCL_STBY/sysaux01.dbf",
  71. "/oradata/ORCL_STBY/undotbs01.dbf",
  72. "/oradata/ORCL_STBY/pdbseed/system01.dbf",
  73. "/oradata/ORCL_STBY/pdbseed/sysaux01.dbf",
  74. "/oradata/ORCL_STBY/users01.dbf",
  75. "/oradata/ORCL_STBY/pdbseed/undotbs01.dbf",
  76. "/oradata/ORCL_STBY/PDB01/o1_mf_system_j749f5d5_.dbf",
  77. "/oradata/ORCL_STBY/PDB01/o1_mf_sysaux_j749f5fw_.dbf",
  78. "/oradata/ORCL_STBY/PDB01/o1_mf_undotbs1_j749f5fx_.dbf",
  79. "/oradata/ORCL_STBY/test01.dbf",
  80. "/oradata/ORCL/test02.dbf";
  81. switch datafile all;
  82. }
  83. executing Memory Script
  84. executing command: SET NEWNAME
  85. executing command: SET NEWNAME
  86. executing command: SET NEWNAME
  87. renamed tempfile 1 to /oradata/ORCL_STBY/temp01.dbf in control file
  88. renamed tempfile 2 to /oradata/ORCL_STBY/pdbseed/temp012021-04-11_06-13-50-844-AM.dbf in control file
  89. renamed tempfile 3 to /oradata/ORCL_STBY/BFA6BEE45A1E3605E053AC01A8C0DD20/datafile/o1_mf_temp_j749f5fy_.dbf in control file
  90. executing command: SET NEWNAME
  91. executing command: SET NEWNAME
  92. executing command: SET NEWNAME
  93. executing command: SET NEWNAME
  94. executing command: SET NEWNAME
  95. executing command: SET NEWNAME
  96. executing command: SET NEWNAME
  97. executing command: SET NEWNAME
  98. executing command: SET NEWNAME
  99. executing command: SET NEWNAME
  100. executing command: SET NEWNAME
  101. executing command: SET NEWNAME
  102. Starting restore at 19-APR-21
  103. allocated channel: ORA_DISK_1
  104. channel ORA_DISK_1: SID=504 device type=DISK
  105. channel ORA_DISK_1: starting datafile backup set restore
  106. channel ORA_DISK_1: using network backup set from service orcl
  107. channel ORA_DISK_1: specifying datafile(s) to restore from backup set
  108. channel ORA_DISK_1: restoring datafile 00014 to /oradata/ORCL/test02.dbf
  109. channel ORA_DISK_1: restore complete, elapsed time: 00:00:03
  110. Finished restore at 19-APR-21
  111. cataloged datafile copy
  112. datafile copy file name=/oradata/ORCL_STBY/system01.dbf RECID=4 STAMP=1070263316
  113. cataloged datafile copy
  114. datafile copy file name=/oradata/ORCL_STBY/sysaux01.dbf RECID=5 STAMP=1070263317
  115. cataloged datafile copy
  116. datafile copy file name=/oradata/ORCL_STBY/undotbs01.dbf RECID=6 STAMP=1070263317
  117. cataloged datafile copy
  118. datafile copy file name=/oradata/ORCL_STBY/pdbseed/system01.dbf RECID=7 STAMP=1070263317
  119. cataloged datafile copy
  120. datafile copy file name=/oradata/ORCL_STBY/pdbseed/sysaux01.dbf RECID=8 STAMP=1070263318
  121. cataloged datafile copy
  122. datafile copy file name=/oradata/ORCL_STBY/users01.dbf RECID=9 STAMP=1070263318
  123. cataloged datafile copy
  124. datafile copy file name=/oradata/ORCL_STBY/pdbseed/undotbs01.dbf RECID=10 STAMP=1070263318
  125. cataloged datafile copy
  126. datafile copy file name=/oradata/ORCL_STBY/PDB01/o1_mf_system_j749f5d5_.dbf RECID=11 STAMP=1070263318
  127. cataloged datafile copy
  128. datafile copy file name=/oradata/ORCL_STBY/PDB01/o1_mf_sysaux_j749f5fw_.dbf RECID=12 STAMP=1070263318
  129. cataloged datafile copy
  130. datafile copy file name=/oradata/ORCL_STBY/PDB01/o1_mf_undotbs1_j749f5fx_.dbf RECID=13 STAMP=1070263318
  131. cataloged datafile copy
  132. datafile copy file name=/oradata/ORCL_STBY/test01.dbf RECID=14 STAMP=1070263318
  133. cataloged datafile copy
  134. datafile copy file name=/oradata/ORCL/test02.dbf RECID=15 STAMP=1070263318
  135. datafile 14 switched to datafile copy
  136. input datafile copy RECID=15 STAMP=1070263318 file name=/oradata/ORCL/test02.dbf
  137. datafile 1 switched to datafile copy
  138. input datafile copy RECID=4 STAMP=1070263316 file name=/oradata/ORCL_STBY/system01.dbf
  139. datafile 3 switched to datafile copy
  140. input datafile copy RECID=5 STAMP=1070263317 file name=/oradata/ORCL_STBY/sysaux01.dbf
  141. datafile 4 switched to datafile copy
  142. input datafile copy RECID=6 STAMP=1070263317 file name=/oradata/ORCL_STBY/undotbs01.dbf
  143. datafile 5 switched to datafile copy
  144. input datafile copy RECID=7 STAMP=1070263317 file name=/oradata/ORCL_STBY/pdbseed/system01.dbf
  145. datafile 6 switched to datafile copy
  146. input datafile copy RECID=8 STAMP=1070263318 file name=/oradata/ORCL_STBY/pdbseed/sysaux01.dbf
  147. datafile 7 switched to datafile copy
  148. input datafile copy RECID=9 STAMP=1070263318 file name=/oradata/ORCL_STBY/users01.dbf
  149. datafile 8 switched to datafile copy
  150. input datafile copy RECID=10 STAMP=1070263318 file name=/oradata/ORCL_STBY/pdbseed/undotbs01.dbf
  151. datafile 9 switched to datafile copy
  152. input datafile copy RECID=11 STAMP=1070263318 file name=/oradata/ORCL_STBY/PDB01/o1_mf_system_j749f5d5_.dbf
  153. datafile 10 switched to datafile copy
  154. input datafile copy RECID=12 STAMP=1070263318 file name=/oradata/ORCL_STBY/PDB01/o1_mf_sysaux_j749f5fw_.dbf
  155. datafile 11 switched to datafile copy
  156. input datafile copy RECID=13 STAMP=1070263318 file name=/oradata/ORCL_STBY/PDB01/o1_mf_undotbs1_j749f5fx_.dbf
  157. datafile 12 switched to datafile copy
  158. input datafile copy RECID=14 STAMP=1070263318 file name=/oradata/ORCL_STBY/test01.dbf
  159. Executing: alter database rename file '/oradata/ORCL/redo01.log' to '/oradata/ORCL_STBY/redo01.log'
  160. Executing: alter database rename file '/oradata/ORCL/redo02.log' to '/oradata/ORCL_STBY/redo02.log'
  161. Executing: alter database rename file '/oradata/ORCL/redo03.log' to '/oradata/ORCL_STBY/redo03.log'
  162. contents of Memory Script:
  163. {
  164. recover database from service 'orcl';
  165. }
  166. executing Memory Script
  167. Starting recover at 19-APR-21
  168. using channel ORA_DISK_1
  169. skipping datafile 5; already restored to SCN 2155383
  170. skipping datafile 6; already restored to SCN 2155383
  171. skipping datafile 8; already restored to SCN 2155383
  172. skipping datafile 14; already restored to SCN 2658548
  173. channel ORA_DISK_1: starting incremental datafile backup set restore
  174. channel ORA_DISK_1: using network backup set from service orcl
  175. destination for restore of datafile 00001: /oradata/ORCL_STBY/system01.dbf
  176. channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
  177. channel ORA_DISK_1: starting incremental datafile backup set restore
  178. channel ORA_DISK_1: using network backup set from service orcl
  179. destination for restore of datafile 00003: /oradata/ORCL_STBY/sysaux01.dbf
  180. channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
  181. channel ORA_DISK_1: starting incremental datafile backup set restore
  182. channel ORA_DISK_1: using network backup set from service orcl
  183. destination for restore of datafile 00004: /oradata/ORCL_STBY/undotbs01.dbf
  184. channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
  185. channel ORA_DISK_1: starting incremental datafile backup set restore
  186. channel ORA_DISK_1: using network backup set from service orcl
  187. destination for restore of datafile 00007: /oradata/ORCL_STBY/users01.dbf
  188. channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
  189. channel ORA_DISK_1: starting incremental datafile backup set restore
  190. channel ORA_DISK_1: using network backup set from service orcl
  191. destination for restore of datafile 00009: /oradata/ORCL_STBY/PDB01/o1_mf_system_j749f5d5_.dbf
  192. channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
  193. channel ORA_DISK_1: starting incremental datafile backup set restore
  194. channel ORA_DISK_1: using network backup set from service orcl
  195. destination for restore of datafile 00010: /oradata/ORCL_STBY/PDB01/o1_mf_sysaux_j749f5fw_.dbf
  196. channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
  197. channel ORA_DISK_1: starting incremental datafile backup set restore
  198. channel ORA_DISK_1: using network backup set from service orcl
  199. destination for restore of datafile 00011: /oradata/ORCL_STBY/PDB01/o1_mf_undotbs1_j749f5fx_.dbf
  200. channel ORA_DISK_1: restore complete, elapsed time: 00:00:02
  201. channel ORA_DISK_1: starting incremental datafile backup set restore
  202. channel ORA_DISK_1: using network backup set from service orcl
  203. destination for restore of datafile 00012: /oradata/ORCL_STBY/test01.dbf
  204. channel ORA_DISK_1: restore complete, elapsed time: 00:00:01
  205. starting media recovery
  206. media recovery complete, elapsed time: 00:00:00
  207. Finished recover at 19-APR-21
  208. Executing: alter system set standby_file_management=auto
  209. Finished recover at 19-APR-21
  210. -----------------------------------

2、rename standby log

—刷新过后,redo log路径已修改,standby log路径未修改。

  1. --刷新过后,redo log路径已修改,standby log路径未修改。
  2. SQL> select member from v$logfile;
  3. MEMBER
  4. --------------------------------------------------------------------------------
  5. /oradata/ORCL_STBY/redo03.log
  6. /oradata/ORCL_STBY/redo02.log
  7. /oradata/ORCL_STBY/redo01.log
  8. /oradata/ORCL/standby_redo04.log
  9. /oradata/ORCL/standby_redo05.log
  10. /oradata/ORCL/standby_redo06.log
  11. /oradata/ORCL/standby_redo07.log
  12. --rename tempfile && logfile
  13. alter system set standby_file_management=MANUAL;
  14. --clear log
  15. alter database clear logfile group 4;
  16. alter database clear logfile group 5;
  17. alter database clear logfile group 6;
  18. alter database clear logfile group 7;
  19. --standby log
  20. alter database rename file '/oradata/ORCL/standby_redo04.log' to '/oradata/ORCL_STBY/standby_redo04.log';
  21. alter database rename file '/oradata/ORCL/standby_redo05.log' to '/oradata/ORCL_STBY/standby_redo05.log';
  22. alter database rename file '/oradata/ORCL/standby_redo06.log' to '/oradata/ORCL_STBY/standby_redo06.log';
  23. alter database rename file '/oradata/ORCL/standby_redo07.log' to '/oradata/ORCL_STBY/standby_redo07.log';
  24. -----------------------------------

3、主库切日志,备库开启日志应用

  1. --主库需要切几次归档
  2. ALTER SYSTEM ARCHIVE LOG CURRENT;
  3. 或者
  4. ALTER SYSTEM SWITCH LOGFILE;
  5. --开启备库应用日志
  6. alter database open;
  7. alter pluggable database all open;
  8. ALTER DATABASE RECOVER MANAGED STANDBY DATABASE USING CURRENT LOGFILE DISCONNECT FROM SESSION;
  9. -----------------------------------

4、测试同步情况

  1. --查看standby日志是否正常
  2. set line222
  3. col member for a60
  4. select t1.group#,t1.thread#,t1.bytes/1024/1024,t1.status,t2.member from gv$standby_log t1,gv$logfile t2 where t1.group#=t2.group#;
  5. GROUP# THREAD# T1.BYTES/1024/1024 STATUS MEMBER
  6. ---------- ---------- ------------------ ---------- ------------------------------------------------------------
  7. 4 1 120 ACTIVE /oradata/ORCL/standby_redo04.log
  8. 5 1 120 UNASSIGNED /oradata/ORCL/standby_redo05.log
  9. 6 1 120 UNASSIGNED /oradata/ORCL/standby_redo06.log
  10. 7 1 120 UNASSIGNED /oradata/ORCL/standby_redo07.log
  11. --主库插入数据
  12. sqlplus test/test@pdb01
  13. insert into test values (999);
  14. commit;
  15. --备库查询
  16. SQL> alter session set container=pdb01;
  17. Session altered.
  18. SQL> select * from test.test;
  19. ID
  20. ----------
  21. 1
  22. 2
  23. 999
  24. 999
  25. 233
  26. 7788
  27. 6 rows selected.
  28. --备库已同步