Oracle DG 切换
一、Switchover正常切换
1、主库
(1)、查看主库状态及角色
SQL> select name,database_role from v$database;
NAME DATABASE_ROLE
--------- ----------------
ORCL PRIMARY
SQL> select switchover_status from v$database;
SWITCHOVER_STATUS
--------------------
TO STANDBY
(2)、查看归档日志是否有中断
SQL> select status,gap_status from v$archive_dest_status where dest_id=2;
STATUS GAP_STATUS
--------- ------------------------
VALID NO GAP
(3)、提前查看备库状态
SQL> select switchover_status,name from v$database;
SWITCHOVER_STATUS NAME
-------------------- ---------
NOT ALLOWED ORCL
(4)、切换到备库,数据库会自动关闭,启动到mount状态
SQL> alter database commit to switchover to physical standby;
Database altered.
SQL> startup mount;
ORACLE instance started.
Total System Global Area 1258291200 bytes
Fixed Size 8620224 bytes
Variable Size 872417088 bytes
Database Buffers 369098752 bytes
Redo Buffers 8155136 bytes
Database mounted.
(5)、查询数据库状态及角色
SQL> select database_role from v$database;
DATABASE_ROLE
----------------
PHYSICAL STANDBY
2、备库
(1)、查询数据库角色及状态
SQL> select name,database_role from v$database;
NAME DATABASE_ROLE
--------- ----------------
ORCL PHYSICAL STANDBY
SQL> select switchover_status from v$database;
SWITCHOVER_STATUS
--------------------
TO PRIMARY
(2)、切换到主库,此时是mount状态
SQL> alter database commit to switchover to primary;
Database altered.
SQL> alter database open;
Database altered.
(3)、开启数据库查询角色信息
SQL> select name,DATABASE_ROLE from v$database;
NAME DATABASE_ROLE
--------- ----------------
ORCL PRIMARY
3、新备库
(1)、打开数据库,查看数据库状态
SQL> alter database open;
Database altered.
SQL> select open_mode from v$database;
OPEN_MODE
--------------------
READ ONLY
(2)、应用日志,查看数据库状态
SQL>alter database recover managed standby database using current logfile disconnect from session;sion;
Database altered.
SQL> select open_mode from v$database;
OPEN_MODE
--------------------
READ ONLY WITH APPLY
4、验证数据同步
二、failover
是当主库不可用的时候,强制性把备库切换成主库,以继续提供数据库服务,此时主库已经崩掉,在备库操作
1、确认日志是否同步
SQL> SELECT THREAD#, LOW_SEQUENCE#, HIGH_SEQUENCE# FROM V$ARCHIVE_GAP;
no rows selected
如果日志不同步可以使用
ALTER DATABASE REGISTER PHYSICAL LOGFILE '缺失日志文件';
2、查询日志是否应用到最新状态
select distinct thread#,max(sequence#) over(partition by thread#) a from v$archived_log;
3、切换到主库
SQL> alter database recover managed standby database finish force;
Database altered.
SQL> alter database commit to switchover to primary;
SQL> alter database commit to switchover to primary;
Database altered.
SQL> alter database open;
Database altered.
SQL> select name,database_role from v$database;
NAME DATABASE_ROLE
--------- ----------------
ORCL PRIMARY
成功切换到主库,此时DG环境已经损坏,需要重新构建。