Oracle 19c 切换保护模式和主备库角色

1 DG保护模式切换
Oracle DG有三种不同的保护模式供选择:最大可用(Maximum Availability),最大性能(Maximum Performance),最大保护(Maximum Protection)。关于他们的区别这里不再描述,可以参考官方手册的说明。 Oracle 默认采用的是最大性能。

SQL> set linesize 200
SQL> select protection_mode,database_role,protection_level from v$database; 

PROTECTION_MODE          DATABASE_ROLE          PROTECTION_LEVEL
------------------- ----------------------------------------
MAXIMUM PERFORMANCE       PRIMARY                MAXIMUM PERFORMANCE

三种模式之前切换的时候,有2个必要条件:

配置了standby redo log
Log_archve_dest_2 参数需要配置sync, affirm的方式, 这个我们在安装的时候已经配置过。

SQL> alter system set log_archive_dest_2='service=orcl_s lgwr sync affirm valid_for=(online_logfiles,primary_role) db_unique_name=orcl_s' scope=both ;

System altered.
SQL> select member from v$logfile;

MEMBER
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
/u01/app/oracle/oradata/ORCL/redo03.log
/u01/app/oracle/oradata/ORCL/redo02.log
/u01/app/oracle/oradata/ORCL/redo01.log
/u01/app/oracle/oradata/ORCL/stdredo01.log
/u01/app/oracle/oradata/ORCL/stdredo02.log
/u01/app/oracle/oradata/ORCL/stdredo03.log
/u01/app/oracle/oradata/ORCL/stdredo04.log

7 rows selected.
SQL> show parameter log_archive_dest_2

NAME                                 TYPE                              VALUE
------------------------------------ --------------------------------- ------------------------------
log_archive_dest_2                   string                            service=orcl_s lgwr sync affir
                                                                       m valid_for=(online_logfiles,p
                                                                       rimary_role) db_unique_name=or
                                                                       cl_s

1.1 最大性能切换到最大可用

SQL> select protection_mode,database_role,protection_level from v$database;                      
PROTECTION_MODE              DATABASE_ROLE              PROTECTION_LEVEL
---------------------------------------- -------------------------------- ---------------------------
MAXIMUM PERFORMANCE             PRIMARY              MAXIMUM PERFORMANCE

#在主库进行切换:
SQL> alter database set standby database to maximize availability;
Database altered.

#主库验证:
SQL> select protection_mode,database_role,protection_level from v$database;
PROTECTION_MODE              DATABASE_ROLE              PROTECTION_LEVEL
---------------------------------------- -------------------------------- --------------------------
MAXIMUM AVAILABILITY             PRIMARY              MAXIMUM AVAILABILITY

#备库验证:
SQL> select protection_mode,database_role,protection_level from v$database; 
PROTECTION_MODE              DATABASE_ROLE              PROTECTION_LEVEL
---------------------------------------- -------------------------------- -------------------------
MAXIMUM AVAILABILITY             PHYSICAL STANDBY          MAXIMUM AVAILABILITY

1.2 最大可用切换到最大保护

[oracle@dg01 ~]$ sqlplus / as sysdba

SQL*Plus: Release 19.0.0.0.0 - Production on Fri Sep 17 22:23:26 2021
Version 19.3.0.0.0

Copyright (c) 1982, 2019, Oracle.  All rights reserved.


Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.3.0.0.0

SQL> select protection_mode,database_role,protection_level from v$database; 
PROTECTION_MODE              DATABASE_ROLE              PROTECTION_LEVEL
---------------------------------------------------------- ----------------------------------------
MAXIMUM AVAILABILITY             PRIMARY              MAXIMUM AVAILABILITY

#在主库进行切换:
SQL> alter database set standby database to maximize protection;  
Database altered.

#主库验证:
SQL> select protection_mode,database_role,protection_level from v$database;
PROTECTION_MODE              DATABASE_ROLE              PROTECTION_LEVEL
------------------------------------------------------- ----------------------------------------
MAXIMUM PROTECTION             PRIMARY              MAXIMUM PROTECTION

#备库验证:
SQL> select protection_mode,database_role,protection_level from v$database; 
PROTECTION_MODE              DATABASE_ROLE              PROTECTION_LEVEL
------------------------------------------------------- ----------------------------------------
MAXIMUM PROTECTION             PHYSICAL STANDBY          MAXIMUM PROTECTION

1.3 最大保护切换到最大可用

SQL> select protection_mode,database_role,protection_level from v$database;
PROTECTION_MODE              DATABASE_ROLE              PROTECTION_LEVEL
---------------------- -------------------------------- ----------------------------------------
MAXIMUM PROTECTION             PRIMARY              MAXIMUM PROTECTION

#在主库进行切换:
SQL> alter database set standby database to maximize availability;
Database altered.

#主库验证:
SQL> select protection_mode,database_role,protection_level from v$database;
PROTECTION_MODE              DATABASE_ROLE              PROTECTION_LEVEL
-------------------------- -------------------------------- ----------------------------------------
MAXIMUM AVAILABILITY             PRIMARY              MAXIMUM AVAILABILITY

#备库验证:
SQL> select protection_mode,database_role,protection_level from v$database; 
PROTECTION_MODE              DATABASE_ROLE              PROTECTION_LEVEL
------------------------- -------------------------------- ----------------------------------------
MAXIMUM AVAILABILITY             PHYSICAL STANDBY          MAXIMUM AVAILABILITY

1.4 最大可用切换到最大性能

SQL> select protection_mode,database_role,protection_level from v$database;
PROTECTION_MODE              DATABASE_ROLE              PROTECTION_LEVEL
---------------------------- -------------------------------- ----------------------------------------
MAXIMUM AVAILABILITY             PRIMARY              MAXIMUM AVAILABILITY

#在主库进行切换:
SQL>  alter database set standby database to maximize performance;
Database altered.

#主库验证:
SQL> select protection_mode,database_role,protection_level from v$database;
PROTECTION_MODE              DATABASE_ROLE              PROTECTION_LEVEL
------------------------- -------------------------------- ----------------------------------------
MAXIMUM PERFORMANCE             PRIMARY              MAXIMUM PERFORMANCE

#备库验证:
SQL> select protection_mode,database_role,protection_level from v$database; 
PROTECTION_MODE              DATABASE_ROLE              PROTECTION_LEVEL
-------------------------- -------------------------------- ----------------------------------------
MAXIMUM PERFORMANCE            PHYSICAL STANDBY      MAXIMUM PERFORMANCE

1.5 最大性能切换到最大保护—-先切换最大可用,再切换最大保护

SQL> select protection_mode,database_role,protection_level from v$database; 

PROTECTION_MODE                                              DATABASE_ROLE                                    PROTECTION_LEVEL
------------------------------------------------------------ ------------------------------------------------ ------------------------------------------------------------
MAXIMUM PERFORMANCE                                          PRIMARY                                          MAXIMUM PERFORMANCE

SQL>  alter database set standby database to maximize availability;

Database altered.

SQL> alter database set standby database to maximize protection; 

Database altered.

2.主备切换
2.1 在主库执行命令

SQL> alter database switchover to orcl_s verify;
Database altered.

2.2 在主数据库上进行switchover

SQL> alter database switchover to orcl_s;

Database altered.

2.3 在新主库上打开数据库

[oracle@dg02 ~]$ sqlplus / as sysdba

SQL*Plus: Release 19.0.0.0.0 - Production on Fri Sep 17 22:40:44 2021
Version 19.3.0.0.0

Copyright (c) 1982, 2019, Oracle.  All rights reserved.

Connected to an idle instance.

SQL> startup mount
ORACLE instance started.

Total System Global Area 2516581456 bytes
Fixed Size                  9141328 bytes
Variable Size             654311424 bytes
Database Buffers         1845493760 bytes
Redo Buffers                7634944 bytes
Database mounted.
SQL> alter database set standby to maximize performance;

Database altered.

SQL> alter database open;

Database altered.

打开备库并并启动apply

SQL> startup
ORACLE instance started.

Total System Global Area 2516581456 bytes
Fixed Size                  9141328 bytes
Variable Size             654311424 bytes
Database Buffers         1845493760 bytes
Redo Buffers                7634944 bytes
Database mounted.
Database opened.

#启用MRP 进程:
SQL> alter database recover managed standby database disconnect;
Database altered.

#查看新备库状态,切换完成,当然可以需要验证一下主备库同步情况,可以参考第一节:
SQL> select database_role ,open_mode from v$database;
DATABASE_ROLE             OPEN_MODE
-------------------------------- ----------------------------------------
PHYSICAL STANDBY         READ ONLY WITH APPLY

3 Failover切换示例
1) 推送日志
Failover是不可逆的操作,一般仅当主库不能打开时使用,如果DG的保护模式不是最大保护,那么在激活备库之前,还是需要尽可能将未发送到备库的日志推动到备库。 只要主库能启动到mount状态就可以进行该操作。

SQL> startup mount
SQL> alter system flush redo to orcl_p;

如果不能执行该操作,那么只能手工将未传过去的日志文件传到备库,然后手工注册:

SQL> alter database register physical logfile 'filespec1';

也可以通过以下语句查询GAP信息:

SQL> select thread#, low_sequence#, high_sequence# from v$archive_gap;

2)正常的Failover 操作


备库关闭mrp

SQL> alter database recover managed standby database cancel;
Database altered.


在备库上执行failvoer

SQL> alter database failover to orcl_p;
Database altered.

如果操作正常,那么备库就已经激活。 如果报错,且alert log 日志里显示与far sync instance相关的错误,可以直接加force 选项:

SQL> alter database failvover to orcl_p force;

3)强制failover
如果正常的Failover 无法成功执行,可以采用这里的强制的激活failover,该操作可能会丢数据:

SQL> alter database activate physical standby database;
Database altered.

4) 启动新主库

SQL> alter database open;
Database altered.

SQL> select open_mode,database_role from v$database;
OPEN_MODE                 DATABASE_ROLE
---------------------------------------- --------------------------------
READ WRITE                 PRIMARY

你可能感兴趣的:(linux系统,Oracle,oracle,数据库)