Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1099783
  • 博文数量: 159
  • 博客积分: 3063
  • 博客等级: 中校
  • 技术积分: 2703
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-01 01:51
文章分类

全部博文(159)

文章存档

2013年(48)

2012年(111)

分类: Oracle

2013-08-10 17:05:07

今天做switchover,环境是11.2.0.3+OEL5.7,开始时主备库状态都是正常的,符合直接切换条件:
主库:
SQL> select open_mode,database_role,switchover_status from v$database;

OPEN_MODE            DATABASE_ROLE    SWITCHOVER_STATUS
-------------------- ---------------- --------------------
READ WRITE           PRIMARY          TO STANDBY

备库:
SQL> select open_mode,database_role,switchover_status from v$database;

OPEN_MODE            DATABASE_ROLE    SWITCHOVER_STATUS
-------------------- ---------------- --------------------
READ ONLY WITH APPLY PHYSICAL STANDBY NOT ALLOWED

主库直接进行swichover:
SQL> alter database commit to switchover to physical standby;
alter database commit to switchover to physical standby
*
ERROR at line 1:
ORA-01093: ALTER DATABASE CLOSE only permitted with no sessions connected


SQL> select open_mode,database_role,switchover_status from v$database;

OPEN_MODE            DATABASE_ROLE    SWITCHOVER_STATUS
-------------------- ---------------- --------------------
READ WRITE           PRIMARY          LOG SWITCH GAP

提示有日志切换GAP,于是直接重启主库:
SQL> startup force
ORACLE instance started.

Total System Global Area  413372416 bytes
Fixed Size                  2228904 bytes
Variable Size             322964824 bytes
Database Buffers           83886080 bytes
Redo Buffers                4292608 bytes
Database mounted.
Database opened.
SQL> select open_mode,database_role,switchover_status from v$database;

OPEN_MODE            DATABASE_ROLE    SWITCHOVER_STATUS
-------------------- ---------------- --------------------
READ WRITE           PRIMARY          RESOLVABLE GAP

状态由LOG SWITCH GAP变成了RESOLVABLE GAP,从字面理解是主备库之间存在GAP,于是执行:

SQL> ALTER SYSTEM FLUSH REDO TO ora11dg2;

SQL> select open_mode,database_role,switchover_status from v$database;

OPEN_MODE            DATABASE_ROLE    SWITCHOVER_STATUS
-------------------- ---------------- --------------------
READ WRITE           PRIMARY          TO STANDBY

把主库REDO FLUSH到备库以后,以上状态消失,又重新回到之前的TO STANDBY状态了,可以重新进行SWITCHOVER了。
如果主库状态不是TO STANDBY,而是SESSION ACTIVE,就要加上WITH SESSION SHUTDOWN,否则不能切换成功,除此以外的其他状态,是不能直接进行转换的。备库通常的状态是NOT ALLOWED,当主库做了切换以后,会变成TO PRIMARY,所以通常是现在主库做ROLE TRANSITION,然后再在备库做。

以下是一些消除主备库之间GAP的命令和说明:

--主库,将所有未传送的redo传送给从库,target_db_name使用DB_UNIQUE_NAME 。
ALTER SYSTEM FLUSH REDO TO target_db_name;

--验证备库
SELECT UNIQUE THREAD# AS THREAD, MAX(SEQUENCE#) OVER (PARTITION BY thread#) AS LAST from V$ARCHIVED_LOG;

--如果必要,拷贝归档日志到从库,并进行注册
 ALTER DATABASE REGISTER PHYSICAL LOGFILE 'filespec1';

--重复上一步,知道确认所有归档完毕。
SELECT THREAD#, LOW_SEQUENCE#, HIGH_SEQUENCE# FROM V$ARCHIVE_GAP;

--查看目标日志传输路径状态和GAP状态
SELECT STATUS, GAP_STATUS FROM V$ARCHIVE_DEST_STATUS WHERE DEST_ID = 2;

--在目标备库上,停止日志应用
ALTER DATABASE RECOVER MANAGED STANDBY DATABASE CANCEL;

--在目标备库上
ALTER DATABASE RECOVER MANAGED STANDBY DATABASE FINISH;

--如果日志确定丢失,可以采用激活方式,但这样会有数据丢失。
--ALTER DATABASE ACTIVATE PHYSICAL STANDBY DATABASE;

--验证目标备库
SELECT SWITCHOVER_STATUS FROM V$DATABASE;

--开始切换,如果状态为“TO PRIMARY”,则WITH SESSION SHUTDOWN从句可以去掉。
ALTER DATABASE COMMIT TO SWITCHOVER TO PRIMARY WITH SESSION SHUTDOWN;

--打开新主库
ALTER DATABASE OPEN;

附一份DG主备库切换状态说明:

v$database
  1. NOT ALLOWED - On a primary database, this status indicates that there are no valid and enabled standby databases. On a standby database, this status indicates that a switchover request has not been received from the primary database. 
  2.  
  3. SESSIONS ACTIVE - The database has active sessions. On a physical standby database, the WITH SESSION SHUTDOWN SQL clause must be specified to perform a role transition while in this state. On a logical standby database, a role transition can be performed while in this state, but the role transition will not complete until all current transactions have committed. 
  4.  
  5. SWITCHOVER PENDING - On a physical standby database, this status indicates that a switchover request has been received from the primary database and is being processed. A physical standby database cannot switch to the primary role while in this transient state. 
  6.  
  7. SWITCHOVER LATENT - On a physical standby database, this status indicates that a switchover request was pending, but the original primary database has been switched back to the primary role. 
  8.  
  9. TO PRIMARY - The database is ready to switch to the primary role. 
  10.  
  11. TO STANDBY - The database is ready to switch to either the physical or logical standby role. 
  12.  
  13. TO LOGICAL STANDBY - The database has received a data dictionary from a logical standby database and is ready to switch to the logical standby role. 
  14.  
  15. RECOVERY NEEDED - On a physical standby database, this status indicates that additional redo must be applied before the database can switch to the primary role. 
  16.  
  17. PREPARING SWITCHOVER - On a primary database, this status indicates that a data dictionary is being received from a logical standby database in preparation for switching to the logical standby role. On a logical standby database, this status indicates that the data dictionary has been sent to the primary database and other standby databases. 
  18.  
  19. PREPARING DICTIONARY - On a logical standby database, this status indicates that the data dictionary is being sent to the primary database and other standby databases in preparation for switching to the primary role. 
  20.  
  21. FAILED DESTINATION - On a primary database, this status indicates that one or more standby destinations are in an error state. 
  22.  
  23. RESOLVABLE GAP - On a primary database, this status indicates that one or more standby databases have a redo gap that can be automatically resolved by fetching the missing redo from the primary database or from another standby database. 
  24.  
  25. UNRESOLVABLE GAP - On a primary database, this status indicates that one or more standby databases have a redo gap that cannot be automatically resolved by fetching the missing redo from the primary database or from another standby database. 
  26.  
  27. LOG SWITCH GAP - On a primary database, this status indicates that one or more standby databases are missing redo due to a recent log switch. 

SWITCHOVER 示例:

原主库:
SQL> alter database commit to switchover to physical standby;


Database altered.


SQL> select open_mode,database_role,switchover_status from v$database;


OPEN_MODE            DATABASE_ROLE    SWITCHOVER_STATUS
-------------------- ---------------- --------------------
READ WRITE           PHYSICAL STANDBY RECOVERY NEEDED


SQL> shutdown abort
ORACLE instance shut down.
SQL> startup mount
ORACLE instance started.


Total System Global Area  413372416 bytes
Fixed Size                  2228904 bytes
Variable Size             331353432 bytes
Database Buffers           75497472 bytes
Redo Buffers                4292608 bytes
Database mounted.

原备库:
SQL> select open_mode,database_role,switchover_status from v$database;


OPEN_MODE            DATABASE_ROLE    SWITCHOVER_STATUS
-------------------- ---------------- --------------------
READ ONLY WITH APPLY PHYSICAL STANDBY TO PRIMARY


SQL> alter database commit to switchover to primary;


Database altered.


SQL> alter database open;


Database altered.


SQL> select open_mode,database_role,switchover_status from v$database;


OPEN_MODE            DATABASE_ROLE    SWITCHOVER_STATUS
-------------------- ---------------- --------------------
READ WRITE           PRIMARY          TO STANDBY

原主库(新备库):
SQL> alter database recover managed standby database using current logfile disconnect from session;


Database altered.
SQL> select open_mode,database_role,switchover_status from v$database;


OPEN_MODE            DATABASE_ROLE    SWITCHOVER_STATUS
-------------------- ---------------- --------------------
MOUNTED              PHYSICAL STANDBY RECOVERY NEEDED

此时刚转换好的新备库SWITCHOVER状态是RECOVERY NEEDED,这是正常的,主库切几次日志以后就会变成NOT ALLOWED状态了,另外这个时候由于是REDO APPLY模式,虽然是11g,但也是无法直接OPEN的,如果要OPEN,必须先停止REDO APPLY:

SQL> alter database open;
alter database open
*
ERROR at line 1:
ORA-10456: cannot open standby database; media recovery session may be in progress




SQL> alter database recover managed standby database cancel;


Database altered.


SQL> alter database open;


Database altered.

由于11g的ACTIVE DG功能(需要LISENCE,否则属于非法使用),所以即便在OPEN状态下,依然能够REDO APPLY:

SQL> alter database recover managed standby database disconnect;


Database altered.


SQL> select open_mode,database_role,switchover_status from v$database;


OPEN_MODE            DATABASE_ROLE    SWITCHOVER_STATUS
-------------------- ---------------- --------------------
READ ONLY WITH APPLY PHYSICAL STANDBY NOT ALLOWED

至此,整个SWITCHOVER就做完了,SWITCHOVER是不会丢失数据的(不同于FAILOVER),如果FAILOVER能做到没有GAP,也可以不丢数据,并且正常使用SWITCHOVER切换(见官方文档B25608,第8章)。

------------------------------------------------------------------
by aaron8219
原创文章,转载请注明链接,谢谢!
http://blog.chinaunix.net/uid-24612962-id-3842449.html
阅读(14075) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~