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

物理dataguard 正常切换 脚色转换,switchover_status 状态改变

程序员文章站 2022-07-06 11:53:39
正常切换切换前: 主库:SQL> select DATABASE_ROLE from v$database;DATABASE_ROLE PRIMARY SQL> select OPEN_MODE,PROTECTION_MODE,PROTECTION_LEVEL,SWITCHOVER_STATUS f ......

正常切换
切换前:
主库:
sql> select database_role from v$database;
database_role
----------------
primary

sql> select open_mode,protection_mode,protection_level,switchover_status from v$database;
open_mode protection_mode protection_level switchover_status
---------- -------------------- -------------------- ------------------
read write maximum performance maximum performance to standby

sql> select count(*) from v$session where username is not null;
count(*)
----------
1

在切换前,杀掉所有的数据库连接 观察switchover_status,如果是 to standby,则可以直接切换
如果sessions active ,则用

备用库 :
sql> select database_role from v$database;
database_role
----------------
physical standby

sql> select open_mode,protection_mode,protection_level,switchover_status from v$database;
open_mode protection_mode protection_level switchover_status
---------- -------------------- -------------------- ------------------
mounted maximum performance maximum performance not allowed

说明:
主库需要注意事项
a 如果switchover_status为to_standby说明可以转换
直接转换
alter database commit to switchover to physical standby;
b 如果switchover_status为sessions active 则关闭会话
sql>alter database commit to switchover to physical standby with session shutdown;

在备库中操作,查看备库
sql> select switchover_status from v$database;

a 如果switchover_status为to_primary 说明标记恢复可以直接转换为primary库
sql>alter database commit to switchover to primary

b 如果switchover_status为session active 就应该断开活动会话
sql>alter database commit to switchover to primary with session shutdown;

c 如果switchover_status为not allowed 说明切换标记还没收到,此时不能
执行转换。

切换中 :

备用库
sql> select database_role from v$database;

database_role
----------------
physical standby

sql> select open_mode,protection_mode,protection_level,switchover_status from v$database;

open_mode protection_mode protection_level switchover_status
---------- -------------------- -------------------- ------------------
mounted maximum performance maximum performance sessions active

切换后备用的状态,模式:

sql> select database_role from v$database;

database_role
----------------
primary

sql> select open_mode,protection_mode,protection_level,switchover_status from v$database;

open_mode protection_mode protection_level switchover_status
---------- -------------------- -------------------- ------------------
read write maximum performance maximum performance to standby

2. 检查环境:

确认主库和从库间网络连接通畅;
确认没有活动的会话连接在数据库中;
primary数据库处于打开的状态,standby数据库处于mount状态;
确保standby数据库处于archivelog模式;
如果设置了redo应用的延迟,那么将这个设置去掉;
确保配置了主库和从库的初始化参数,使得切换完成后,data guard机制可以顺利的运行。
如果是最大保护模式,先变成最大性能模式:

3. 切换的顺序: 先从主库到备用,再从备库到主库
主切备:
sql> select database_role from v$database;

database_role
----------------
primary

sql> select open_mode,protection_mode,protection_level,switchover_status from v$database;

open_mode protection_mode protection_level switchover_status
---------- -------------------- -------------------- ------------------
read write maximum performance maximum performance to standby
sql>
sql> alter database commit to switchover to physical standby ;
database altered.

sql> shutdown immediate;
ora-01507: database not mounted
oracle instance shut down.
sql> startup nomount ;
oracle instance started.

total system global area 3242987696 bytes
fixed size 733360 bytes
variable size 1174405120 bytes
database buffers 2063597568 bytes
redo buffers 4251648 bytes
sql> alter database mount standby database;
database altered.

sql> alter database recover managed standby database disconnect from session;
database altered.

sql> select database_role from v$database;
database_role
----------------
physical standby

sql> select open_mode,protection_mode,protection_level,switchover_status from v$database;
open_mode protection_mode protection_level switchover_status
---------- -------------------- -------------------- ------------------
mounted maximum performance maximum performance to primary

备切主
sql> select database_role from v$database;
database_role
----------------
physical standby

sql> select open_mode,protection_mode,protection_level,switchover_status from v$database;
open_mode protection_mode protection_level switchover_status
---------- -------------------- -------------------- ------------------
mounted maximum performance maximum performance to primary

sql> alter database commit to switchover to primary;
database altered.

sql>
sql> shutdown immediate;
ora-01507: database not mounted

oracle instance shut down.
sql> startup ;
oracle instance started.

total system global area 3242987696 bytes
fixed size 733360 bytes
variable size 1174405120 bytes
database buffers 2063597568 bytes
redo buffers 4251648 bytes
database mounted.
database opened.

修改主 备库 的 tnsnames.ora
主库sessions active是正常primary状态下的结果。
dataguard:physicalstandby failover
接physical standby switchover
primary db :db2
standby db :db1

一般情况下failover都是表示primary数据库瘫痪,最起码也是起不来了,因此这种类型的切换基本上不需要primary数据库做什么操作。所以下列步骤中如果有提到primary和standby执行的,只是建议你如果primary还可以用,那就执行一下,即使不去执行,对failover来说也没有关系
1.检查归档文件是否连续,是否有gap
在standby库执行
sql> select thread#,low_sequence#,high_sequence# from v$archive_gap;
2.如果步骤1查询出来纪录,则在primary库上执行,否则跳过此步骤
在主库上执行语句,按步骤1查询出来的纪录找出归档文件
 sql> select name from v$archived_log where thread#=1 and dest_id=1 and sequence# between xx and xx;
  --如果primary存在,拷贝相应的归档到standby数据库,并注册.
  sql> alter database register physical logfile 'xxx';

3.检查归档文件是否完整
分别在primary/standby执行下列语句:
  sql> select distinct thread#,max(sequence#) over(partition by thread#) a from v$archived_log;
  把相差的归档复制到待转换的standby服务器,并手工register

4.开始做failover
察看standby进程状态
sql> select process,client_process,sequence#,status from v$managed_standby;
process client_p sequence# status
--------- -------- ---------- ------------
arch arch 30 closing
arch arch 0 connected
rfs lgwr 31 idle
rfs arch 0 idle
rfs n/a 0 idle
mrp0 n/a 31 applying_log

sql> alter database recover managed standby database finish force ;
force关键字将会停止当前活动的rfs进程,以便立刻执行failover。
  或
  sql> alter database recover managed standby database finish skip standby logfile;
sql> alter database commit to switchover to primary;
sql> shutdown immediate
sql> startup
sql> select database_role,protection_mode,protection_level,open_mode from v$database;
database_role protection_mode protection_level open_mode
---------------- -------------------- -------------------- ----------
primary maximum performance maximum performance read write
failover切换成功!


dataguard验证是否正常同步的2种方法
1、
最简单的方法,备库查看最新的归档日志是否apply,如果applied是yes则正常同步
主库sqlplus>
alter system switch logfile;
备库sqlplus>
select sequence#,applied,first_time,next_time from v$archived_log order by 1 desc;

2、
主库lns进程的block#是否变动、备库查看rfs进程的block#是否变动,如果block#都变动,则正常同步
主库sqlplus>
select process,status,sequence#,block# from v$managed_standby where process='lns';
备库sqlplus>
select process,client_process,sequence#,block# from v$managed_standby where process='rfs';

dataguard主备延迟多长时间的2种查询方法
1、
备库sqlplus>
select value from v$dataguard_stats where name='apply lag';
2、
备库sqlplus>
select ceil((sysdate-next_time)*24*60) "m" from v$archived_log where applied='yes' and sequence#=(select max(sequence#) from v$archived_log where applied='yes');

//
检查主备两边的序号
select max(sequence#) from v$log;

备库执行,查看是否有数据未应用
select name,sequence#,applied from v$archived_log order by sequence#;
select sequence#,first_time,next_time ,applied from v$archived_log order by 1;

检查备库是否开启实时应用
select recovery_mode from v$archive_dest_status where dest_id=2;

检查备库状态
select switchover_status from v$database; --发现状态not allowed

看看进程mrp是否存在
ps aux|grep mrp --发现进程不存在

如果不存在执行以下:
alter database recover managed standby database using current logfile disconnect;
alter database recover managed standby database disconnect from session; --后台执行
alter database recover managed standby database --前台执行,执行这个可以看到报错的情况
如果有报错,查看alert日志和log.xml日志

验证是否正常
select process,status from v$managed_standby;
select process,status,sequence# from v$managed_standby;
如果看到mrp0正常

6、以上步骤处理好后,如果数据还不正常,接着处理

col open_mode for a10
col database_role for a20
col protection_mode for a20
col protection_level for a20
col switchover_status for a20
select open_mode,database_role,protection_mode,protection_level,switchover_status from v$database;

转载于

switch_status not allowed等物理dataguard 正常切换 脚色转换
https://blog.csdn.net/demonson/article/details/17913217