正常切換切換前: 主庫:SQL> select DATABASE_ROLE from v$database;DATABASE_ROLE PRIMARY SQL> select OPEN_MODE,PROTECTION_MODE,PROTECTION_LEVEL,SWITCHOVER_STATUS f ...
正常切換
切換前:
主庫:
SQL> select DATABASE_ROLE from v$database;
DATABASE_ROLE
----------------
PRIMARY
SQL> select OPEN_MODE,PROTECTION_MODE,PROTECTION_LEVEL,SWITCHOVER_STATUS from v$database;
OPEN_MODE PROTECTION_MODE PROTECTION_LEVEL SWITCHOVER_STATUS
---------- -------------------- -------------------- ------------------
READ WRITE MAXIMUM PERFORMANCE MAXIMUM PERFORMANCE TO STANDBY
SQL> SELECT COUNT(*) FROM V$SESSION WHERE USERNAME IS NOT NULL;
COUNT(*)
----------
1
在切換前,殺掉所有的資料庫連接 觀察SWITCHOVER_STATUS,如果是 TO STANDBY,則可以直接切換
如果SESSIONS ACTIVE ,則用
備用庫 :
SQL> select DATABASE_ROLE from v$database;
DATABASE_ROLE
----------------
PHYSICAL STANDBY
SQL> select OPEN_MODE,PROTECTION_MODE,PROTECTION_LEVEL,SWITCHOVER_STATUS from v$database;
OPEN_MODE PROTECTION_MODE PROTECTION_LEVEL SWITCHOVER_STATUS
---------- -------------------- -------------------- ------------------
MOUNTED MAXIMUM PERFORMANCE MAXIMUM PERFORMANCE NOT ALLOWED
說明:
主庫需要註意事項
A 如果switchover_status為TO_STANDBY說明可以轉換
直接轉換
alter database commit to switchover to physical standby;
B 如果switchover_status為SESSIONS ACTIVE 則關閉會話
SQL>alter database commit to switchover to physical standby with session shutdown;
在備庫中操作,查看備庫
SQL> select switchover_status from v$database;
A 如果switchover_status為TO_PRIMARY 說明標記恢復可以直接轉換為primary庫
SQL>alter database commit to switchover to primary
B 如果switchover_status為SESSION ACTIVE 就應該斷開活動會話
SQL>alter database commit to switchover to primary with session shutdown;
C 如果switchover_status為NOT ALLOWED 說明切換標記還沒收到,此時不能
執行轉換。
切換中 :
備用庫
SQL> select DATABASE_ROLE from v$database;
DATABASE_ROLE
----------------
PHYSICAL STANDBY
SQL> select OPEN_MODE,PROTECTION_MODE,PROTECTION_LEVEL,SWITCHOVER_STATUS from v$database;
OPEN_MODE PROTECTION_MODE PROTECTION_LEVEL SWITCHOVER_STATUS
---------- -------------------- -------------------- ------------------
MOUNTED MAXIMUM PERFORMANCE MAXIMUM PERFORMANCE SESSIONS ACTIVE
切換後備用的狀態,模式:
SQL> select DATABASE_ROLE from v$database;
DATABASE_ROLE
----------------
PRIMARY
SQL> select OPEN_MODE,PROTECTION_MODE,PROTECTION_LEVEL,SWITCHOVER_STATUS from v$database;
OPEN_MODE PROTECTION_MODE PROTECTION_LEVEL SWITCHOVER_STATUS
---------- -------------------- -------------------- ------------------
READ WRITE MAXIMUM PERFORMANCE MAXIMUM PERFORMANCE TO STANDBY
2. 檢查環境:
確認主庫和從庫間網路連接通暢;
確認沒有活動的會話連接在資料庫中;
PRIMARY資料庫處於打開的狀態,STANDBY資料庫處於MOUNT狀態;
確保STANDBY資料庫處於ARCHIVELOG模式;
如果設置了REDO應用的延遲,那麼將這個設置去掉;
確保配置了主庫和從庫的初始化參數,使得切換完成後,DATA GUARD機制可以順利的運行。
如果是最大保護模式,先變成最大性能模式:
3. 切換的順序: 先從主庫到備用,再從備庫到主庫
主切備:
SQL> select DATABASE_ROLE from v$database;
DATABASE_ROLE
----------------
PRIMARY
SQL> select OPEN_MODE,PROTECTION_MODE,PROTECTION_LEVEL,SWITCHOVER_STATUS from v$database;
OPEN_MODE PROTECTION_MODE PROTECTION_LEVEL SWITCHOVER_STATUS
---------- -------------------- -------------------- ------------------
READ WRITE MAXIMUM PERFORMANCE MAXIMUM PERFORMANCE TO STANDBY
SQL>
SQL> alter database commit to switchover to physical standby ;
Database altered.
SQL> shutdown immediate;
ORA-01507: database not mounted
ORACLE instance shut down.
SQL> startup nomount ;
ORACLE instance started.
Total System Global Area 3242987696 bytes
Fixed Size 733360 bytes
Variable Size 1174405120 bytes
Database Buffers 2063597568 bytes
Redo Buffers 4251648 bytes
SQL> alter database mount standby database;
Database altered.
SQL> alter database recover managed standby database disconnect from session;
Database altered.
SQL> select DATABASE_ROLE from v$database;
DATABASE_ROLE
----------------
PHYSICAL STANDBY
SQL> select OPEN_MODE,PROTECTION_MODE,PROTECTION_LEVEL,SWITCHOVER_STATUS from v$database;
OPEN_MODE PROTECTION_MODE PROTECTION_LEVEL SWITCHOVER_STATUS
---------- -------------------- -------------------- ------------------
MOUNTED MAXIMUM PERFORMANCE MAXIMUM PERFORMANCE TO PRIMARY
備切主
SQL> select DATABASE_ROLE from v$database;
DATABASE_ROLE
----------------
PHYSICAL STANDBY
SQL> select OPEN_MODE,PROTECTION_MODE,PROTECTION_LEVEL,SWITCHOVER_STATUS from v$database;
OPEN_MODE PROTECTION_MODE PROTECTION_LEVEL SWITCHOVER_STATUS
---------- -------------------- -------------------- ------------------
MOUNTED MAXIMUM PERFORMANCE MAXIMUM PERFORMANCE TO PRIMARY
SQL> alter database commit to switchover to primary;
Database altered.
SQL>
SQL> shutdown immediate;
ORA-01507: database not mounted
ORACLE instance shut down.
SQL> startup ;
ORACLE instance started.
Total System Global Area 3242987696 bytes
Fixed Size 733360 bytes
Variable Size 1174405120 bytes
Database Buffers 2063597568 bytes
Redo Buffers 4251648 bytes
Database mounted.
Database opened.
修改主 備庫 的 tnsnames.ora
主庫SESSIONS ACTIVE是正常primary狀態下的結果。
DataGuard:PhysicalStandby Failover
接physical standby switchover
primary db :db2
standby db :db1
一般情況下failover都是表示primary資料庫癱瘓,最起碼也是起不來了,因此這種類型的切換基本上不需要primary資料庫做什麼操作。所以下列步驟中如果有提到primary和standby執行的,只是建議你如果primary還可以用,那就執行一下,即使不去執行,對failover來說也沒有關係
1.檢查歸檔文件是否連續,是否有gap
在standby庫執行
SQL> select THREAD#,LOW_SEQUENCE#,HIGH_SEQUENCE# from v$archive_gap;
2.如果步驟1查詢出來紀錄,則在primary庫上執行,否則跳過此步驟
在主庫上執行語句,按步驟1查詢出來的紀錄找出歸檔文件
SQL> SELECT NAME FROM V$ARCHIVED_LOG WHERE THREAD#=1 AND DEST_ID=1 AND SEQUENCE# BETWEEN XX AND XX;
--如果primary存在,拷貝相應的歸檔到STANDBY資料庫,並註冊.
SQL> ALTER DATABASE REGISTER PHYSICAL LOGFILE 'xxx';
3.檢查歸檔文件是否完整
分別在primary/standby執行下列語句:
SQL> select distinct thread#,max(sequence#) over(partition by thread#) a from v$archived_log;
把相差的歸檔複製到待轉換的standby伺服器,並手工register
4.開始做failover
察看standby進程狀態
SQL> select process,client_process,sequence#,status from v$managed_standby;
PROCESS CLIENT_P SEQUENCE# STATUS
--------- -------- ---------- ------------
ARCH ARCH 30 CLOSING
ARCH ARCH 0 CONNECTED
RFS LGWR 31 IDLE
RFS ARCH 0 IDLE
RFS N/A 0 IDLE
MRP0 N/A 31 APPLYING_LOG
SQL> alter database recover managed standby database finish force ;
FORCE關鍵字將會停止當前活動的RFS進程,以便立刻執行failover。
或
SQL> alter database recover managed standby database finish skip standby logfile;
SQL> alter database commit to switchover to primary;
SQL> shutdown immediate
SQL> startup
SQL> select database_role,protection_mode,protection_level,open_mode from v$database;
DATABASE_ROLE PROTECTION_MODE PROTECTION_LEVEL OPEN_MODE
---------------- -------------------- -------------------- ----------
PRIMARY MAXIMUM PERFORMANCE MAXIMUM PERFORMANCE READ WRITE
Failover切換成功!
dataguard驗證是否正常同步的2種方法
1、
最簡單的方法,備庫查看最新的歸檔日誌是否apply,如果applied是yes則正常同步
主庫sqlplus>
alter system switch logfile;
備庫sqlplus>
select SEQUENCE#,applied,FIRST_TIME,NEXT_TIME from v$archived_log order by 1 desc;
2、
主庫LNS進程的BLOCK#是否變動、備庫查看RFS進程的BLOCK#是否變動,如果BLOCK#都變動,則正常同步
主庫sqlplus>
select PROCESS,STATUS,SEQUENCE#,BLOCK# from V$MANAGED_STANDBY where process='LNS';
備庫sqlplus>
select PROCESS,CLIENT_PROCESS,SEQUENCE#,BLOCK# from V$MANAGED_STANDBY where PROCESS='RFS';
dataguard主備延遲多長時間的2種查詢方法
1、
備庫sqlplus>
select value from v$dataguard_stats where name='apply lag';
2、
備庫sqlplus>
select ceil((sysdate-next_time)*24*60) "M" from v$archived_log where applied='YES' AND SEQUENCE#=(SELECT MAX(SEQUENCE#) FROM V$ARCHIVED_LOG WHERE applied='YES');
//
檢查主備兩邊的序號
select max(sequence#) from v$log;
備庫執行,查看是否有數據未應用
select name,SEQUENCE#,APPLIED from v$archived_log order by sequence#;
select SEQUENCE#,FIRST_TIME,NEXT_TIME ,APPLIED from v$archived_log order by 1;
檢查備庫是否開啟實時應用
select recovery_mode from v$archive_dest_status where dest_id=2;
檢查備庫狀態
select switchover_status from v$database; --發現狀態not allowed
看看進程MRP是否存在
ps aux|grep mrp --發現進程不存在
如果不存在執行以下:
alter database recover managed standby database using current logfile disconnect;
alter database recover managed standby database disconnect from session; --後臺執行
alter database recover managed standby database --前臺執行,執行這個可以看到報錯的情況
如果有報錯,查看alert日誌和log.xml日誌
驗證是否正常
select process,status from v$managed_standby;
select process,status,sequence# from v$managed_standby;
如果看到mrp0正常
6、以上步驟處理好後,如果數據還不正常,接著處理
col OPEN_MODE for a10
col DATABASE_ROLE for a20
col PROTECTION_MODE for a20
col PROTECTION_LEVEL for a20
col SWITCHOVER_STATUS for a20
select OPEN_MODE,DATABASE_ROLE,PROTECTION_MODE,PROTECTION_LEVEL,SWITCHOVER_STATUS from v$database;
轉載於
switch_status not allowed等物理dataguard 正常切換 腳色轉換
https://blog.csdn.net/demonson/article/details/17913217