用于有准备的、计划之中的切换,通常是系统升级、数据迁移等常态任务
1、检查主备库状态
确认主、备库正常运行:
查看主库状态为OPEN,备库为MOUNT,备库能顺利接收到主库日志。
确认没有其他会话连接:
select SID,SERIAL#,USERNAME,status FROM V$SESSION WHERE USERNAME IS NOT NULL;
必要的情况将Session Kill:
Alter system kill session‘SID,SERIAL#’;
确保备库为归档模式:
SQL>SELECT log_mode FROM v$DATABASE;
LOG_MODE
------------
ARCHIVELOG
2、切换主库为备库
SQL>SELECT SWITCHOVER_STATUS FROM V$DATABASE;
SWITCHOVER_STATUS
--------------------
SESSIONS ACTIVE
SQL>ALTER DATABASE COMMIT TO SWITCHOVER TOPHYSICAL STANDBY;
Database altered.
或者alter database commit to switchover to physical standby with session shutdown;
SQL>SHUTDOWN
ORA-01507: databasenot mounted
ORACLE instanceshut down.
SQL>STARTUP MOUNT
ORACLE instancestarted.
Total System GlobalArea 159383552 bytes
Fixed Size 1218268 bytes
Variable Size 71305508 bytes
DatabaseBuffers 83886080 bytes
Redo Buffers 2973696 bytes
Database mounted.
3、切换备库为主库
SQL>SELECT SWITCHOVER_STATUS FROM V$DATABASE;
SWITCHOVER_STATUS
--------------------
TO PRIMARY
SQL>ALTER DATABASE COMMIT TO SWITCHOVER TO PRIMARY;
Database altered.
SQL>SHUTDOWN IMMEDIATE
ORA-01507: databasenot mounted
ORACLE instanceshut down.
SQL>STARTUP
ORACLE instancestarted.
Total System GlobalArea 159383552 bytes
Fixed Size 1218268 bytes
Variable Size 71305508 bytes
DatabaseBuffers 83886080 bytes
Redo Buffers 2973696 bytes
Database mounted.
Database opened.
4、备库启用日志应用
ALTER DATABASE RECOVER MANAGED STANDBY DATABASE DISCONNECT FROM SESSION;
5、查看主库状态
SQL> select DATABASE_ROLE, PROTECTION_MODE, SWITCHOVER_STATUS, FLASHBACK_ON from v$database;
DATABASE_ROLE PROTECTION_MODE SWITCHOVER_STATUS FLASHBACK_ON
------------------------------------ -------------------- ------------------
PRIMARY MAXIMUM PERFORMANCE TO STANDBY NO
主库状态为“TO STANDBY”,说明主库可以回切为备库,如果想将备库重新切换为主库,可以按照上面的步骤重新执行一遍即可。
注意切换完成以后必须自行添加临时文件,当我们在Primary端添加一个临时表空间,Standby端虽然不能创建出临时文件,但是临时表空间的信息是联动的带入的
Failover
用于意料之外的突发情况,比如异常掉电、自然灾难等等。
1、 检查备库日志是否完成所有可用日志恢复
检查备库alert日志,查看所有可用的日志是否reply完成,完成后即可操作下面步骤。
2、 终止日志reply操作
SQL> alter database recover managed standby database cancel;
Database altered.
SQL> alter database recover managed standby database finish;
Database altered
3、 检查数据库的状态为“ TO PRIMARY”即可(TO PRIMARY说明是备库)
SQL> select open_mode, switchover_status from v$database;
OPEN_MODE SWITCHOVER_STATUS
-------------------- --------------------
READ ONLY TO PRIMARY
4、 切换备库为主库角色
SQL> ALTER DATABASE ACTIVATE PHYSICAL STANDBY DATABASE;
SQL> alter database commit to switchover to primary with session shutdown;
Database altered.
SQL> select open_mode from v$database;
OPEN_MODE
--------------------
MOUNTED
SQL> alter database open;
Database altered.
注意切换完成以后必须自行添加临时文件,当我们在Primary端添加一个临时表空间,Standby端虽然不能创建出临时文件,但是临时表空间的信息是联动的带入的