签到成功

知道了

CNDBA社区CNDBA社区

Oracle 19c(19.6) Data Guard 备库使用闪回数据库(Flashback database)测试

2020-03-15 08:43 5217 0 原创 Oracle 19c
作者: dave

1 测试背景说明

之前的博客已经对Oracle 的闪回技术进行说明,如下:

Oracle Flashback 技术 总结
https://www.cndba.cn/Dave/article/1276

我们现在用的最多的还是闪回查询,但闪回查询有时间限制,超过了时间就无法查询到数据,此时就需要通过备份来查找需要的数据。 显然用备份的方式会麻烦很多。

在Oracle 数据库中,默认是不启用Flashback Database功能的,因为会带来一些性能开销,主要是闪回数据库依赖的闪回日志。实际上,从技术发展的角度来说,现在的硬件性能水平已经取得了很大的进步,尤其是闪存卡和CPU性能的提升,在当前的硬件条件下,起用闪回数据库带来的开销是可以接受的。

尤其是在Data Guard 架构中,备库最适合使用Flashback Database 功能的。在DG 架构中,备库是只读的,以下两种情况是备库启用闪回数据库的优势:

1)主库出现误操作,发现时已经过去了几天,超过了闪回查询的时间。 此时如果备库有闪回数据库功能,完全可以通过功能找回之前的数据,而不需要通过恢复备份来查找。
2)业务测试场景。 如果要对业务进行测试,可以临时将主库切换成读写模式进行测试,测试完毕之后在将数据库闪回到测试之前,继续进行数据库同步。 当然在Oracle 11g中也可以利用快照standby 来实现。

Flashback Database 整个架构包括一个进程Recover Writer(RVWR)后台进程,Flashback Database Log日志 和Flash Recovery Area。一旦数据库启用了Flashback Database, 则RVWR进程会启动,该进程会向Flash Recovery Area中写入Flashback Database Log, 这些日志包括的是数据块的 “ 前镜像(before image)”, 通过Flashback log 日志可以把整个数据库回退到过去的某个时点的状态。

[oracle@www.cndba.cn_1 ~]$ ps -ef|grep rvwr
oracle   107622      1  0 08:32 ?        00:00:00 ora_rvwr_cndba
oracle   109398 101805  0 08:38 pts/0    00:00:00 grep --color=auto rvwr
[oracle@www.cndba.cn_1 ~]$

2 DG 备库使用闪回数据库测试

2.1 搭建19c 的DG 环境

具体搭建过程参考之前的博客,这里不再描述:http://www.cndba.cn/cndba/dave/article/4084

Linux 7.7 平台 Oracle 19.3 物理Data Guard 搭建手册
https://www.cndba.cn/dave/article/4067
Linux 7.7 平台 Oracle 19c Data Guard 环境 升级RU 19.3 到19.6 操作手册
https://www.cndba.cn/dave/article/4068

http://www.cndba.cn/cndba/dave/article/4084


[oracle@www.cndba.cn_2 ~]$ sqlplus / as sysdba

SQL*Plus: Release 19.0.0.0.0 - Production on Sat Mar 14 20:47:50 2020
Version 19.6.0.0.0

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


Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.6.0.0.0

SQL> select process,status,sequence# from v$managed_standby;

PROCESS                     STATUS                                SEQUENCE#
--------------------------- ------------------------------------ ----------
ARCH                        CLOSING                                      22
DGRD                        ALLOCATED                                     0
DGRD                        ALLOCATED                                     0
ARCH                        CLOSING                                      33
ARCH                        CLOSING                                      32
ARCH                        CLOSING                                      31
RFS                         IDLE                                          0
RFS                         IDLE                                         34
RFS                         IDLE                                          0
MRP0                        APPLYING_LOG                                 34
RFS                         IDLE                                          0

11 rows selected.

SQL>

2.2 启用配置闪回恢复区

配置闪回恢复区需要设置3个参数,如下:

1.db_recovery_file_dest:指定闪回恢复区的位置。
2.db_recovery_file_dest_size:指定闪回恢复区的可用空间大小。
3.db_flashback_retention_target:指定数据库可以回退的时间,单位为分钟,默认1440分钟(一天)。实际上可回退的时间还决定于闪回恢复区的大小,因为里面保存了回退所需要的flashback log。

创建实例如果没有启用闪回恢复区,配置以上三个参数后即表示启用FRA。http://www.cndba.cn/cndba/dave/article/4084


[oracle@www.cndba.cn_2 ~]$ sqlplus / as sysdba
SQL*Plus: Release 19.0.0.0.0 - Production on Sat Mar 14 21:43:10 2020
Version 19.6.0.0.0

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


Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.6.0.0.0


Session altered.

SYS@cndba>show parameter db_recovery

NAME                                 TYPE                              VALUE
------------------------------------ --------------------------------- ----------------------------------------------------------------------
db_recovery_file_dest                string                            /u01/app/oracle/fast_recovery_area
db_recovery_file_dest_size           big integer                       12732M
SYS@cndba>

SYS@cndba>alter system set db_recovery_file_dest_size=100G scope=both;
System altered.

SYS@cndba>show parameter db_flashback
NAME                                 TYPE                              VALUE
------------------------------------ --------------------------------- ----------------------------------------------------------------------
db_flashback_retention_target        integer                           1440
SYS@cndba>

这里的时间和数据库变化量有关,如果时间设置的比较长,且数据库变化量比较大,那么就设置比较大的db_recovery_file_dest_size。 这里可以简答的预估一下,比如100G的库,每天变化10%,则需要10g的空间。

我们这里设置为10天:


SYS@cndba>alter system set db_flashback_retention_target=14400 scope=both;

System altered.

2.3 启用Flashback Database功能

数据库默认不启用Flashback Database,在配置闪回恢复区之后,就可以启用改功能。 这里还有一个前提,就是数据库必须是归档模式。

启用闪回功能数据库不用切换状态,直接在open状态执行即可。 但使用闪回数据库时会自动关闭数据库,并启动到mount状态。


SYS@cndba>archive log list;
Database log mode              Archive Mode
Automatic archival             Enabled
Archive destination            /u01/archivelog/
Oldest online log sequence     0
Next log sequence to archive   0
Current log sequence           0
SYS@cndba>


SYS@cndba>select open_mode from v$database;

OPEN_MODE
------------------------------------------------------------
READ ONLY WITH APPLY

SYS@cndba>alter database flashback on;
alter database flashback on
*
ERROR at line 1:
ORA-01153: an incompatible media recovery is active

--取消MRP:
SYS@cndba>alter database recover managed standby database cancel;
Database altered.

--启用Flashback database:
SYS@cndba>alter database flashback on;

Database altered.

--启用MRP:
SYS@cndba> alter database recover managed standby database disconnect;

Database altered.

2.4 Flashback Database 相关的视图

2.4.1 V$database

查看是否启用了Flashback database功能


SYS@cndba>select flashback_on from v$database;

FLASHBACK_ON
------------------------------------------------------
YES

2.4.2 V$flashback_database_log

Flashback Database 所能回退到的最早时间,取决与保留的Flashback Database Log 的多少, 该视图就可以查看许多有用的信息。

http://www.cndba.cn/cndba/dave/article/4084

Oldest_flashback_scn / Oldest_flashback_time : 这两列用来记录可以恢复到最早的时点
Flashback_size: 记录了当前使用的Flash Recovery Area 空间的大小
Retention_target: 系统定义的策略
Estimated_flashback_size: 根据策略对需要的空间大小的估计值http://www.cndba.cn/cndba/dave/article/4084


SYS@cndba>select oldest_flashback_scn os, to_char(oldest_flashback_time,'yy-mm-dd hh24:mi:ss') ot, retention_target rt,flashback_size fs, estimated_flashback_size es  from v$flashback_database_log;

        OS OT                                                          RT         FS         ES
---------- --------------------------------------------------- ---------- ---------- ----------
   2864851 20-03-14 21:55:47                                        14400  419430400          0

SYS@cndba>

2.4.3 V$flashback_database_stat

该视图用来对Flashback log 空间情况进行更细粒度的记录和估计。 这个视图以小时为单位记录单位时间内数据库的活动量:

Flashback_Data 代表Flashback log产生数量,
DB_Date 代表数据改变数量,
Redo_Date代表日志数量,

http://www.cndba.cn/cndba/dave/article/4084

通过这3个数量可以反映出数据的活动特点,更准确的预计Flash Recovery Area的空间需求


SYS@cndba>select *from v$flashback_database_stat;

BEGIN_TIME          END_TIME            FLASHBACK_DATA    DB_DATA  REDO_DATA ESTIMATED_FLASHBACK_SIZE     CON_ID
------------------- ------------------- -------------- ---------- ---------- ------------------------ ----------
2020-03-14 21:55:54 2020-03-14 22:17:16        9224192   13877248          0                        0          0

2.5 Flashback Database 使用示例

2.5.1 查询备库flashback的最早支持时间


SYS@cndba>select oldest_flashback_scn os, to_char(oldest_flashback_time,'yy-mm-dd hh24:mi:ss') ot, retention_target rt,flashback_size fs, estimated_flashback_size es  from v$flashback_database_log;

        OS OT                                                          RT         FS         ES
---------- --------------------------------------------------- ---------- ---------- ----------
   2864851 20-03-14 21:55:47                                        14400  419430400          0

SYS@cndba>

2.5.2 主库创建测试表


[oracle@www.cndba.cn_1 ~]$ sqlplus / as sysdba

SQL*Plus: Release 19.0.0.0.0 - Production on Sun Mar 15 07:57:59 2020
Version 19.6.0.0.0

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


Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.6.0.0.0

SQL> create table cndba as select * from sys_objects;

Table created.

SQL> select count(1) from cndba;

  COUNT(1)
----------
      6291

SQL>

2.5.3 查询主库当前的scn和时间


SQL> select current_scn from v$database;

CURRENT_SCN
-----------
    2906364

SQL> select to_char(sysdate,'yy-mm-dd hh24:mi:ss') time from dual;

TIME
---------------------------------------------------
20-03-15 08:00:23

SQL>

2.5.4 验证主备库同步情况(备库执行):


SQL>  select max(SEQUENCE#) from v$archived_log where APPLIED='YES';

MAX(SEQUENCE#)
--------------
            35
SYS@cndba>select count(1) from cndba;

  COUNT(1)
----------
      6291

2.5.5 删除刚创建主库测试表的数据


SQL> delete from cndba;
6291 rows deleted.
SQL> commit;
Commit complete.


备库查询也被删除了:
SYS@cndba>select count(1) from cndba;

  COUNT(1)
----------
         0

2.5.6 使用闪回数据库恢复

如果测试时间超过了Flashback Table的时间,那么我们可以利用备库的Flashback database 功能来实现数据恢复。

这里需要先记录我们操作的时间或者SCN, 或者从alert log里查询,因为操作结束之后,我们还需要将数据库闪回到这个点,继续同步数据。http://www.cndba.cn/cndba/dave/article/4084


SQL> select current_scn from v$database;
SQL> select to_char(sysdate,'yy-mm-dd hh24:mi:ss') time from dual;


SYS@cndba>flashback database to scn 2906364;
flashback database to scn 2906364
*
ERROR at line 1:
ORA-01153: an incompatible media recovery is active

SYS@cndba>alter database recover managed standby database cancel;
Database altered.

--基于SCN:
SYS@cndba>flashback database to scn 2906364;
Flashback complete.

--基于timestamp :
SYS@cndba>flashback database to timestamp to_timestamp('20-03-15 08:00:23','yy-mm-dd hh24:mi:ss');

Flashback complete.

查看alert log 日志,里面有详细的过程:

http://www.cndba.cn/cndba/dave/article/4084
http://www.cndba.cn/cndba/dave/article/4084


Errors in file /u01/app/oracle/diag/rdbms/cndba_s/cndba/trace/cndba_pr00_46656.trc:
ORA-16037: user requested cancel of managed recovery operation
2020-03-15T08:09:32.790097+08:00
Background Media Recovery process shutdown (cndba)
2020-03-15T08:09:33.545125+08:00
Managed Standby Recovery Canceled (cndba)
Completed: alter database recover managed standby database cancel
2020-03-15T08:09:37.975686+08:00
flashback database to scn 2906364
2020-03-15T08:09:38.984317+08:00
Stopping background process MMNL
2020-03-15T08:09:39.984756+08:00
Stopping background process MMON
2020-03-15T08:09:40.996447+08:00
Stopping Emon pool
Dispatchers and shared servers shutdown
CLOSE: killing server sessions.
2020-03-15T08:09:41.205359+08:00
Process termination requested for pid 41235 [source = rdbms], [info = 2] [request issued by pid: 70195, uid: 54321]
……
2020-03-15T08:09:44.290147+08:00
CLOSE: all sessions shutdown successfully.
alter pluggable database all close
DAVE(3):JIT: pid 70195 requesting stop
DAVE(3):Buffer Cache flush deferred for PDB 3
Pluggable database DAVE closed
Completed: alter pluggable database all close
PDB$SEED(2):JIT: pid 70195 requesting stop
PDB$SEED(2):Buffer Cache flush deferred for PDB 2
2020-03-15T08:09:45.469478+08:00

IM on ADG: Start of Empty Journal

IM on ADG: End of Empty Journal
Stopping Emon pool
Buffer Cache invalidation for all PDBs started
Buffer Cache invalidation for all PDBs complete
Flashback Restore Start
Flashback Restore Complete
Flashback Media Recovery Start
 Started logmerger process
2020-03-15T08:09:46.105963+08:00
max_pdb is 3
2020-03-15T08:09:46.204649+08:00
Parallel Media Recovery started with 4 slaves
2020-03-15T08:09:46.249296+08:00
stopping change tracking
2020-03-15T08:09:46.402100+08:00
Media Recovery of Online Log [Thread=1, Seq=36]
2020-03-15T08:09:46.402719+08:00
Recovery of Online Redo Log: Thread 1 Group 5 Seq 36 Reading mem 0
  Mem# 0: /u01/app/oracle/oradata/CNDBA_S/onlinelog/o1_mf_5_h6h3bzdn_.log
  Mem# 1: /u01/app/oracle/fast_recovery_area/CNDBA_S/onlinelog/o1_mf_5_h6h3bznl_.log
(3):Resize operation completed for file# 9, old size 307200K, new size 317440K
2020-03-15T08:09:46.676437+08:00
Incomplete Recovery applied until change 2906365 time 03/15/2020 08:00:17
2020-03-15T08:09:46.678177+08:00
Flashback Media Recovery Complete
2020-03-15T08:09:46.724360+08:00
stopping change tracking
Completed: flashback database to scn 2906364
2020-03-15T08:11:40.699148+08:00
flashback database to timestamp to_timestamp('20-03-15 08:00:23','yy-mm-dd hh24:mi:ss')
2020-03-15T08:11:40.710580+08:00
Flashback Restore Start
Flashback Restore Complete
Flashback Media Recovery Start
 Started logmerger process
2020-03-15T08:11:40.854987+08:00
max_pdb is 3
2020-03-15T08:11:40.905168+08:00
Parallel Media Recovery started with 4 slaves
2020-03-15T08:11:40.932479+08:00
stopping change tracking
2020-03-15T08:11:40.983757+08:00
Media Recovery of Online Log [Thread=1, Seq=36]
2020-03-15T08:11:40.983998+08:00
Recovery of Online Redo Log: Thread 1 Group 5 Seq 36 Reading mem 0
  Mem# 0: /u01/app/oracle/oradata/CNDBA_S/onlinelog/o1_mf_5_h6h3bzdn_.log
  Mem# 1: /u01/app/oracle/fast_recovery_area/CNDBA_S/onlinelog/o1_mf_5_h6h3bznl_.log
(3):Resize operation completed for file# 9, old size 307200K, new size 317440K
2020-03-15T08:11:41.249884+08:00
Incomplete Recovery applied until change 2906379 time 03/15/2020 08:00:24
2020-03-15T08:11:41.251548+08:00
Flashback Media Recovery Complete
2020-03-15T08:11:41.318909+08:00
stopping change tracking
Completed: flashback database to timestamp to_timestamp('20-03-15 08:00:23','yy-mm-dd hh24:mi:ss')

2.5.7 open 数据库

在执行闪回之前我们数据库是只读模式的,执行闪回之后,现在成了mounted模式:


SYS@cndba>select open_mode from v$database;

OPEN_MODE
------------------------------------------------------------
MOUNTED

SYS@cndba>

--只读模式打开,可以查询到我们之前删除的cndba表的数据了:
SYS@cndba>alter database open read only;

Database altered.

SYS@cndba>select count(1) from cndba;

  COUNT(1)
----------
      6291

SYS@cndba>

2.5.8 启动MRP

如果闪回的时间不长,对应的归档还在,那么直接启动MRP,追加备库的新数据,完成同步即可。


SYS@cndba> alter database recover managed standby database disconnect;

Database altered.

SYS@cndba>select open_mode from v$database;

OPEN_MODE
------------------------------------------------------------
READ ONLY WITH APPLY

SYS@cndba>

SYS@cndba>select sequence#,applied from v$archived_log;

 SEQUENCE# APPLIED
---------- ---------------------------
……
        34 YES
        37 YES
        36 YES

27 rows selected.

SYS@cndba>select max(SEQUENCE#) from v$archived_log where APPLIED='YES';

MAX(SEQUENCE#)
--------------
            37

如果闪回的时间太久,备库的归档已经删除了,或者需要应用的归档太多,可以先闪回到我们执行Flashback 数据库的时间,在启动MRP 进程,从而缩短执行应用备库日志的时间。

这里要注意,如果是在主库操作,闪回到之前,还是需要闪回到当前的时间,这里可以记录闪回操作的时间,闪回到最开始指定的时间,或者通过recover database 应用归档和redo ,数据同步之后,最后再以open resetlogs 打开。http://www.cndba.cn/cndba/dave/article/4084

版权声明:本文为博主原创文章,未经博主允许不得转载。

用户评论
* 以下用户言论只代表其个人观点,不代表CNDBA社区的观点或立场
dave

dave

关注

人的一生应该是这样度过的:当他回首往事的时候,他不会因为虚度年华而悔恨,也不会因为碌碌无为而羞耻;这样,在临死的时候,他就能够说:“我的整个生命和全部精力,都已经献给世界上最壮丽的事业....."

  • 2296
    原创
  • 3
    翻译
  • 582
    转载
  • 198
    评论
  • 访问:8513288次
  • 积分:4477
  • 等级:核心会员
  • 排名:第1名
精华文章
    最新问题
    查看更多+
    热门文章
      热门用户
      推荐用户
        Copyright © 2016 All Rights Reserved. Powered by CNDBA · 皖ICP备2022006297号-1·

        QQ交流群

        注册联系QQ