网上关于一主两从 switchover的文章特别少,自己测试了一下,做个记录。
数据库IP |
db_unique_name |
原角色 |
目标角色 |
192.168.3.224 |
prodcdb |
主库 |
备库 |
192.168.3.225 |
prodcdb_dg |
备库 |
主库 |
192.168.3.247 |
prodcdb_dg02 |
备库 |
备库 |
即,测试从224 switchover到225,同时保证switchover后224与247均以225为主库。
确认主从库间有没有延迟
select value from v$dataguard_stats where name='transport lag';
select value from v$dataguard_stats where name='apply lag';
如果有,检查最新归档到的日志号及是否有gap
SELECT UNIQUE THREAD# AS THREAD, MAX(SEQUENCE#) OVER (PARTITION BY thread#) AS LAST from V$ARCHIVED_LOG;
SELECT THREAD#, LOW_SEQUENCE#, HIGH_SEQUENCE# FROM V$ARCHIVE_GAP;
状态为TO STANDBY 或 SESSIONS ACTIVE均可切换,SESSIONS ACTIVE说明还有活跃会话
select switchover_status from v$database;
with session shutdown用于强制关闭活动会话
alter database commit to switchover to physical standby with session shutdown;
startup
alter database recover managed standby database using current logfile disconnect from session parallel 4; -- 已有触发器,忽略此步
检查状态,可以看到已变为从库
select OPEN_MODE, database_role, SWITCHOVER_STATUS, db_unique_name from v$database;
将fal_server参数改至新主库(可提前设好)
show parameter fal_server
至此对原主库操作已完成,下面是对原备库的操作。
两个从库状态应该均为TO PRIMARY 或 SESSIONS ACTIVE,SESSIONS ACTIVE说明还有活跃会话。
select switchover_status from v$database;
补充:如果发现not allowed状态
根据文档 Physical Standby Switchover_status Showing Not Allowed. (文档 ID 1392763.1),如果确定切换前主从是同步的,可以忽略Not Allowed 状态直接进行switchover。
alter database commit to switchover to primary with session shutdown;
select open_mode from v$database;
alter database open;
select database_role,switchover_status from v$database;
检查新主库参数log_archive_config及archive_dest
show parameter archive
log_archive_config事先已配置,log_archive_dest_3还需加上从库prodcdb_dg02
-- log_archive_config若未设置需将三节点都加上
alter system set LOG_ARCHIVE_CONFIG='DG_CONFIG=(prodcdb,prodcdb_dg,prodcdb_dg02)';
alter system set LOG_ARCHIVE_DEST_3='SERVICE=prodcdb_dg02 ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=prodcdb_dg02';
对于dg02库,也需要修改目标主库为prodcdb_dg库
show parameter archive
-- log_archive_config若未设置需将三节点都加上
alter system set LOG_ARCHIVE_CONFIG='DG_CONFIG=(prodcdb,prodcdb_dg,prodcdb_dg02)';
alter system set LOG_ARCHIVE_DEST_2='SERVICE=prodcdb_dg ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=prodcdb_dg';
alter system set FAL_SERVER='prodcdb_dg';
至此主从切换已完成,新主库应与两从库正常同步
select value from v$dataguard_stats where name='transport lag';
select value from v$dataguard_stats where name='apply lag';
表格版操作步骤
编号 | 需求项 | 操作环境 | 需求细节内容 | 说明 |
1 | 迁移前主从延迟检查 | standby,standby02 | select value from v$dataguard_stats where name='transport lag'; select value from v$dataguard_stats where name='apply lag'; 延迟应该为0 |
如果有延迟,检查最新归档到的日志号及是否有gap SELECT UNIQUE THREAD# AS THREAD, MAX(SEQUENCE#) OVER (PARTITION BY thread#) AS LAST from V$ARCHIVED_LOG; SELECT THREAD#, LOW_SEQUENCE#, HIGH_SEQUENCE# FROM V$ARCHIVE_GAP; |
2 | 主库检查switchover_status | Primary | select switchover_status from v$database; | 状态为TO STANDBY 或 SESSIONS ACTIVE均可切换,SESSIONS ACTIVE说明还有活跃会话 |
3 | 主库转为物理备库 | Primary | alter database commit to switchover to physical standby with session shutdown; | with session shutdown用于强制关闭活动会话 |
4 | 启动数据库及MRP进程 | 原Primary(已转为standby) | startup alter database recover managed standby database using current logfile disconnect from session parallel 4; |
若已创建自动启动MRP进程触发器,忽略第二步 |
5 | 检查状态并设置fal_server参数 | 原Primary(已转为standby) | select OPEN_MODE, database_role, SWITCHOVER_STATUS, db_unique_name from v$database; alter system set FAL_SERVER='prodcdb_dg'; |
应该看到 database_role 变为 physical standby fal_server参数可以提前设好 |
6 | 检查原备库状态 | standby,standby02 | SELECT SWITCHOVER_STATUS FROM V$DATABASE; | 两个从库状态应该均为TO PRIMARY 或 SESSIONS ACTIVE,SESSIONS ACTIVE说明还有活跃会话。 |
7 | 原standby库切换为主库 | standby | alter database commit to switchover to primary with session shutdown; select open_mode from v$database; alter database open; select database_role,switchover_status from v$database; |
应该看到 database_role 变为 primary |
8 | 检查并设置新主库参数 | 原standby(已转为Primary) | -- 主要检查log_archive_config及archive_dest show parameter archive alter system set LOG_ARCHIVE_DEST_3='SERVICE=prodcdb_dg02 ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=prodcdb_dg02'; |
log_archive_config若未设置需将三节点都加上 alter system set LOG_ARCHIVE_CONFIG='DG_CONFIG=(prodcdb,prodcdb_dg,prodcdb_dg02)'; |
9 | 检查并设置旁观备库参数 | standby02 | -- 主要检查log_archive_config及archive_dest show parameter archive alter system set LOG_ARCHIVE_DEST_2='SERVICE=prodcdb_dg ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=prodcdb_dg'; |
log_archive_config若未设置需将三节点都加上 alter system set LOG_ARCHIVE_CONFIG='DG_CONFIG=(prodcdb,prodcdb_dg,prodcdb_dg02)'; 将LOG_ARCHIVE_DEST_2改为新主节点对应配置 |
10 | 切换后验证 | 新standby,standby02 | select value from v$dataguard_stats where name='transport lag'; select value from v$dataguard_stats where name='apply lag'; 延迟应该为0 |
参考
http://www.dbdream.com.cn/2016/03/oracle%E6%95%B0%E6%8D%AE%E5%BA%93%E4%B8%80%E4%B8%BB%E5%A4%9A%E5%A4%87dg%E7%8E%AF%E5%A2%83%E4%B8%80%E5%AF%B9%E5%A4%9A%E4%B8%BB%E5%A4%87%E5%88%87%E6%8D%A2/
https://blog.csdn.net/Hehuyi_In/article/details/94384431