1. 程式人生 > >Data Guard主備切換

Data Guard主備切換

一、Switchover正常切換

1、主庫

(1)、檢視主庫狀態及角色

SQL> select name,database_role from v$database;

NAME   DATABASE_ROLE

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

ORCL   PRIMARY

SQL> select switchover_status from v$database;

SWITCHOVER_STATUS

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

TO STANDBY

(2)、檢視歸檔日誌是否有中斷

SQL> select status,gap_status from v$archive_dest_status where dest_id=2;

STATUS   GAP_STATUS

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

VALID   NO GAP

(3)、提前檢視備庫狀態

SQL> select switchover_status,name from v$database;

SWITCHOVER_STATUS    NAME

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

NOT ALLOWED      ORCL

(4)、切換到備庫,資料庫會自動關閉,啟動到mount狀態

SQL> alter database commit to switchover to physical standby;

Database altered.

SQL> startup mount;

ORACLE instance started.

Total System Global Area 1258291200 bytes

Fixed Size     8620224 bytes

Variable Size   872417088 bytes

Database Buffers   369098752 bytes

Redo Buffers     8155136 bytes

Database mounted.

(5)、查詢資料庫狀態及角色

SQL> select database_role from v$database;

DATABASE_ROLE

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

PHYSICAL STANDBY

2、備庫

(1)、查詢資料庫角色及狀態

SQL> select name,database_role from v$database;

NAME   DATABASE_ROLE

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

ORCL   PHYSICAL STANDBY

SQL> select switchover_status from v$database;

SWITCHOVER_STATUS

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

TO PRIMARY

(2)、切換到主庫,此時是mount狀態

SQL> alter database commit to switchover to primary;

Database altered.

SQL> alter database open;

Database altered.

(3)、開啟資料庫查詢角色資訊

SQL> select name,DATABASE_ROLE from v$database;

NAME   DATABASE_ROLE

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

ORCL   PRIMARY

3、新備庫

(1)、開啟資料庫,檢視資料庫狀態

SQL> alter database open;

Database altered.

SQL> select open_mode from v$database;

OPEN_MODE

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

READ ONLY

(2)、應用日誌,檢視資料庫狀態

SQL>alter database recover managed standby database using current logfile disconnect from session;sion;

Database altered.

SQL> select open_mode from v$database;

OPEN_MODE

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

READ ONLY WITH APPLY

4、驗證資料同步

二、failover

是當主庫不可用的時候,強制性把備庫切換成主庫,以繼續提供資料庫服務,此時主庫已經崩掉,在備庫操作

1、確認日誌是否同步

SQL> SELECT THREAD#, LOW_SEQUENCE#, HIGH_SEQUENCE# FROM V$ARCHIVE_GAP;

no rows selected

如果日誌不同步可以使用

ALTER DATABASE REGISTER PHYSICAL LOGFILE '缺失日誌檔案'; 

2、查詢日誌是否應用到最新狀態

select distinct thread#,max(sequence#) over(partition by thread#) a from v$archived_log;

3、切換到主庫

SQL> alter database recover managed standby database finish force;

Database altered.

SQL> alter database commit to switchover to primary;                               

SQL> alter database commit to switchover to primary;

Database altered.

SQL> alter database open;

Database altered.

SQL> select name,database_role from v$database;

NAME   DATABASE_ROLE

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

ORCL   PRIMARY

成功切換到主庫,此時DG環境已經損壞,需要重新構建。