在Data Guard环境中可以利用数据库的闪回特性将备库以读写模式打开,用于执行对主数据库的某种测试工作,当测试完毕后,再将数据库闪回到之前的状态,作为备份继续提供保护服务。
1 在备库闪回数据库1)取消备库托管恢复
SQL> alter database recover managed standby database cancel; Database altered.
2)设置备库的闪回恢复区
SQL> show parameter db_recover NAME TYPE VALUE ------------------------------------ ----------- ------------------------------ db_recovery_file_dest string /u01/app/oracle/fast_recovery_ area db_recovery_file_dest_size big integer 4560M
3)启动闪回特性
SQL> startup mount ORACLE instance started. Total System Global Area 1644167168 bytes Fixed Size 2925024 bytes Variable Size 1073745440 bytes Database Buffers 553648128 bytes Redo Buffers 13848576 bytes Database mounted. SQL> alter database flashback on; Database altered. SQL> select flashback_on from v$database; FLASHBACK_ON ------------------ YES
4)创建一个保证还原点
SQL> create restore point before_opem_rw guarantee flashback database; Restore point created.
5)将主库向备库传输日志的目标目的地设置为延迟状态
SQL> show parameter log_archive_dest_2 NAME TYPE VALUE ------------------------------------ ----------- ------------------------------ log_archive_dest_2 string SERVICE=shhai LGWR ASYNC VALID_FOR=(ONLINE_LOGFILES,P RIMARY_ROLE) DB_UNIQUE_NAME=orcldg SQL> show parameter log_archive_dest_state_2 NAME TYPE VALUE ------------------------------------ ----------- ------------------------------ log_archive_dest_state_2 string ENABLE SQL> alter system set log_archive_dest_state_2='defer'; System altered.
6)激活物理备库,将备库转换为独立的数据库运行
SQL> alter database activate physical standby database; Database altered. SQL> alter database open; Database altered. SQL> select database_role,open_mode,flashback_on from v$database; DATABASE_ROLE OPEN_MODE FLASHBACK_ON ---------------- -------------------- ------------------ PRIMARY READ WRITE YES
7)在激活后的备库执行测试等各种操作
8)测试完毕,将数据库闪回到保证还原点
SQL> shutdown abort ORACLE instance shut down. SQL> startup mount ORACLE instance started. Total System Global Area 1644167168 bytes Fixed Size 2925024 bytes Variable Size 1073745440 bytes Database Buffers 553648128 bytes Redo Buffers 13848576 bytes Database mounted. SQL> flashback database to restore point before_opem_rw; Flashback complete. SQL> select database_role,open_mode,flashback_on from v$database; DATABASE_ROLE OPEN_MODE FLASHBACK_ON ---------------- -------------------- ------------------ PRIMARY MOUNTED YES
此时,数据库仍然使独立的数据库,只是状态回到了还原点。
9)将数据库转换为备库
SQL> alter database convert to physical standby ; Database altered. SQL> select database_role,open_mode,flashback_on from v$database; DATABASE_ROLE OPEN_MODE FLASHBACK_ON ---------------- -------------------- ------------------ PHYSICAL STANDBY MOUNTED YES SQL> alter database open; Database altered.
10)在主库启用向备库的日志传输
SQL> alter system set log_archive_dest_state_2='enable'; System altered.
11)启动物理备库的日志应用
SQL> alter database recover managed standby database disconnect from session; Database altered. SQL> select database_role,open_mode,flashback_on from v$database; DATABASE_ROLE OPEN_MODE FLASHBACK_ON ---------------- -------------------- ------------------ PHYSICAL STANDBY READ ONLY WITH APPLY YES
上述演示展示了创建还原点将备库激活,以读写模式打开,进行一系列的测试,并利用闪回技术还原,之后再将其转为备库,整个过程比较繁琐,并且再备库以读写模式打开后,实际上是作为一个独立的数据库存在,但这个期间数据库停止接收了来自主库的重做日志,这在一定程度上就失去了对主库的保护,那么有没有一种方法,在使备库处于读写状态时,可以继续接收重做日志,只是不应用?
快照数据库(Snapshot standby database)可以做到,下面将进行演示。
2 快照备用数据库1)取消备库托管恢复
SQL> alter database recover managed standby database cancel; Database altered.
2)将备库转换为快照数据库
SQL> alter database convert to snapshot standby; Database altered. SQL> select database_role,open_mode,flashback_on from v$database; DATABASE_ROLE OPEN_MODE FLASHBACK_ON ---------------- -------------------- ------------------ SNAPSHOT STANDBY MOUNTED YES
3)打开数据库
SQL> alter database open; Database altered. SQL> select database_role,open_mode,flashback_on from v$database; DATABASE_ROLE OPEN_MODE FLASHBACK_ON ---------------- -------------------- ------------------ SNAPSHOT STANDBY READ WRITE YES
4)在快照数据库执行一系列的测试等操作;
5)转换快照数据库为物理备库
SQL> shutdown abort ORACLE instance shut down. SQL> startup mount ORACLE instance started. Total System Global Area 1644167168 bytes Fixed Size 2925024 bytes Variable Size 1073745440 bytes Database Buffers 553648128 bytes Redo Buffers 13848576 bytes Database mounted. SQL> alter database convert to physical standby; Database altered. SQL> alter database open; Database altered.
6)备库开启托管恢复
SQL> alter database recover managed standby database disconnect from session; Database altered.
7)主库修改数据
SQL> update scott.emp set comm=1000 where empno=7369; 1 row updated. SQL> commit; Commit complete. SQL> select * from scott.emp where empno=7369; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------- 7369 SMITH CLERK 7902 17-DEC-80 800 1000 20
8)备库验证,一致。
SQL> select * from scott.emp where empno=7369; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ---------- ---------- --------- ---------- --------- ---------- ---------- ---------- 7369 SMITH CLERK 7902 17-DEC-80 800 1000 20