1.首先要应用日志,保持主备库一致:

备库:SBDB@SYS> recover managed standby database using current logfile disconnect from session;
Media recovery complete.

2.查看主备状态:

(1)主库:
ORA11GR2@SYS> select open_mode,protection_mode,database_role,switchover_status from v$database; OPEN_MODE PROTECTION_MODE DATABASE_ROLE SWITCHOVER_STATUS
-------------------- -------------------- ---------------- --------------------
READ WRITE MAXIMUM PERFORMANCE PRIMARY SESSIONS ACTIVE (2)备库:
SBDB@SYS> select open_mode,protection_mode,database_role,switchover_status from v$database; OPEN_MODE PROTECTION_MODE DATABASE_ROLE SWITCHOVER_STATUS
-------------------- -------------------- ---------------- --------------------
READ ONLY MAXIMUM PERFORMANCE PHYSICAL STANDBY NOT ALLOWED

3.把主库切换为备库,重启后检查SWITCHOVER_STATUS为RECOVERY NEEDED:

ORA11GR2@SYS> alter database commit to switchover to physical standby with session shutdown;

Database altered.

ORA11GR2@SYS> shutdown abort
ORACLE instance shut down.
ORA11GR2@SYS> startup mount
ORACLE instance started. Total System Global Area 830930944 bytes
Fixed Size 2257800 bytes
Variable Size 570428536 bytes
Database Buffers 255852544 bytes
Redo Buffers 2392064 bytes
Database mounted.
ORA11GR2@SYS> select switchover_status from v$database; SWITCHOVER_STATUS
--------------------
RECOVERY NEEDED

4.转换完备库恢复日志后,SWITCHOVER_STATUS为TO PRIMARY:

ORA11GR2@SYS>  recover managed standby database using current logfile disconnect from session;
Media recovery complete.
ORA11GR2@SYS> select switchover_status from v$database; SWITCHOVER_STATUS
--------------------
TO PRIMARY

5.查看备库的状态,并应用日志,只有当SWITCHOVER_STATUS是sessions active或者是to_primary状态时才可以切换角色:

SBDB@SYS> recover managed standby database using current logfile disconnect from session;
Media recovery complete.
SBDB@SYS> select switchover_status from v$database; SWITCHOVER_STATUS
--------------------
SESSIONS ACTIVE

6.切换备库并查看database_role,正常为primary:

SBDB@SYS>  alter database commit to switchover to primary with session shutdown;

Database altered.

SBDB@SYS>  alter database open;

Database altered.

SBDB@SYS>  select database_role from v$database;

DATABASE_ROLE
----------------
PRIMARY SBDB@SYS> select switchover_status from v$database; SWITCHOVER_STATUS
--------------------
RESOLVABLE GAP

7.转换后在备库(切换之前的主库)应用日志,并查看状态:

ORA11GR2@SYS>  recover managed standby database using current logfile disconnect from session;
ORA-01153: an incompatible media recovery is active ORA11GR2@SYS> recover managed standby database cancel;
Media recovery complete.
ORA11GR2@SYS> select switchover_status from v$database; SWITCHOVER_STATUS
--------------------
NOT ALLOWED ORA11GR2@SYS> select database_role from v$database; DATABASE_ROLE
----------------
PHYSICAL STANDBY

8.再次查看转换后主库(切换之前的备库)的switchover_status,正常应为TO STANDBY:

SBDB@SYS> select switchover_status from v$database;

SWITCHOVER_STATUS
--------------------
TO STANDBY
05-11 13:19