11.2.0.4 Dataguard臨時讀寫三種方法

你好我是李白發表於2020-07-26

包含以下三部分內容,都可以實現物理備庫臨時讀寫以及再恢復為physical standby:

  • flashback database閃回physical standby

  • activate standby database

  • snapshot standby

1. 在physical standby閃回恢復主庫誤運算元據

只要Flashback dest空間跟歸檔在,可以任意閃回。

1.1 查詢當前primary SCN

SYS@honor1 > select to_char(current_scn) from v$database;
TO_CHAR(CURRENT_SCN)
----------------------------------------
17031266

1.2 primary建立測試表

HR@honor1 > create table test_flashback parallel as select * from user_objects;
HR@honor1 > insert into test_flashback parallel select * from user_objects;
HR@honor1 > commit;

1.3 備庫查詢測試表,閃回到建立該表前

SYS@honordg > desc hr.test_flashback;    # 檢查,備庫已經同步該表
SYS@honordg > alter database recover managed standby database cancel;
SYS@honordg > shutdown immediate;
SYS@honordg > startup mount;
SYS@honordg > flashback database to scn 17031266;
Flashback complete.
21:03:11 SYS@honordg > alter database open read only;
Database altered.
HR@honordg > desc test_flashback;     # 可以看到已經閃回到建立該表前。
ERROR:
ORA-04043: object test_flashback does not exist

1.4 恢復physical standby

# 檢查備庫當前狀態

SYS@honordg > select GUARD_STATUS,OPEN_MODE,STANDBY_BECAME_PRIMARY_SCN,SWITCHOVER_STATUS,DATABASE_ROLE from v$database;
GUARD_S OPEN_MODE                          STANDBY_BECAME_PRIMARY_SCN SWITCHOVER_STATUS    DATABASE_ROLE
------- -------------------- ---------------------------------------- -------------------- ----------------
NONE    READ ONLY                                                   0 NOT ALLOWED          PHYSICAL STANDBY
SYS@honordg > select to_char(current_scn) from v$database;
TO_CHAR(CURRENT_SCN)
----------------------------------------
17031266

# 直接開啟恢復,備庫即可利用歸檔

SYS@honordg > alter database recover managed standby database disconnect from session;
Database altered.

1.5 透過alert詳解閃回以及恢復physical standby過程

(1)可以看到physical standby已經恢復sequence 914日誌

Archived Log entry 39 added for thread 1 sequence 914 rlc 984508005 ID 0x150ac660 dest 2:
RFS[2]: No standby redo logfiles created
RFS[2]: Opened log for thread 1 sequence 915 dbid 353046371 branch 984508005
Tue Jun 09 20:30:02 2020
Media Recovery Log +DGFRA/honordg/archivelog/2020_06_09/thread_1_seq_914.493.1042661953
RFS[1]: Opened log for thread 1 sequence 915 dbid 353046371 branch 984508005
RFS[2]: Assigned to RFS process 14109
RFS[2]: No standby redo logfiles created
Archived Log entry 40 added for thread 1 sequence 915 rlc 984508005 ID 0x150ac660 dest 2:
RFS[2]: Opened log for thread 1 sequence 916 dbid 353046371 branch 984508005
Tue Jun 09 21:02:12 2020

(2)執行閃回,可以看到利用閃回日誌以及歸檔閃回到指定scn

flashback database to scn 17031266
Flashback Restore Start
Flashback Restore Complete
Flashback Media Recovery Start
Serial Media Recovery started
Flashback Media Recovery Log +DGFRA/honordg/archivelog/2020_06_09/thread_1_seq_912.498.1042661497
Flashback Media Recovery Log +DGFRA/honordg/archivelog/2020_06_09/thread_1_seq_913.467.1042661953
Flashback Media Recovery Log +DGFRA/honordg/archivelog/2020_06_09/thread_1_seq_914.493.1042661953
Incomplete Recovery applied until change 17031267 time 06/18/2020 10:47:12
Flashback Media Recovery Complete
Completed: flashback database to scn 17031266

(3)恢復physical standby

# 可以看到會再次利用sequence 914恢復physical standby

Physical standby database opened for read only access.
Completed: alter database open read only
Tue Jun 09 21:03:28 2020
db_recovery_file_dest_size of 9216 MB is 17.72% used. This is a
user-specified limit on the amount of space that will be used by this
database for recovery-related files, and does not reflect the amount of
space available in the underlying filesystem or ASM diskgroup.
Tue Jun 09 21:06:41 2020
alter database recover managed standby database disconnect from session
Attempt to start background Managed Standby Recovery process (honordg)
Tue Jun 09 21:06:41 2020
MRP0 started with pid=34, OS id=14273 
MRP0: Background Managed Standby Recovery process started (honordg)
Serial Media Recovery started
Managed Standby Recovery not using Real Time Apply
Waiting for all non-current ORLs to be archived...
All non-current ORLs have been archived.
Media Recovery Log +DGFRA/honordg/archivelog/2020_06_09/thread_1_seq_914.493.1042661953
Completed: alter database recover managed standby database disconnect from session
Media Recovery Log +DGFRA/honordg/archivelog/2020_06_09/thread_1_seq_915.494.1042664529
Media Recovery Waiting for thread 1 sequence 916 (in transit)

2. 臨時啟用轉換為讀寫庫

2.1 取消日誌應用

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

2.2 建立guarantee還原點

SYS@honordg > create restore point rst_guar guarantee flashback database;
Restore point created.
SYS@honordg > select scn,name,guarantee_flashback_database,preserved from v$restore_point;
                                     SCN NAME                                                                   GUA PRE
---------------------------------------- ---------------------------------------------------------------------- --- ---
                                17517039 RST_GUAR                                                               YES YES

2.3 啟用備庫,啟用後庫由read only轉化為mount

SYS@honordg > ALTER DATABASE ACTIVATE PHYSICAL STANDBY DATABASE;
Database altered.
SYS@honordg > select database_role,open_mode from v$database;
DATABASE_ROLE    OPEN_MODE
---------------- --------------------
PRIMARY          MOUNTED
SYS@honordg > select scn,name,guarantee_flashback_database,preserved from v$restore_point;
                                     SCN NAME                                                                   GUA PRE
---------------------------------------- ---------------------------------------------------------------------- --- ---
                                17517039 RST_GUAR                                                               YES YES

2.4 開啟資料庫,建立測試資料

SYS@honordg > alter database open;
Database altered.
SYS@honordg > select database_role,open_mode from v$database;
DATABASE_ROLE    OPEN_MODE
---------------- --------------------
PRIMARY          READ WRITE
SYS@honordg > create table hr.test_guarantee as select * from dba_tables;
Table created.
SYS@honordg > select count(1) from hr.test_guarantee;
                                COUNT(1)
----------------------------------------
                                    3024

2.5 還原physical standby

(1)關閉資料庫,啟動到mount

SYS@honordg > shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
SYS@honordg > startup mount;
ORACLE instance started.
Total System Global Area                                521936896 bytes
Fixed Size                                                2254824 bytes
Variable Size                                           377489432 bytes
Database Buffers                                        138412032 bytes
Redo Buffers                                              3780608 bytes
Database mounted.

# 閃回還原點

SYS@honordg > flashback database to restore point RST_GUAR;
Flashback complete.

# 檢視測試資料

SYS@honordg > alter database open;    #閃回之後,需要執行convert語句轉換為physical standby
alter database open
*
ERROR at line 1:
ORA-01589: must use RESETLOGS or NORESETLOGS option for database open
SYS@honordg > select database_role,open_mode from v$database;
DATABASE_ROLE    OPEN_MODE
---------------- --------------------
PRIMARY          MOUNTED
SYS@honordg > alter database convert to physical standby;
Database altered.

# 轉換後資料庫處於nomount階段

SYS@honordg > select status from v$instance;
STATUS
----------
STARTED
SYS@honordg > shutdown immediate;
ORA-01507: database not mounted
ORACLE instance shut down.
SYS@honordg > startup;
ORACLE instance started.
Total System Global Area                                521936896 bytes
Fixed Size                                                2254824 bytes
Variable Size                                           377489432 bytes
Database Buffers                                        138412032 bytes
Redo Buffers                                              3780608 bytes
Database mounted.
Database opened.

# 開啟日誌應用

SYS@honordg > alter database recover managed standby database disconnect from session;
Database altered.

# 檢查資料庫狀態角色

SYS@honordg > select database_role,open_mode from v$database;
DATABASE_ROLE    OPEN_MODE
---------------- --------------------
PHYSICAL STANDBY READ ONLY WITH APPLY

3. 利用snapshot database,臨時讀寫備庫

3.1 檢視physical狀態,是否開啟閃回

SYS@honordg > select * from v$restore_point;
no rows selected
     
SYS@honordg > show parameter recovery;
NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
db_recovery_file_dest                string      +DGFRA
db_recovery_file_dest_size           big integer 9G
recovery_parallelism                 integer     0
SYS@honordg > select flashback_on from v$database;
FLASHBACK_ON
------------------
YES
SYS@honordg > select database_role,open_mode from v$database; 
DATABASE_ROLE    OPEN_MODE
---------------- --------------------
PHYSICAL STANDBY READ ONLY WITH APPLY

3.2 取消日誌應用, 轉換為snapshot standby database

SYS@honordg > alter database recover managed standby database cancel;
Database altered.
SYS@honordg > alter database convert to snapshot standby;
Database altered.
SYS@honordg > select database_role,open_mode from v$database; 
DATABASE_ROLE    OPEN_MODE
---------------- --------------------
SNAPSHOT STANDBY MOUNTED
SYS@honordg > alter database open;
Database altered.

3.3 檢查physical standby當前狀態

SYS@honordg > select database_role,open_mode from v$database; 
DATABASE_ROLE    OPEN_MODE
---------------- --------------------
SNAPSHOT STANDBY READ WRITE
SYS@honordg > select scn,name,guarantee_flashback_database,preserved from v$restore_point;
           SCN NAME                                                                   GUA PRE
-------------- ---------------------------------------------------------------------- --- ---
      17034110 SNAPSHOT_STANDBY_REQUIRED_06/09/2020 21:36:15                          YES YES

3.4 臨時讀寫

SYS@honordg > create table hr.test_snapshot as select * from dba_users;
Table created.
SYS@honordg > select count(*) from hr.test_snapshot;
                                COUNT(*)
----------------------------------------
                                      44

 3 .5 恢復physical standby

(1)檢查狀態,轉換為physical standby

SYS@honordg > shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
SYS@honordg > startup mount;
ORACLE instance started.
Total System Global Area                                521936896 bytes
Fixed Size                                                2254824 bytes
Variable Size                                           377489432 bytes
Database Buffers                                        138412032 bytes
Redo Buffers                                              3780608 bytes
Database mounted.
SYS@honordg > select database_role,open_mode from v$database;
DATABASE_ROLE    OPEN_MODE
---------------- --------------------
SNAPSHOT STANDBY MOUNTED
Elapsed: 00:00:00.02
SYS@honordg > select flashback_on from v$database;
FLASHBACK_ON
------------------
YES
Elapsed: 00:00:00.00
SYS@honordg > alter database convert to physical standby;
Database altered.
SYS@honordg > select status from v$instance;      # 可以看到轉換完成資料庫處於nomount狀態
STATUS
----------
STARTED
SYS@honordg > shutdown immediate;
ORA-01507: database not mounted
ORACLE instance shut down.
SYS@honordg > startup;
ORACLE instance started.
Total System Global Area                                521936896 bytes
Fixed Size                                                2254824 bytes
Variable Size                                           377489432 bytes
Database Buffers                                        138412032 bytes
Redo Buffers                                              3780608 bytes
Database mounted.
Database opened.
SYS@honordg > alter database recover managed standby database disconnect from session;
Database altered.

# 檢視之前讀寫資料,發現已經清除

SYS@honordg > select count(*) from hr.test_snapshot;
select count(*) from hr.test_snapshot
                        *
ERROR at line 1:
ORA-00942: table or view does not exist

# 檢查alert日誌,可以發現,snapshot standby是利用還原點restore point完成恢復

Flashback Restore Start
Flashback Restore Complete
Drop guaranteed restore point 
Guaranteed restore point  dropped



來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31439444/viewspace-2707018/,如需轉載,請註明出處,否則將追究法律責任。

相關文章