Oracle DB閃回(Flashback database)開啓記

這兩天要爲測試部門搭建一臺Oracle DB,需知足能夠常常將整個數據庫回退到某時間點的需求。比較簡單的方法是使用Oracle的閃回特性之一"閃回數據庫"。

預備知識:閃回數據庫須要使用兩種日誌:閃回日誌和重作日誌。重作日誌的概念不需多談,所謂的閃回日誌的記錄正好與重作日誌的記錄相反。能夠簡單的這樣認爲:好比重作日誌記錄了一條insert命令,那麼閃回日誌就記錄這條記錄相關的delete命令,固然實際上這是很是複雜的過程。

這裏並不會介紹「閃回數據庫」的原理,由於已經有太多的資料來闡明它,也不會介紹「閃回數據庫」的操做方法,由於它很是簡單(在進入MOUNT狀態後執行"flashback database to scn …"或"flashback database to timestamp …")。

1. 開啓DB閃回以前需確保啓用了歸檔模式
SQL> archive log list
Database log mode              Archive Mode
Automatic archival             Enabled
……

2. 查看閃回日誌是否已啓用 sql

SQL> select LOG_MODE,FLASHBACK_ON from v$database;

LOG_MODE   FLASHBACK_ON
---------- ---------------
ARCHIVELOG NO


3. 啓用閃回日誌,即閃回數據庫功能
SQL> alter database flashback on;
alter database flashback on
*
ERROR at line 1:
ORA-38706: Cannot turn on FLASHBACK DATABASE logging.
ORA-38709: Recovery Area is not enabled.
報錯,沒法開啓閃回功能。到這裏本文的主角--"oerr"命令君隆重登場。

4. 根據上文中提示的錯誤號使用oerr命令排錯
$ oerr ORA 38709
38709, 00000, "Recovery Area is not enabled."
// *Cause:  An ALTER DATABASE FLASHBACK ON command failed because the
//          Recovery Area was not enabled.
// *Action: Set DB_RECOVERY_FILE_DEST to a location and retry.
5.  從上述的提示中找到錯誤緣由:DB_RECOVERY_FILE_DEST 沒有預設
SQL> show parameter DB_RECOVERY_FILE_DEST

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
db_recovery_file_dest                string
db_recovery_file_dest_size           big integer 0
6.  設置DB_RECOVERY_FILE_DEST
SQL> alter system set db_recovery_file_dest='/opt/oracle/fast_recovery_area';
alter system set db_recovery_file_dest='/opt/oracle/fast_recovery_area'
*
ERROR at line 1:
ORA-02097: parameter cannot be modified because specified value is invalid
ORA-19802: cannot use DB_RECOVERY_FILE_DEST without DB_RECOVERY_FILE_DEST_SIZE
7. 使用oerr命令排錯
$ oerr ORA 19802
19802, 00000, "cannot use DB_RECOVERY_FILE_DEST without DB_RECOVERY_FILE_DEST_SIZE"
// *Cause: There are two possible cause for this error:
//         1) The DB_RECOVERY_FILE_DEST parameter was in use when no
//            DB_RECOVERY_FILE_DEST_SIZE parameter was encountered while
//            fetching initialization parameter.
//         2) An attempt was made to set DB_RECOVERY_FILE_DEST with the
//            ALTER SYSTEM command when no DB_RECOVERY_FILE_DEST_SIZE
//            was in use.
// *Action: Correct the dependency parameter definitions and retry the command.
8. 從上述錯誤緣由與解決方案提示中得知須要先設置DB_RECOVERY_FILE_DEST_SIZE
SQL> alter system set DB_RECOVERY_FILE_DEST_SIZE=20G scope=both; 
System altered.


SQL> show parameter db_recovery

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
db_recovery_file_dest                string
db_recovery_file_dest_size           big integer 20G
9. 設置DB_RECOVERY_FILE_DEST
SQL> alter system set db_recovery_file_dest='/opt/oracle/fast_recovery_area' scope=both;

System altered.
10. 啓用閃回日誌,即閃回數據庫功能
SQL> alter database flashback on; 
Database altered.
此時能夠在'db_recovery_file_dest'設定的目錄中看到擴展名爲.flb的文件,它們就是閃回日誌。
$ ls
o1_mf_98mpkdl6_.flb  o1_mf_98okkcs9_.flb
11. 閃存日誌的保存期限由參數db_flashback_ retention_target控制(單位爲分鐘),凡是超出保存期限的閃回日誌將會在快速恢復區空間吃緊時被自動刪除。
SQL> show parameter db_flashback_retention_target

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
db_flashback_retention_target        integer     10080
相關文章
相關標籤/搜索