Oracle 11g開啟閃回功能Flashback

來源:互聯網
上載者:User

Oracle 11g開啟閃回功能Flashback

在Oracle的建庫過程中,通常會提示是否開啟閃回並指定閃回恢複區大小,我一般會選擇不開啟。這樣,如果需要使用閃回功能,就需要手動開啟。

1.環境準備
我們在Oracle11g上進行測試。

點擊(此處)摺疊或開啟

SQL> select * from v$version;
 

BANNER

--------------------------------------------------------------------------------

Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - Production

PL/SQL Release 11.2.0.3.0 - Production

CORE 11.2.0.3.0 Production

TNS for Linux: Version 11.2.0.3.0 - Production

NLSRTL Version 11.2.0.3.0 - Production

SQL>

2.查詢閃回功能是否開啟
可以通過查詢v$database視圖的flashback_on欄位來擷取閃回功能的狀態,該欄位是一個布爾類型,YES表示開啟,NO表示未開啟。
點擊(此處)摺疊或開啟

SQL>
 
SQL> select flashback_on from v$database;

FLASHBACK_ON

------------------

NO

SQL>

 3.開啟歸檔功能 在開啟閃回功能之前,我們必須首先開啟資料庫歸檔,否則資料庫會報錯。

點擊(此處)摺疊或開啟

SQL>
 
SQL> archive log list;

Database log mode No Archive Mode

Automatic archival Disabled

Archive destination /u01/app/oracle/product/11.2.0/dbhome_1/dbs/arch

Oldest online log sequence 16

Current log sequence 20

SQL>

SQL> 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.

SQL>

ORA-38706和ORA-38707兩個報錯提醒我們要開啟資料庫歸檔,步驟如下;

點擊(此處)摺疊或開啟

SQL>
 
SQL> alter database archive;

alter database archive

                    *

ERROR at line 1:

ORA-02231: missing or invalid option to ALTER DATABASE

SQL> alter database archivelog;

alter database archivelog

*

ERROR at line 1:

ORA-01126: database must be mounted in this instance and not open in any

instance

SQL> 
SQL>

SQL> shutdown immediate

Database closed.

Database dismounted.

ORACLE instance shut down.

SQL>

SQL> startup mount

ORACLE instance started.

Total System Global Area 941600768 bytes

Fixed Size 1348860 bytes

Variable Size 536873732 bytes

Database Buffers 398458880 bytes

Redo Buffers 4919296 bytes

Database mounted.

SQL>

SQL> alter database archivelog;

Database altered.

SQL>

SQL> alter database open;

Database altered.

SQL>

SQL> archive log list;

Database log mode Archive Mode

Automatic archival Enabled

Archive destination /u01/app/oracle/product/11.2.0/dbhome_1/dbs/arch

Oldest online log sequence 16

Next log sequence to archive 20

Current log sequence 20

SQL>

SQL>

4.開啟閃回功能

4.1設定參數
閃回功能和兩個初始化參數有關,我們先認識一下,其中,db_recovery_file_dest_size表示閃回恢複區大小,db_recovery_file_dest表示閃回恢複區路徑。

點擊(此處)摺疊或開啟

SQL>
 
SQL> show parameter db_recovery

NAME TYPE VALUE

------------------------------------ ----------- ------------------------------

db_recovery_file_dest string

db_recovery_file_dest_size big integer 0

SQL>
在開啟閃回功能前,必須設定這兩個參數,而且,二者還有先後順序,如果順序搞錯了系統也會報錯並給出提示資訊。

點擊(此處)摺疊或開啟

SQL>
 
SQL> alter system set db_recovery_file_dest=\'/home/oracle/flashback\';

alter system set db_recovery_file_dest=\'/home/oracle/flashback\'

*

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

SQL>

SQL> alter system set db_recovery_file_dest_size=2g;

System altered.

SQL>

SQL> alter system set db_recovery_file_dest=\'/home/oracle/flashback\';

System altered.

SQL>

SQL> show parameter db_recovery

NAME TYPE VALUE

------------------------------------ ----------- ------------------------------

db_recovery_file_dest string /home/oracle/flashback

db_recovery_file_dest_size big integer 2G

SQL>

4.2開啟閃回功能
需要注意的一點是,在10G中,如果要開啟資料庫層級的閃回,需要設定相關的參數,並且使資料庫處于歸檔模式,然後再在MOUNT狀態下開啟閃回。在11G中,如果設定了相關的參數及其開啟了歸檔,那麼可以再OPEN狀態下開啟閃回。這也算是Oracle 11g的一個新特性。

點擊(此處)摺疊或開啟

SQL>
 
SQL> select status from v$instance;

STATUS

------------

OPEN

SQL>

SQL> alter database flashback on;

Database altered.

SQL>

SQL> select flashback_on from v$database;

FLASHBACK_ON

------------------

YES

SQL>
這樣,我們就開啟了資料庫閃回功能,進而可以實現閃回資料庫等功能。

5.關閉閃回功能
關閉閃回功能十分簡單,一條語句就能搞定。

點擊(此處)摺疊或開啟

SQL>
 
SQL> alter database flashback off;

Database altered.

SQL> select flashback_on from v$database;

FLASHBACK_ON

------------------

NO

SQL>

6.總結
從上面的過程來看,需要注意幾點:

1.Oracle11g支援在Open狀態下開啟閃回功能,這一點和Oracle 10g等較早版本不一樣,算是一個新特性;
2.開啟資料庫閃回的前提條件是,開啟資料庫歸檔;
3.設定兩個初始化參數:閃回恢複區大小db_recovery_file_dest_size和閃回恢複區路徑db_recovery_file_dest,而這兩個參數是有先後順序的,必須先設定db_recovery_file_dest_size後設定db_recovery_file_dest,否則系統會報錯並給出提示;

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.