欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

[Oracle] Data Guard 之 浅析Switchover与Failover

程序员文章站 2023-01-08 11:51:29
data guard主从库之间的角色切换分为以下两种:1)switchoverswithchover通常都是人为的有计划的进行角色互换,比如升级等。它通常都是无损的,即不会...

data guard主从库之间的角色切换分为以下两种:
1)switchover
swithchover通常都是人为的有计划的进行角色互换,比如升级等。它通常都是无损的,即不会有数据丢失。其执行主要分为两个阶段:
1.primary转为standby
2.standby(之一)转为primary
2)failover
failover是指由于primary故障无法短时间恢复,standby不得不充当primay的角色,如果处于最高性能模式,这种切换很有可能导致数据丢失。

下面分别演示物理standby的switchover和failover(逻辑standby的切换类似)。

1. 物理standby执行switchover
1) 确认pirmary是否可以切换为standby
primary>select switchover_status from v$database;
switchover_status
--------------------
to standby
2) 切换primary为standby
primary>alter database commit to switchover to physical standby;
3) 启动原primary到mount状态
此时原primary就是以standby身份在运行了。
4) 确认standby是否可以切换为primary
standby>select switchover_status from v$database;
switchover_status
--------------------
to primary
5) 切换standby为primary
standby>alter database commit to switchover to primary;
database altered.
standby>alter database open;
database altered.
6) 验证
primary>select database_role from v$database;
database_role
----------------
physical standby
standby>select database_role from v$database;
database_role
----------------
primary
可以看到原来的primary现在变成standby,原来的standby变成primary了。
检查新的primary的log_archive_dest参数的设置是否正确:
standby>show parameter log_archive_dest_2
name                                 type        value
------------------------------------ ----------- ------------------------------
log_archive_dest_2            string      service=o01dms0 arch valid_for
                                                              =(online_logfiles,primary_role
                                                               ) db_unique_name=o01dms0
standby>show parameter log_archive_dest_state_2
name                                 type        value
------------------------------------ ----------- ---------------------
log_archive_dest_state_2             string      enable
在新primary端做log switch:
standby>alter system switch logfile;
system altered.
standby>select max(sequence#) from v$archived_log;
max(sequence#)
--------------
           479
在新standby端查看:
primary>select max(sequence#) from v$archived_log;
max(sequence#)
--------------
           479
两边的归档日志的最大序号吻合,说明switchover成功。

2. 物理standby执行failover
1). 检查standby归档文件是否连续
standby>select thread#, low_sequence#, high_sequence# from v$archive_gap;
no rows selected
这一步很重要,必须确保所有已生成的归档文件都已传到standby服务器。
如果上述语句有返回结果,则必须把缺失的归档文件从primary端复制到standby端,然后通过以下命令将其加入数据库:
standby>alter database register physical logfile 'filespec'
2). 检查standby归档文件是否完整
standby>select unique thread# as thread, max(sequence#) over (partition by threa
d#) as last from v$archived_log;
    thread       last
---------- ----------
         1        479
通过上述语句可以查出standby目前序号最大的归档日志为479,如果在primary端有比479更大的归档日志,把它们拷贝过来,然后通过以下命令将其加入数据库:
standby>alter database register physical logfile 'filespec'
3)启动failover
alter database recover managed standby database finish force;4)切换为primaryter database commit to switchover to primary;
alter database open;standby>select database_role from v$database;database_role----------------primary