操作系统:OEL 5.6
数据库版本:Oracle11gR2 11.2.0.4.0
Switchover切换要求主库和备库在数据同步情况下进行,是主备之间的正常切换,主要用于日常维护、灾备演练等。切换顺序为主库先切换为新备库,然后原备库再切换为新主库。
1.通过V$DATABASE查看主库的DATABASE_ROLE、SWITCHOVER_STATUS
##SWITCHOVER_STATUS为TO STANDBY、SESSIONS ACTIVE 均是满足要求状态。
SQL> SELECT OPEN_MODE,SWITCHOVER_STATUS FROM V$DATABASE;
OPEN_MODE SWITCHOVER_STATUS
-------------------- --------------------
READ WRITE TO STANDBY
2.对主库进行切换
SQL> ALTER DATABASE COMMIT TO SWITCHOVER TO PHYSICAL STANDBY WITH SESSION SHUTDOWN;
Database altered.
3.启动原主库到MOUNTED状态
SQL> STARTUP MOUNT;
ORACLE instance started.
Total System Global Area 835104768 bytes
Fixed Size 2257840 bytes
Variable Size 536874064 bytes
Database Buffers 289406976 bytes
Redo Buffers 6565888 bytes
Database mounted.
4.查询转换后的DATABASE_ROLE、SWITCHOVER_STATUS
SQL> SELECT OPEN_MODE,SWITCHOVER_STATUS FROM V$DATABASE;
OPEN_MODE SWITCHOVER_STATUS
-------------------- --------------------
MOUNTED RECOVERY NEEDED
5.查看原备库的DATABASE_ROLE、SWITCHOVER_STATUS
##SWITCHOVER_STATUS为TO PRIMARY、SESSIONS ACTIVE 均是满足要求状态。
SQL> SELECT OPEN_MODE,SWITCHOVER_STATUS FROM V$DATABASE;
OPEN_MODE SWITCHOVER_STATUS
-------------------- --------------------
READ ONLY WITH APPLY TO PRIMARY
6.对原备库进行切换
SQL> ALTER DATABASE COMMIT TO SWITCHOVER TO PRIMARY WITH SESSION SHUTDOWN;
Database altered.
7.打开转换后的新主库
SQL> ALTER DATABASE OPEN;
Database altered.
8.对转换后的新备库应用日志
SQL> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE USING CURRENT LOGFILE DISCONNECT FROM SESSION;
Database altered.
9.查看新主库、新备库状态
SQL> SELECT OPEN_MODE,SWITCHOVER_STATUS FROM V$DATABASE;
OPEN_MODE SWITCHOVER_STATUS
-------------------- --------------------
READ WRITE TO STANDBY
SQL> SELECT OPEN_MODE,SWITCHOVER_STATUS FROM V$DATABASE;
OPEN_MODE SWITCHOVER_STATUS
-------------------- --------------------
MOUNTED NOT ALLOWED
至此,Switchover切换完成!
总结:Switchover为主、备之间的正常切换,切换前要保证主、备库的数据一致,而且要先主切备,后备切主,避免同时存在两个主库。