在Oracle 11g之前,物理备库(physical Standby)在应用redo的时候,是不可以打开的,只可以mount。从11g开始,在应用redo的时候,物理备库可以处于read-only模式,这就称为Active Data Guard 。通过Active Data Guard,可以在物理备库进行查询或者导出数据,从而减少对主库的访问和压力。
Active Data Guard适用于一些只读性的应用,比如,有的应用程序只是查询数据,进行一些报表业务,不会产生redo数据,这些应用可以转移到备库上,避免对主库资源的争用。
Oracle Active Data Guard 是Oracle Database Enterprise Edition的一个功能,需要额外付费来使用这个功能。
如需启用Active Data Guard, 只需要将备库以 read-only 模式打开,而且执行 ALTER DATABASE RECOVER MANAGED STANDBY DATABASE语句就可以。需要注意的是:主库和备库的COMPATIBLE 参数至少要设置为11.0.0。
Data Guard可以以只读的方式打开数据库,但此时Media Recovery利用日志进行数据同步的过程就停止了,如果物理备用数据库处于恢复的过程中数据库就不能打开查询,也就是说日志应用和只读打开两个状态是互斥的。
Oracle 11g 中推出的Active Data Guard功能解决了这个矛盾,在利用日志恢复数据的同时可以用只读的方式打开数据库,用户可以在备用数据库上进行查询、报表等操作,这类似逻辑Data Guard备用数据库的功能(查询功能方面),但是,数据同步的效率更高、对硬件的资源要求更低。这样可以更大程度地发挥物理备用数据库的硬件资源的效能。
实际操作验示:
Oracle 11G Data Guard配置
1.以read only方式打开数据库,如果目前在应用日志模式,需要执行alter database recover managed standby database cancel取消日志应用,然后执行alter database open read only。
SQL> startup mount;
ORACLE instance started.
Total System Global Area 313159680 bytes
Fixed Size 2227944 bytes
Variable Size 218104088 bytes
Database Buffers 88080384 bytes
Redo Buffers 4747264 bytes
Database mounted.
SQL> alter database open read only;
Database altered.
SQL> SELECT open_mode FROM V$DATABASE;
OPEN_MODE
--------------------
READ ONLY
2.在read only模式下,执行ALTER DATABASE RECOVER MANAGED STANDBY DATABASE DISCONNECT FROM SESSION
SQL> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE DISCONNECT FROM SESSION;
Database altered.
如果已经启用了Active Data Guard,备库的V$DATABASE会显示为"READ ONLY WITH APPLY':
SQL> SELECT open_mode FROM V$DATABASE;
OPEN_MODE
--------------------
READ ONLY WITH APPLY
这种方式不会应用主库未归档的日志,只会应用主库已归档并且已传输到备库的日志。
3.为了保证备库数据的实时性,需要在备库启动real-time apply:
先要关闭步骤2中的应用日志方式 :
SQL> alter database recover managed standby database cancel;
Database altered.
SQL> SELECT open_mode FROM V$DATABASE;
OPEN_MODE
--------------------
READ ONLY
执行real-time apply,注意这里是using current logfile;
SQL> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE USING CURRENT LOGFILE DISCONNECT FROM SESSION;
Database altered.
提示:disconnect from session子句并非必须,该子句用于指定启动完应用后自动退出到命令操作符前,如果不指定的话,当前session 就会一直停留处理redo 应用,如果想做其它操作,就只能新建一个连接。
SQL> SELECT open_mode FROM V$DATABASE;
OPEN_MODE
--------------------
READ ONLY WITH APPLY
4.验证real-time apply
查看主库目前的日志,当前的归档日志序号是78
SQL> archive log list;
Database log mode Archive Mode
Automatic archival Enabled
Archive destination /u01/app/oracle/admin/orcl/flash_recovery_area/ORCL/archivelog
Oldest online log sequence 73
Next log sequence to archive 78
Current log sequence 78
主库归档日志里目前才归档到77
SQL> SELECT SEQUENCE#, REGISTRAR, FIRST_TIME, NEXT_TIME, APPLIED FROM V$ARCHIVED_LOG where regis trar='LGWR' order by SEQUENCE#;
。。。
SEQUENCE# REGISTR FIRST_TIM NEXT_TIME APPLIED
---------- ------- --------- --------- ---------
75 LGWR 08-OCT-18 08-OCT-18 YES
76 LGWR 08-OCT-18 08-OCT-18 NO
76 LGWR 08-OCT-18 08-OCT-18 YES
77 LGWR 08-OCT-18 08-OCT-18 NO
48 rows selected.
备库归档日志自然也不会有78号的归档日志:
SQL> SELECT SEQUENCE#, REGISTRAR, FIRST_TIME, NEXT_TIME, APPLIED FROM V$ARCHIVED_LOG ORDER BY SEQUENCE#;
SEQUENCE# REGISTR FIRST_TIM NEXT_TIME APPLIED
---------- ------- --------- --------- ---------
71 RFS 08-OCT-18 08-OCT-18 YES
72 RFS 08-OCT-18 08-OCT-18 YES
73 RFS 08-OCT-18 08-OCT-18 YES
74 RFS 08-OCT-18 08-OCT-18 YES
75 RFS 08-OCT-18 08-OCT-18 YES
76 RFS 08-OCT-18 08-OCT-18 YES
77 RFS 08-OCT-18 08-OCT-18 YES
7 rows selected.
在主库做一些数据的更改:
SQL> select count(*) from l5m.t_test;
COUNT(*)
----------
3446
SQL> delete from l5m.t_test where rownum<100;
99 rows deleted.
SQL> select count(*) from l5m.t_test;
COUNT(*)
----------
3347
SQL> commit;
Commit complete.
立即回到备库查看一下这个数据有没有被同步:
SQL> select count(*) from l5m.t_test;
COUNT(*)
----------
3347
经过验证,已立即同步了过来,并不需要等待归档日志产生后再应用。
官方文档的说明:
If the real-time apply feature is enabled, apply services can apply redo data as it is received, without waiting for the current standby redo log file to be archived. This results in faster switchover and failover times because the standby redo log files have been applied already to the standby database by the time the failover or switchover begins.
下面的功能是允许在read-only的数据库上执行的:
• Issue SELECT statements, including queries that require multiple sorts that leverage TEMP
segments
• Use ALTER SESSION and ALTER SYSTEM statements
• Use SET ROLE
• Call stored procedures
• Use database links (dblinks) to write to remote databases
• Use stored procedures to call remote procedures via dblinks
• Use SET TRANSACTION READ ONLY for transaction level read consistency
• Issue complex queries (such as grouping SET queries and WITH CLAUSE queries)
下面的功能是不允许在read-only的数据库上执行的:
• Any DMLs (excluding simple SELECT statements) or DDLs
• Query accessing local sequences
• DMLs to local temporary tables