• 运用闪回功能恢复被TRUNCATE表的内容


    我们实际感受一下使用Flashback Database功能找回被TRUNCATE表的快感。

    1.Oracle数据库版本信息
    sys@secooler> select * from v$version;

    BANNER
    ---------------------------------------------------------------
    Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
    PL/SQL Release 11.2.0.1.0 - Production
    CORE    11.2.0.1.0      Production
    TNS for Linux: Version 11.2.0.1.0 - Production
    NLSRTL Version 11.2.0.1.0 - Production

    2.验证是否启用了flashback database
    sys@secooler> select flashback_on,force_logging from v$database;

    FLASHBACK_ON       FOR
    ------------------ ---
    NO                 NO

    这里显示没有开启闪回功能,同时force_logging也没有开启。

    3.开启闪回功能和force logging
    开启过程可以详细参考《【Flashback】启用Flashback闪回功能》http://space.itpub.net/?uid-519536-action-viewspace-itemid-590636
    1)关闭数据库,启动到mount状态
    sys@secooler> shutdown immediate;
    Database closed.
    Database dismounted.
    ORACLE instance shut down.

    NotConnected@> startup mount;
    ORACLE instance started.

    Total System Global Area  726540288 bytes
    Fixed Size                  2216904 bytes
    Variable Size             541068344 bytes
    Database Buffers          176160768 bytes
    Redo Buffers                7094272 bytes
    Database mounted.

    2)设置db_recovery_file_dest和db_recovery_file_dest_size参数
    NotConnected@> alter system set db_recovery_file_dest='/oracle/ora11gR2/flash_recovery_area';

    System altered.

    NotConnected@> alter system set db_recovery_file_dest_size=4g scope=spfile;

    System altered.

    NotConnected@> show parameter db_recovery_file_dest

    NAME                                     TYPE                 VALUE
    ---------------------------------------- -------------------- ------------------------------------------------------------
    db_recovery_file_dest                    string               /oracle/ora11gR2/flash_recovery_area
    db_recovery_file_dest_size               big integer          3882M

    3)重启数据库到mount状态
    NotConnected@> shutdown immediate;
    ORA-01109: database not open


    Database dismounted.
    ORACLE instance shut down.
    NotConnected@> startup mount;
    ORACLE instance started.

    Total System Global Area  726540288 bytes
    Fixed Size                  2216904 bytes
    Variable Size             541068344 bytes
    Database Buffers          176160768 bytes
    Redo Buffers                7094272 bytes
    Database mounted.

    4)开启闪回功能
    (1)第一次尝试
    NotConnected@> alter database flashback on;
    alter database flashback on
    *
    ERROR at line 1:
    ORA-38706: Cannot turn on FLASHBACK DATABASE logging.
    ORA-38707: Media recovery is not enabled.

    之所以会报此错误,是因为这个数据库没有运行在归档模式下。

    (2)将数据库修改为归档模式
    NotConnected@> alter database archivelog;

    Database altered.

    (3)再次尝试开启闪回功能,成功。
    NotConnected@> alter database flashback on;

    Database altered.

    5)OPEN数据库,
    NotConnected@> alter database open;

    Database altered.

    6)开启force logging功能
    sys@secooler> alter database force logging;

    Database altered.

    7)最后的验证
    sys@secooler> select flashback_on,force_logging from v$database;

    FLASHBACK_ON       FOR
    ------------------ ---
    YES                YES

    此时,数据库已经启用闪回功能,同时force_logging也被开启。


    4.在sec用户下创建一个T表并初始化一条数据
    sys@secooler> conn sec/sec
    Connected.
    sec@secooler> select * from tab;

    no rows selected

    sec@secooler> create table t (x varchar2(8));

    Table created.

    sec@secooler> insert into t values ('secooler');

    1 row created.

    sec@secooler> commit;

    Commit complete.

    sec@secooler> select * from t;

    X
    --------
    secooler

    5.查看当前数据库可以闪回到的时间
    sec@secooler> alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';

    Session altered.

    sec@secooler> select oldest_flashback_scn,oldest_flashback_time from v$flashback_database_log;

    OLDEST_FLASHBACK_SCN OLDEST_FLASHBACK_TI
    -------------------- -------------------
                 8443212 2010-03-17 19:26:45

    6.模拟表T被误TRUNCATE
    sec@secooler> select sysdate from dual;

    SYSDATE
    -------------------
    2010-03-17 19:47:14

    sec@secooler> truncate table t;

    Table truncated.

    7.此时数据已被删除
    sec@secooler> select * from t;

    no rows selected

    8.使用Flash Database功能
    1)关闭数据库
    sys@secooler> shutdown immediate;
    Database closed.
    Database dismounted.
    ORACLE instance shut down.

    2)开启数据库到mount exclusive状态
    sys@secooler> startup mount exclusive;
    ORACLE instance started.

    Total System Global Area  726540288 bytes
    Fixed Size                  2216904 bytes
    Variable Size             541068344 bytes
    Database Buffers          176160768 bytes
    Redo Buffers                7094272 bytes
    Database mounted.

    3)恢复到truncate表T之前的时间,注意这里时间格式的写法
    sys@secooler> Flashback Database to timestamp(to_date('2010-03-17 19:47:14','yyyy-mm-dd hh24:mi:ss'));

    Flashback complete.

    4)打开数据库到read only状态
    sys@secooler> alter database open read only;

    Database altered.

    5)到sec用户中确认,数据是否被恢复
    sys@secooler> conn sec/sec
    Connected.
    sec@secooler> select * from t;

    X
    --------
    secooler

    “激动人心”的消息:“我secooler又回来啦!”

    此时也可以以“resetlogs”的方式开启数据库(alter database open resetlogs;),不推荐这样使用,因为这样打开数据库后,恢复到的时间点之后的数据将会丢失。

    9.小结
    我们真实完整的操练了一遍Flash Database功能,它可以完美、便捷和快速的恢复被误TRUNCATE的表。在“特定场合”下可以考虑使用这个优秀的功能。
    “特定场合”:数据库的数据量不大,确认数据库确实可以恢复到指定的时间点。
  • 相关阅读:
    Id_Name
    注入类型(Injection Type)
    搭建 Spring 2.5.6 开发环境
    模拟Spring框架
    AJAX 学习笔记 2017_05_04
    Gson 转换hibernate级联对象出现StackOverFlow(堆栈溢出)问题
    页面左侧可收缩菜单案例
    struts1.3.10 和 hibernate 3.3.2 一起加载时 出现javax.servlet.ServletException: java.lang.NoSuchMethodError: antlr.collections.A 错误
    1.21 Java基础总结 数组知识
    1.20 Java基础总结 输入数据类型判断 Java低耦合原则 for嵌套思路
  • 原文地址:https://www.cnblogs.com/liuzhuqing/p/7480647.html
Copyright © 2020-2023  润新知