Oracle 11g官方文档的记录整理,用于物理Standby数据库的Failover.

   其中要注意,alter system flush redo是11g的新特性。


Step 1 Flush any unsent redo from the primary database to the target standby database.

   

alter system flush redo to target_db_name


 说明:


  For target_db_name,specify the DB_UNIQUE_NAME of the standby database that is to receive the redo flushed from the primary database.


  This statement flushes any unsend redo from the primary database to standby database,and waits for that redo to be applied to the standby database;


  alter system flush redo 能在failover(失败切换)时,可以将没发送出去的redo log从主库发送到备库.


  并且确认备库接收到redo日志并应用后,才返回.前提是主库要能启动到mount状态,这样命令能保证主数据库不会有数据丢失.


如果主库不能启动到mount状态:


 1. 从备库查出每个日志线程的最高日志序号


        SELECT UNIQUE THREAD# AS THREAD, MAX(SEQUENCE#) OVER (PARTITION BY thread#) AS LAST from V$ARCHIVED_LOG; 


 2. 将主库中没有应用的日志复制到备库上,


    3. 在备库注册新复制过来的日志.


        ALTER DATABASE REGISTER PHYSICAL LOGFILE 'filespec1';



Step 2 处理缺失的日志

   1.找出缺失的,不一致的日志来应用.  


       

SELECT thread#,low_sequence#,high_sequence# FROM v$archive_gap;


 2. 将主库中没有应用的

日志复制到备库上,


    3. 在备库注册新复制过来的日志.


  

ALTER DATABASE REGISTER PHYSICAL LOGFILE 'filespec1'; 


    4. 

再重复执行查询,找出下一次中断. 


     原因是v$archive_gap不能一次列出所有,一次只会显示一个当前的中断.



Step 3 Stop Redo Apply

alter database recover managed standby database cancel;


Step 4 Finish applying all received redo data

   

alter database recover managed standby database finish;


   注意:


         假如有问题无法解决,用下面的这个命令,仍然可以将其转为主库,但可能会有数据丢失.


   执行完后,直接到Step 7去执行alter database open;等后续步骤即可.


   没问题,则可省略这条命令.


           alter database activate physical standby database;



Step 5 Verify that the target standby database databse is ready to become a primary database.

     

col OPEN_MODE format a10

      col DB_UNIQUE_NAME format a20

      SELECT NAME,OPEN_MODE,DB_UNIQUE_NAME ,switchover_status FROM v$database;


  假如switchover_status不是返回的TO PRIMARY或SESSIONS ACTIVE则说明备库还没有准备好


    转成主库.需要检查是否有日志没有应用.处理后,再查询,一直到返回TO PRIMARY或SESSIONS ACTIVE为止.



Step 6 Switch the physical standby database to the primary role

   

alter database commit to switchover to primary with session shutdown;




Step 7.Open the new primary database

   

alter database open;



Step 8.Back up the new primary database.

    Oracle recommends that a full backup be taken of the new primary database.



Step 9.Restart Redo Apply if it has stopped at any of the other physical standby database

    in your Data Guard configuration.


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

      其中 using current logfile 的意思是,当备库收到传过来的日志后,会尽快完成恢复。