Oracle提供了一种快速自动故障转移架构(Fast-Start Failover).当主库发生故障时,FSFO会自动的将指定的从数据库转换成主数据库。此功能需要一个第三个的程序Observer来实现。Observer用于维护主数据库和目标备库的连接,监控配置状况及必要时执行故障转移。如果可能的话,当失败的主库重新联机后,Observer尝试恢复数据库,并将其转换成从库。

  Observer最好是部署在非Dataguard的主库上,为了防止Dataguard中某台机器发生宕机,而凑巧Observer就在这台机器上。系统将无法自动故障转移。

  下图描述了主从数据库与观察器之间的关系

wKiom1XEEQ3Axn0hAAB5Tnwak50402.gif


我们先查看一下FAST_START FAILOVER的属性

DGMGRL> SHOW FAST_START FAILOVER
Fast-Start Failover: DISABLED
  Threshold:        30 seconds
  Target:           (none)
  Observer:         (none)
  Lag Limit:        30 seconds
  Shutdown Primary: TRUE
  Auto-reinstate:   TRUE
Configurable Failover Conditions
  Health Conditions:
    Corrupted Controlfile          YES
    Corrupted Dictionary           YES
    Inaccessible Logfile            NO
    Stuck Archiver                  NO
    Datafile Offline               YES
  Oracle Error Conditions:
    (none)

目前是没有启动自动故障转移功能的。

其中Threshold表示:当主库无效时,Observer等待多久再进行故障转移。

Lag Limit表示:从库的日志应用晚于主库多久,在这个范围内才允许故障转移。此参数用于数据库保护模式为最大性能。

target表示:故障转移目标,也就是将那个从数据库变成主库。

Observer表示:观察器部署在哪个机器上。

Failover Conditions表示:当主数据库发生哪些故障时,才启动故障转移。


一、将主从数据库都开启数据库闪回功能

1.1 从数据库在启用数据库闪回之前,需要关闭日志应用功能

DGMGRL> edit database 11gdg2 set state=APPLY-OFF;
Succeeded.

1.2 将从库启用数据库闪回

SQL>  alter database flashback on;

Database altered.

1.3 开启从库日志应用功能

DGMGRL> edit database 11gdg2 set state=APPLY-ON;
Succeeded.

1.4 将主库开启数据库闪回功能

SQL> alter database flashback on;

Database altered.


二、设置故障切换目标

11gdg1的故障转移目标为11gdg2,反过来也需要设置,可以反复的故障转移。

DGMGRL> EDIT DATABASE 11gdg1 SET PROPERTY FastStartFailoverTarget='11gdg2';
Property "faststartfailovertarget" updated
DGMGRL> EDIT DATABASE 11gdg2 SET PROPERTY FastStartFailoverTarget='11gdg1';
Property "faststartfailovertarget" updated


三、启动FAST_START FAILOVER

DGMGRL> enable FAST_START FAILOVER
Enabled.

此时Observer还没有启动,还不能进行故障转移。Observer的启动,可以是FAST_START FAILOVER生效之前,也可以是FAST_START FAILOVER生效之后。


四、启动Observer

DGMGRL> start observer
Observer started

该命令会阻塞这个终端,并且在当前目录下生成一个FSFO.dat的文件。也可以在start Observer命令后跟上path=/file.dat指定生成文件目录及名称。


五、使用abort方式关闭主库,查看故障转移情况。

看看start Observer窗口打印的信息。


DGMGRL> start observer
Observer started
00:43:49.25  Saturday, July 11, 2015
Initiating Fast-Start Failover to database "11gdg2"...
Performing failover NOW, please wait...
Failover succeeded, new primary is "11gdg2"
00:43:55.15  Saturday, July 11, 2015


DGMGRL> show configuration;

Configuration - DR

  Protection Mode: MaxPerformance
  Databases:
    11gdg2 - Primary database
      Warning: ORA-16829: fast-start failover configuration is lagging

    11gdg1 - (*) Physical standby database (disabled)
      ORA-16661: the standby database needs to be reinstated

Fast-Start Failover: ENABLED

Configuration Status:
WARNING


将原主库启动

SQL> startup
ORACLE instance started.

Total System Global Area  839282688 bytes
Fixed Size		    2233000 bytes
Variable Size		  499125592 bytes
Database Buffers	  335544320 bytes
Redo Buffers		    2379776 bytes
Database mounted.
ORA-16649: possible failover to another database prevents this database from
being opened

由Observer保护着,该库是不允许直接打开的。

我们看看Observer端的打印信息

11:03:42.70  Saturday, July 11, 2015
Initiating reinstatement for database "11gdg1"...
Reinstating database "11gdg1", please wait...
Operation requires shutdown of instance "dgtst" on database "11gdg1"
Shutting down instance "dgtst"...
ORA-01109: database not open

Database dismounted.
ORACLE instance shut down.
Operation requires startup of instance "dgtst" on database "11gdg1"
Starting instance "dgtst"...
ORACLE instance started.
Database mounted.
Continuing to reinstate database "11gdg1" ...
Reinstatement of database "11gdg1" succeeded
11:04:35.56  Saturday, July 11, 2015

修复主库成功。


主库修改的原理是什么呢? 我们查看一下主库的scn以及备库切换成主库时的scn

SQL> select db_unique_name,CURRENT_SCN from v$database;
DB_UNIQUE_NAME	     CURRENT_SCN
-------------------- -----------
11gdg1			 1075548

SQL> select db_unique_name,STANDBY_BECAME_PRIMARY_SCN from v$database;

DB_UNIQUE_NAME	     STANDBY_BECAME_PRIMARY_SCN
-------------------- --------------------------
11gdg2					1075548

先将数据库启动到mount状态,然后通过数据库闪回技术,将库闪回至STANDBY_BECAME_PRIMARY_SCN.

再将数据库变为物理从库。

SQL> STARTUP MOUNT
ORACLE instance started.
Database mounted.
SQL> FLASHBACK DATABASE TO SCN 1075548;
Flashback complete.
SQL> ALTER DATABASE CONVERT TO PHYSICAL STANDBY;