Oracle DG Failover 操作

1 在主库端模拟数据库意外宕机 
主库操作 

[oracle@localhost ~]$ sqlplus /nolog

SQL*Plus: Release 11.2.0.4.0 Production on Wed May 10 11:53:25 2017

Copyright (c) 1982, 2013, Oracle.  All rights reserved.

SQL> conn /as sysdba
Connected to an idle instance.
SQL> startup
ORACLE instance started.

Total System Global Area 1043886080 bytes
Fixed Size		    2259840 bytes
Variable Size		  889193600 bytes
Database Buffers	  146800640 bytes
Redo Buffers		    5632000 bytes
Database mounted.
Database opened.
SQL> set lines 200;
SQL> select name, LOG_MODE, OPEN_MODE, database_role, SWITCHOVER_STATUS, db_unique_name from v$database;

NAME	  LOG_MODE     OPEN_MODE	    DATABASE_ROLE    SWITCHOVER_STATUS	  DB_UNIQUE_NAME
--------- ------------ -------------------- ---------------- -------------------- ------------------------------
ORCL	  ARCHIVELOG   READ WRITE	    PRIMARY	     TO STANDBY 	  orcl_pd

SQL> alter system switch logfile;

System altered.


SQL> create table test  as select * from dba_objects;

Table created.

SQL> insert into test select * from test;

86963 rows created.

SQL> insert into test select * from test;

173926 rows created.

SQL> insert into test select * from test;

347852 rows created.

SQL> insert into test select * from test;

695704 rows created.

SQL> commit;

Commit complete.

SQL> select count(1) from test;

  COUNT(1)
----------
   1391408

SQL> alter system switch logfile;

System altered.

备库查询

SQL> select count(1) from test;

  COUNT(1)
----------
   1391408

SQL> select thread#, low_sequence#, high_sequence# from v$archive_gap;

no rows selected

如果没有发现明显的gap现象,说明此次的failover不会有数据损失情况。在standby端,要进行关闭apply和结束应用动作。

2 备库直接切换

[oracle@localhost ~]$ sqlplus /nolog

SQL*Plus: Release 11.2.0.4.0 Production on Wed May 10 11:53:40 2017

Copyright (c) 1982, 2013, Oracle.  All rights reserved.

SQL> conn /as sysdba
Connected to an idle instance.
SQL> startup
ORACLE instance started.

Total System Global Area 1043886080 bytes
Fixed Size		    2259840 bytes
Variable Size		  889193600 bytes
Database Buffers	  146800640 bytes
Redo Buffers		    5632000 bytes
Database mounted.
Database opened.
SQL> alter database recover managed standby database disconnect from session;

Database altered.

SQL> SELECT UNIQUE THREAD# AS THREAD, MAX(SEQUENCE#) OVER (PARTITION BY thread#) AS LAST from V$ARCHIVE

    THREAD	 LAST
---------- ----------
	 1	   40

SQL> SELECT THREAD#, LOW_SEQUENCE#, HIGH_SEQUENCE# FROM V$ARCHIVE_GAP;

no rows selected

SQL> SELECT SWITCHOVER_STATUS FROM V$DATABASE;

SWITCHOVER_STATUS
--------------------
NOT ALLOWED

SQL>  alter database recover managed standby database cancel;

Database altered.

SQL>  alter database recover managed standby database finish; 

Database altered.


SQL> set lines 200

SQL> select name, LOG_MODE, OPEN_MODE, database_role, SWITCHOVER_STATUS, db_unique_name from v$database;
NAME	  LOG_MODE     OPEN_MODE	    DATABASE_ROLE    SWITCHOVER_STATUS	  DB_UNIQUE_NAME
--------- ------------ -------------------- ---------------- -------------------- ------------------------------
ORCL	  ARCHIVELOG   READ ONLY	    PHYSICAL STANDBY TO PRIMARY 	  orcl_st

SQL> alter database commit to switchover to primary with session shutdown;

Database altered.

SQL> select name, LOG_MODE, OPEN_MODE, database_role, SWITCHOVER_STATUS, db_unique_name from v$database;

NAME	  LOG_MODE     OPEN_MODE	    DATABASE_ROLE    SWITCHOVER_STATUS	  DB_UNIQUE_NAME
--------- ------------ -------------------- ---------------- -------------------- ------------------------------
ORCL	  ARCHIVELOG   MOUNTED		    PRIMARY	     NOT ALLOWED	  orcl_st

SQL> alter database open; 

Database altered.

SQL> select name, LOG_MODE, OPEN_MODE, database_role, SWITCHOVER_STATUS, db_unique_name from v$database;

NAME	  LOG_MODE     OPEN_MODE	    DATABASE_ROLE    SWITCHOVER_STATUS	  DB_UNIQUE_NAME
--------- ------------ -------------------- ---------------- -------------------- ------------------------------
ORCL	  ARCHIVELOG   READ WRITE	    PRIMARY	     RESOLVABLE GAP	  orcl_st

SQL> select open_mode from v$database;

OPEN_MODE
--------------------
READ WRITE

SQL> select database_role from v$database;

DATABASE_ROLE
----------------
PRIMARY


3 切换后的测试 
新主库执行 

SQL> alter system switch logfile; 

System altered.

SQL>  delete from test where rownum<=10000;

10000 rows deleted.

SQL> commit;

Commit complete.

SQL> select name, LOG_MODE, OPEN_MODE, database_role, SWITCHOVER_STATUS, db_unique_name from v$database;

NAME	  LOG_MODE     OPEN_MODE	    DATABASE_ROLE    SWITCHOVER_STATUS	  DB_UNIQUE_NAME
--------- ------------ -------------------- ---------------- -------------------- ------------------------------
ORCL	  ARCHIVELOG   READ WRITE	    PRIMARY	     FAILED DESTINATION   orcl_st

FAILED DESTINATION 表明 DG 现在不能同步了。新主库open_mode 为READ WRITE,database_role 为 PRIMARY 说明备库切换主库成功, 
 以上说明failover 过程成功。

4 原主库修复后,开机


SQL> startup
ORACLE instance started.

Total System Global Area 1043886080 bytes
Fixed Size		    2259840 bytes
Variable Size		  889193600 bytes
Database Buffers	  146800640 bytes
Redo Buffers		    5632000 bytes
Database mounted.
Database opened.
SQL> alter database commit to switchover to physical standby with session shutdown;
alter database commit to switchover to physical standby with session shutdown
*
ERROR at line 1:
ORA-16416: No viable Physical Standby switchover targets available


SQL> select name, LOG_MODE, OPEN_MODE, database_role, SWITCHOVER_STATUS, db_unique_name from v$database; 

NAME	  LOG_MODE     OPEN_MODE	    DATABASE_ROLE    SWITCHOVER_STATUS	  DB_UNIQUE_NAME
--------- ------------ -------------------- ---------------- -------------------- ------------------------------
ORCL	  ARCHIVELOG   READ WRITE	    PRIMARY	     FAILED DESTINATION   orcl_pd

修复主库成功后,整个DG 架构已经破坏,需要将原主库删掉,根据ADG 方式重新搭建DG 环境,最后可以根据需要是否进行switchover切换来调整环境。

5 在原主库上重新搭建dg 

SQL> alter database close;

Database altered.

SQL> drop database;
drop database
*
ERROR at line 1:
ORA-12719: operation requires database is in RESTRICTED mode


SQL>  alter system enable restricted session;

System altered.

SQL> drop database;

Database dropped.

[oracle@localhost ~]$ cd $ORACLE_HOME
[oracle@localhost db_1]$ cd /dbs
[oracle@localhost db_1]$ rm -rf spfileorcl.ora


[oracle@localhost trace]$ rman target sys/oracle@orcl_pd auxiliary sys/oracle@orcl_st

Recovery Manager: Release 11.2.0.4.0 - Production on Wed May 10 13:33:17 2017

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

connected to target database: ORCL (not mounted)
connected to auxiliary database: ORCL (DBID=1468135053)

RMAN>  duplicate target database for standby from active database nofilenamecheck;

Starting Duplicate Db at 10-MAY-17
using target database control file instead of recovery catalog
allocated channel: ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: SID=20 device type=DISK
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-03002: failure of Duplicate Db command at 05/10/2017 13:33:29
RMAN-05501: aborting duplication of target database
RMAN-05502: the target database must be mounted when issuing a DUPLICATE command

这里出现一个小插曲,我把以前的搭建dg 的语句直接拿来用,忘了新主库对应的服务名是orcl_st 
此时应该将orcl_pd 和orcl_st 位置对调 就可以了 此命令在主备库执行都可以。切记服务名不要搞反了。

 [oracle@localhost trace]$ rman target sys/oracle@orcl_st auxiliary sys/oracle@orcl_pd

Recovery Manager: Release 11.2.0.4.0 - Production on Wed May 10 13:36:42 2017

Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.

connected to target database: ORCL (DBID=1468135053)
connected to auxiliary database: ORCL (not mounted)

RMAN> duplicate target database for standby from active database nofilenamecheck;

Starting Duplicate Db at 10-MAY-17
using target database control file instead of recovery catalog
allocated channel: ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: SID=21 device type=DISK

contents of Memory Script:
{
   backup as copy reuse
   targetfile  '/u01/app/oracle/product/11.2.0/db_1/dbs/orapworcl' auxiliary format 
 '/u01/app/oracle/product/11.2.0/db_1/dbs/orapworcl'   ;
}
executing Memory Script

Starting backup at 10-MAY-17
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=38 device type=DISK
Finished backup at 10-MAY-17

contents of Memory Script:
{
   backup as copy current controlfile for standby auxiliary format  '/u01/app/oracle/oradata/orcl/control01.ctl';
   restore clone controlfile to  '/u01/app/oracle/fast_recovery_area/orcl/control02.ctl' from 
 '/u01/app/oracle/oradata/orcl/control01.ctl';
}
executing Memory Script

Starting backup at 10-MAY-17
using channel ORA_DISK_1
channel ORA_DISK_1: starting datafile copy
copying standby control file
output file name=/u01/app/oracle/product/11.2.0/db_1/dbs/snapcf_orcl.f tag=TAG20170510T133702 RECID=8 STAMP=943623424
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:03
Finished backup at 10-MAY-17

Starting restore at 10-MAY-17
using channel ORA_AUX_DISK_1

channel ORA_AUX_DISK_1: copied control file copy
Finished restore at 10-MAY-17

contents of Memory Script:
{
   sql clone 'alter database mount standby database';
}
executing Memory Script

sql statement: alter database mount standby database

contents of Memory Script:
{
   set newname for tempfile  1 to 
 "/u01/app/oracle/oradata/orcl/temp01.dbf";
   switch clone tempfile all;
   set newname for datafile  1 to 
 "/u01/app/oracle/oradata/orcl/system01.dbf";
   set newname for datafile  2 to 
 "/u01/app/oracle/oradata/orcl/sysaux01.dbf";
   set newname for datafile  3 to 
 "/u01/app/oracle/oradata/orcl/undotbs01.dbf";
   set newname for datafile  4 to 
 "/u01/app/oracle/oradata/orcl/users01.dbf";
   set newname for datafile  5 to 
 "/u01/app/oracle/oradata/orcl/example01.dbf";
   backup as copy reuse
   datafile  1 auxiliary format 
 "/u01/app/oracle/oradata/orcl/system01.dbf"   datafile 
 2 auxiliary format 
 "/u01/app/oracle/oradata/orcl/sysaux01.dbf"   datafile 
 3 auxiliary format 
 "/u01/app/oracle/oradata/orcl/undotbs01.dbf"   datafile 
 4 auxiliary format 
 "/u01/app/oracle/oradata/orcl/users01.dbf"   datafile 
 5 auxiliary format 
 "/u01/app/oracle/oradata/orcl/example01.dbf"   ;
   sql 'alter system archive log current';
}
executing Memory Script

executing command: SET NEWNAME

renamed tempfile 1 to /u01/app/oracle/oradata/orcl/temp01.dbf in control file

executing command: SET NEWNAME

executing command: SET NEWNAME

executing command: SET NEWNAME

executing command: SET NEWNAME

executing command: SET NEWNAME

Starting backup at 10-MAY-17
using channel ORA_DISK_1
channel ORA_DISK_1: starting datafile copy
input datafile file number=00001 name=/u01/app/oracle/oradata/orcl/system01.dbf
output file name=/u01/app/oracle/oradata/orcl/system01.dbf tag=TAG20170510T133713
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:35
channel ORA_DISK_1: starting datafile copy
input datafile file number=00002 name=/u01/app/oracle/oradata/orcl/sysaux01.dbf
output file name=/u01/app/oracle/oradata/orcl/sysaux01.dbf tag=TAG20170510T133713
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:36
channel ORA_DISK_1: starting datafile copy
input datafile file number=00005 name=/u01/app/oracle/oradata/orcl/example01.dbf
output file name=/u01/app/oracle/oradata/orcl/example01.dbf tag=TAG20170510T133713
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:15
channel ORA_DISK_1: starting datafile copy
input datafile file number=00003 name=/u01/app/oracle/oradata/orcl/undotbs01.dbf
output file name=/u01/app/oracle/oradata/orcl/undotbs01.dbf tag=TAG20170510T133713
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:07
channel ORA_DISK_1: starting datafile copy
input datafile file number=00004 name=/u01/app/oracle/oradata/orcl/users01.dbf
output file name=/u01/app/oracle/oradata/orcl/users01.dbf tag=TAG20170510T133713
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:01
Finished backup at 10-MAY-17

sql statement: alter system archive log current

contents of Memory Script:
{
   switch clone datafile all;
}
executing Memory Script

datafile 1 switched to datafile copy
input datafile copy RECID=8 STAMP=943623527 file name=/u01/app/oracle/oradata/orcl/system01.dbf
datafile 2 switched to datafile copy
input datafile copy RECID=9 STAMP=943623527 file name=/u01/app/oracle/oradata/orcl/sysaux01.dbf
datafile 3 switched to datafile copy
input datafile copy RECID=10 STAMP=943623527 file name=/u01/app/oracle/oradata/orcl/undotbs01.dbf
datafile 4 switched to datafile copy
input datafile copy RECID=11 STAMP=943623527 file name=/u01/app/oracle/oradata/orcl/users01.dbf
datafile 5 switched to datafile copy
input datafile copy RECID=12 STAMP=943623527 file name=/u01/app/oracle/oradata/orcl/example01.dbf
Finished Duplicate Db at 10-MAY-17
-- rman 搭建dg 环境成功 需要将新备库应用到dg 环境
新备库执行
SQL> select open_mode from v$database; 

OPEN_MODE
--------------------
MOUNTED

SQL> alter database open; 

Database altered.

SQL> select open_mode from v$database; 

OPEN_MODE
--------------------
READ ONLY

SQL> alter database recover managed standby database disconnect from session; 

Database altered.

SQL> select open_mode from v$database; 

OPEN_MODE
--------------------
READ ONLY WITH APPLY

主库
SQL> select count(1) from test;

  COUNT(1)
----------
   1381408
SQL> alter system switch logfile;

System altered.

备库查询
SQL> select count(1) from test;

  COUNT(1)
----------
   1381408
数据跟主库同步

至此 dg 环境恢复成功。

如果想还原最初的环境 将现在的主备库switchover 后即可还原原始环境 ,switchover 可参考;

http://www.cndba.cn/leo1990/article/1910

adg 方式搭建dg 环境 可参考:

  http://www.cndba.cn/leo1990/article/1873 

你可能感兴趣的:(Oracle,11g,Data,Guard)