主备库切换原因:

1. switch:用户主动切换;

2. failover:主库出现故障,强行切换;

 

switch切换过程:先主库再备库

 

主库

1. 查看switchover_status:select database_role, switchover_status from v$database;

如果switchover_status为:to standby 则:

ALTER DATABASE COMMIT TO SWITCHOVER TO PHYSICAL STANDBY

否则用:ALTER DATABASE COMMIT TO SWITCHOVER TO PHYSICAL STANDBY WITH SESSION SHUTDOWN;

 

2. 重启数据库:

shutdown immediate;

startup nomount;

 

3. 以备库mount

alter database mount standby database;

 

备库

1. 应用redo:alter database recover managed standby database disconnect from session;

 

2. 查看switchover_status:select database_role, switchover_status from v$database;

如果switchover_status为:to primary 则:ALTER DATABASE COMMIT TO SWITCHOVER TO PRIMARY

否则用:ALTER DATABASE COMMIT TO SWITCHOVER TO PRIMARY WITH SESSION SHUTDOWN;

 

3.  重启数据库

shutdown immediate;

startup;

 

failover 主备库切换

1.. 停止日志应用 
alter database recover managed standby database cancel; 

2. 关闭standby日志传输 
alter database recover managed standby database finish force;  

3. 切换到primary 
alter database commit to switchover to primary with session shutdown; 

做这一步的时候,若存在gap,则会报ORA-16139:Switchover: Media recovery required - standby not in limbo 错误。

做测试的时候,若先起主库再起备库,且未等待备库相关日志传输完毕,就会出现这个问题。此时需要强制切换 
alter database activate physical standby database; 

 

4. 重启数据库到open状态