物理STANDBY的FAILOVER切换会把当前的一个物理STANDBY切换为PRIMARY数据库。
FAILOVER切换一般是PRIMARY数据库发生故障后的切换,这种情况是STANDBY数据库发挥其作用的情况。这种切换发生后,可能会造成数据的丢失。而且这个过程不是可逆的,DATA GUARD环境会被破坏。
由于PRIMARY数据库已经无法启动,所以FAILOVER切换所需的条件并不多,只要检查STANDBY是否运行在最大保护模式下,如果是的话,需要将其置为最大性能模式,否则切换到PRIMARY角色也无法启动。
为了模拟FAILOVER的特点,首先在主库上运行一个比较大的事务,然后通过SHUTDOWN ABORT来关闭PRIMARY数据库:
[oracle@yangtk ~]$ sqlplus yangtk/yangtk SQL*Plus: Release 11.1.0.6.0 - Production on Mon Nov 5 13:11:14 2007 Copyright (c) 1982, 2007, Oracle. All rights reserved. Connected to: Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 - Production With the Partitioning, OLAP, Data Mining and Real Application Testing options SQL> CREATE TABLE T_FAILOVER (ID NUMBER); Table created. SQL> DECLARE 2 V_JOB NUMBER; 3 BEGIN 4 DBMS_JOB.SUBMIT(V_JOB, 5 'BEGIN FOR I IN 1..10000000 LOOP INSERT INTO T_FAILOVER VALUES (I); COMMIT; END LOOP; END;', 6 SYSDATE); 7 COMMIT; 8 END; 9 / PL/SQL procedure successfully completed. SQL> ALTER SYSTEM SWITCH LOGFILE; System altered. SQL> CONN / AS SYSDBA Connected. SQL> ALTER SYSTEM SWITCH LOGFILE; System altered. SQL> SHUTDOWN ABORT ORACLE instance shut down. |
下面登陆STANDBY数据库:
[oracle@yangtk2 ~]$ sqlplus "/ as sysdba" SQL*Plus: Release 11.1.0.6.0 - Production on Mon Nov 26 19:57:11 2007 Copyright (c) 1982, 2007, Oracle. All rights reserved. Connected to: Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 - Production With the Partitioning, OLAP, Data Mining and Real Application Testing options SQL> SELECT THREAD#, LOW_SEQUENCE#, HIGH_SEQUENCE# FROM V$ARCHIVE_GAP; no rows selected |
检查是否存在GAP,如果存在拷贝相应的归档到STANDBY数据库,并注册,再次执行上面的查询,直到上面的查询不返回记录。
在PRIMARY数据库和其他的数据库中检查是否存在本地没有的归档文件,拷贝并注册这些文件:
SQL> SELECT UNIQUE THREAD#, MAX(SEQUENCE#) OVER(PARTITION BY THREAD#) LAST FROM V$ARCHIVED_LOG; THREAD# LAST ---------- ---------- 1 228 |
把所有的归档都拷贝到STANDBY站点,使用ALTER DATABASE REGISTER PHYSICAL LOGFILE语句进行注册,然后停止应用归档:
SQL> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE CANCEL; Database altered. |
下面将STANDBY数据库切换为PRIMARY数据库:
SQL> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE FINISH; Database altered. SQL> SELECT DATABASE_ROLE FROM V$DATABASE; DATABASE_ROLE ---------------- PHYSICAL STANDBY SQL> ALTER DATABASE COMMIT TO SWITCHOVER TO PRIMARY; Database altered. SQL> ALTER DATABASE OPEN; Database altered. |
检查数据库是否已经切换成功:
SQL> SELECT DATABASE_ROLE FROM V$DATABASE; DATABASE_ROLE ---------------- PRIMARY |
至此,FAILOVER切换完成。这个时候应该马上对新的PRIMARY数据库进行备份。
归档是否都拷贝到本地,和志传输的方式,决定FAILOVER过程是否会丢失数据。这个例子中FAILOVER切换没有造成数据的丢失:
SQL> SELECT COUNT(*) FROM YANGTK.T_FAILOVER; COUNT(*) ---------- 77880 |
重启原来的PRIMARY数据库:
SQL> STARTUP ORACLE instance started. Total System Global Area 267825152 bytes Fixed Size 1299316 bytes Variable Size 167775372 bytes Database Buffers 96468992 bytes Redo Buffers 2281472 bytes Database mounted. Database opened. SQL> SELECT COUNT(*) FROM YANGTK.T_FAILOVER; COUNT(*) ---------- 77880 |