Oracle Data Guard 主备切换(rac to rac的adg架构)
一、Switchover(可逆)
一般SWITCHOVER切换都是计划中的切换,特点是在切换后,不会丢失任何的数据,而且这个过程是可逆的,整个DATA GUARD环境不会被破坏,原来DATA GUARD环境中的所有物理和逻辑STANDBY都可以继续工作。
在进行DATA GUARD的物理STANDBY切换前需要注意的事项:
1)确认主库和从库间网络连接通畅;
2)确认没有活动的会话连接在数据库中;
3)PRIMARY数据库处于打开的状态,STANDBY数据库处于MOUNT状态;
4)确保STANDBY数据库处于ARCHIVELOG模式;
5)如果设置了REDO应用的延迟,那么将这个设置去掉;
6)确保配置了主库和从库的初始化参数,使得切换完成后,DATA GUARD机制可以顺利的运行。
(注:rac to rac的adg切换只需在其中一个节点执行相应命令即可,执行完后,再把另外的节点open。)
----顺切主备(主库:testdb 备库:testdbdg)
主库:
1、查看switchover 状态
SYS@testdb1> select name,database_role,switchover_status from v$database;
NAME DATABASE_ROLE SWITCHOVER_STATUS
TESTDB PRIMARY TO STANDBY
附:A:switchover_status出现session active/not allowed
当出现session active的时候表示还有活动的session,则运行
Alter database commit to switchover to physical standby with session shutdown;
当出现not allowed时,说明切换标记还没收到,此时不能执行转换。
当主库备库状态都正常,并且没有进行主备切换的操作时,备库的switchover_status就是not allowed。当主库执行了切换成备库的操作后,备库的switchover_status才是to primary。
B:ora- 01153: an incompatible media recovery is active
运行下面代码
Alter database recover managed standby database finish;
或者Alter database recover managed standby database finish force;
Alter database recover managed standby database disconnect from session;
2、切换成备库(在主库1号机执行即可)
SYS@testdb1> alter database commit to switchover to physical standby with session shutdown;
或者
(SYS@testdb1> alter database commit to switchover to physical standby;
Database altered. )
3、启动到mount状态和实时应用日志
SYS@pri> shutdown immediate;
若此时提示ORA-01092: ORACLE instance terminated. Disconnection forced,则quit退出,重新sqlplus登陆数据库即可。
SYS@testdb1> startup nomount;
SYS@testdb1> alter database mount standby database;
(或者直接startup)
SYS@testdb1> alter database recover managed standby database using current logfile disconnect from session;
4、查看数据库模式
SYS@testdb1> select name,database_role,switchover_status from v$database;
NAME DATABASE_ROLE SWITCHOVER_STATUS
TESTDB PHYSICAL STANDBY TO PRIMARY
如上,此时主库已经切换为物理备库
备库:
1、查看switchover状态
SYS@testdbdg1> select name,database_role,switchover_status from v$database;
NAME DATABASE_ROLE SWITCHOVER_STATUS
TESTDB PHYSICAL STANDBY TO PRIMARY
补充:若出现:ORA-16139: media recovery required
是因为没有执行:alter database recover managed standby database disconnect from session;
2、切换成主库,并打开数据库
SYS@testdbdg1> alter database commit to switchover to primary;
SYS@testdbdg1> startup
3、查看数据库模式
SYS@testdbdg1> select name,database_role,switchover_status from v$database;
NAME DATABASE_ROLE SWITCHOVER_STATUS
TESTDB PRIMARY SESSIONS ACTIVE
如上,物理备库已成功切换为主库
将备库的状态由mount修改为read only状态(如果前面使用的是startup,则不需要进行以下操作)
SYS@testdb1> select open_mode from v$database;
MOUNTED
–取消日志应用
SYS@testdb1> alter database recover managed standby database cancel;
Database altered.
–开库
SYS@testdb1> alter database open;
Database altered.
–打开实时日志应用
SYS@testdb1> alter database recover managed standby database using current logfile disconnect from session;
Database altered.
SYS@testdb1> select open_mode from v$database;
READ ONLY WITH APPLY
–将主库2号机打开
SYS@testdbdg2> select open_mode from v$database;
MOUNTED
SYS@testdbdg2> alter database open;
Database altered.
SYS@testdbdg2> select open_mode from v$database;
READ WRITE
–将备库2号机打开
SYS@testdb2> startup
SYS@testdb2> select open_mode from v$database;
READ ONLY WITH APPLY
查看状态
主库:
SYS@testdbdg1> select name,database_role,open_mode,switchover_status from gv$database;
NAME DATABASE_ROLE OPEN_MODE SWITCHOVER_STATUS
TESTDB PRIMARY READ WRITE SESSIONS ACTIVE
TESTDB PRIMARY READ WRITE SESSIONS ACTIVE
备库:
SYS@testdb1> select name,database_role,open_mode,switchover_status from gv$database;
NAME DATABASE_ROLE OPEN_MODE SWITCHOVER_STATUS
TESTDB PHYSICAL STANDBY READ ONLY WITH APPLY NOT ALLOWED
TESTDB PHYSICAL STANDBY READ ONLY WITH APPLY NOT ALLOWED
-----------------------------------------------------顺切结束-----------------------------------------------
-----回切主备(主库:testdbdg 备库:testdb)
主库:
1、查看switchover 状态
SYS@testdbdg1> select name,database_role,switchover_status from v$database;
NAME DATABASE_ROLE SWITCHOVER_STATUS
TESTDB PRIMARY SESSIONS ACTIVE
附:A:switchover_status出现session active/not allowed
当出现session active的时候表示还有活动的session,则运行
Alter database commit to switchover to physical standby with session shutdown;
当出现not allowed时,说明切换标记还没收到,此时不能执行转换。
当主库备库状态都正常,并且没有进行主备切换的操作时,备库的switchover_status就是not allowed。当主库执行了切换成备库的操作后,备库的switchover_status才是to primary。
B:ora- 01153: an incompatible media recovery is active
运行下面代码
Alter database recover managed standby database finish;
或者Alter database recover managed standby database finish force;
Alter database recover managed standby database disconnect from session;
2、切换成备库
SYS@testdbdg1> alter database commit to switchover to physical standby with session shutdown;
或者
(SYS@pri> alter database commit to switchover to physical standby;
Database altered. )
3、启动到mount状态和实时应用日志
SYS@pri> shutdown immediate;
若此时提示ORA-01092: ORACLE instance terminated. Disconnection forced,则quit退出,重新sqlplus登陆数据库即可。
SYS@testdbdg1> startup nomount;
SYS@testdbdg1> alter database mount standby database;
(或者使用startup直接开库)
SYS@testdbdg1> alter database recover managed standby database using current logfile disconnect from session;
4、查看数据库模式
SYS@testdbdg1> select name,database_role,switchover_status from v$database;
NAME DATABASE_ROLE SWITCHOVER_STATUS
TESTDB PHYSICAL STANDBY TO PRIMARY
如上,此时主库已经切换为物理备库
备库:
1、查看switchover状态
SYS@testdb1> select name,database_role,switchover_status from v$database;
NAME DATABASE_ROLE SWITCHOVER_STATUS
TESTDB PHYSICAL STANDBY TO PRIMARY
补充:若出现:ORA-16139: media recovery required
是因为没有执行:alter database recover managed standby database disconnect from session;
2、切换成主库,并打开数据库
SYS@testdb1> alter database commit to switchover to primary;
SYS@testdb1>startup
3、查看数据库模式
SYS@std> select name,database_role,switchover_status from v$database;
NAME DATABASE_ROLE SWITCHOVER_STATUS
PRI PRIMARY TO STANDBY
如上,物理备库已成功切换为主库
备库打开为read only状态(如果前面使用的是startup开库,则不需要以下操作)
SYS@testdbdg1> select open_mode from v$database;
MOUNTED
SYS@testdbdg1> alter database recover managed standby database cancel;
Database altered.
SYS@testdbdg1> alter database open;
Database altered.
SYS@testdbdg1> alter database recover managed standby database using current logfile disconnect from session;
Database altered.
SYS@testdbdg1> select open_mode from v$database;
READ ONLY WITH APPLY
–将主库2号机打开
SYS@testdb2> select open_mode from v$database;
MOUNTED
SYS@testdb2> alter database open;
Database altered.
SYS@testdb2> select open_mode from v$database;
READ WRITE
–将备库2号机打开
SYS@testdbdg2> startup
SYS@testdbdg2> select open_mode from v$database;
READ ONLY WITH APPLY
查看状态
主库:
SYS@testdb1> select name,database_role,open_mode,switchover_status from gv$database;
NAME DATABASE_ROLE OPEN_MODE SWITCHOVER_STATUS
TESTDB PRIMARY READ WRITE SESSIONS ACTIVE
TESTDB PRIMARY READ WRITE SESSIONS ACTIVE
备库:
SYS@testdbdg1> select name,database_role,open_mode,switchover_status from gv$database;
NAME DATABASE_ROLE OPEN_MODE SWITCHOVER_STATUS
TESTDB PHYSICAL STANDBY READ ONLY WITH APPLY NOT ALLOWED
TESTDB PHYSICAL STANDBY READ ONLY WITH APPLY NOT ALLOWED
------------------------------------------回切主备结束-----------------------------------------------
---------------------------------至此Switchover切换结束-------------------------------------------
二、Failover(不可逆)
FAILOVER切换一般是PRIMARY数据库发生故障后的切换,这种情况是STANDBY数据库发挥其作用的情况。根据保护模式的不同,可能会没有或者很少的数据损失。。而且这个过程不是可逆的,DATA GUARD环境会被破坏。
由于PRIMARY数据库已经无法启动,所以FAILOVER切换所需的条件并不多,只要检查STANDBY是否运行在最大保护模式下,如果是的话,需要将其置为最大性能模式,否则切换到PRIMARY角色也无法启动。 主库意外宕机,备库切换为主库(切换过程不可逆,会破坏dg架构)
(将主库关闭,模拟宕机:srvctl stop database -d testdb)
1、查看当前保护模式,确保当前保护模式为最大性能模式,否则切换到PRIMARY角色也无法启动:
查看:
SYS@testdbdg1> select name,database_role,open_mode,protection_mode from gv$database;
NAME DATABASE_ROLE OPEN_MODE PROTECTION_MODE
TESTDB PHYSICAL STANDBY READ ONLY WITH APPLY MAXIMUM PERFORMANCE
TESTDB PHYSICAL STANDBY READ ONLY WITH APPLY MAXIMUM PERFORMANCE
修改:
alter database set standby database to maximize performance;
2、查看是否有日志GAP,没有应用的日志:
SYS@testdbdg1> select thread#,low_sequence#,high_sequence# from v$archive_gap;
no rows selected
(no rows selected,表示没有GAP。)
附:如果存在日志gap,可以使用以下两种方法修复。
方法一、使用flush将主数据库中任何未发送的存档和当前redo刷新到备用数据库。
SQL> ALTER SYSTEM FLUSH REDO TO target_db_name;
(target_db_name为备库的DB_UNIQUE_NAME )
前提:
1)、数据库版本为11.2及以上 ;
2)、主库能mount;
3)、备库启用日志应用。
方法二、将丢失的存档重日志文件从主数据库复制到备用数据库,并在备用数据库中注册它们。(必须为每个redo线程执行此操作。)
1)、将丢失的存档重日志文件从主数据库复制到备用数据库;
2)、注册:SQL> ALTER DATABASE REGISTER PHYSICAL LOGFILE ‘filespec1’;
3)、再次查看是否有日志gap。
3、备库停止应用归档:
SYS@testdbdg1> alter database recover managed standby database cancel;
4、将STANDBY数据库切换为PRIMARY数据库:
完成所有日志应用:
SYS@testdbdg1> alter database recover managed standby database finish;
(或:alter database recover managed standby database finish force;)
5、切换为主库:
SYS@testdbdg1> alter database commit to switchover to primary;
6、OPEN主库:
SYS@testdbdg1> select open_mode from v$database;
MOUNTED
SYS@testdbdg1> alter database open;
(或:shutdown immediate + startup )
7、检查数据库是否已经切换成功:
SYS@testdbdg1> select name,database_role,open_mode,protection_mode from v$database;
NAME DATABASE_ROLE OPEN_MODE PROTECTION_MODE
TESTDB PRIMARY READ WRITE MAXIMUM PERFORMANCE
8、打开2号机
SYS@testdbdg2> select open_mode from v$database;
MOUNTED
SYS@testdbdg2> alter database open;
Database altered.
SYS@testdbdg2> select open_mode from v$database;
READ WRITE
9、检查
SYS@testdbdg1> select name,database_role,open_mode,protection_mode from gv$database;
NAME DATABASE_ROLE OPEN_MODE PROTECTION_MODE
TESTDB PRIMARY READ WRITE MAXIMUM PERFORMANCE
TESTDB PRIMARY READ WRITE MAXIMUM PERFORMANCE
---------------------------------至此Failover切换结束-------------------------------------------