Data Guard环境中数据库的角色转换有两种,分别为Switchover和Failover,通过名称可知,前者是正常的主备库之间的角色切换,该切换方式不会丢失数据;后者是故障切换,即主库不能继续提供服务的切换,可能发生数据丢失。从12.1版本开始,切换到物理备库的操作得以简化,本篇分别对这两种切换方式进行演示。
1)验证目标备库是否做好切换
SQL> alter database switchover to orcldg verify;
Database altered.
2)启动主数据库的切换
SQL> alter database switchover to orcldg;
Database altered.
3)打开新主库
SQL> select instance_name,status from v$instance;
INSTANCE_NAME STATUS
---------------- ------------
orcldg MOUNTED
SQL> alter database open;
Database altered.
4)启动新备库
SQL> startup
ORACLE instance started.
Total System Global Area 1644167168 bytes
Fixed Size 2925024 bytes
Variable Size 1056968224 bytes
Database Buffers 570425344 bytes
Redo Buffers 13848576 bytes
Database mounted.
Database opened.
5)新备库开启日志应用
SQL> alter database recover managed standby database disconnect from session;
Database altered.
6)查看主备库状态
主库:
SQL> select name,database_role,switchover_status from v$database;
NAME DATABASE_ROLE SWITCHOVER_STATUS
--------- ---------------- --------------------
ORCL PRIMARY TO STANDBY
备库:
SQL> select name,database_role,switchover_status from v$database;
NAME DATABASE_ROLE SWITCHOVER_STATUS
--------- ---------------- --------------------
ORCL PHYSICAL STANDBY NOT ALLOWED
1)主库故障,但可启动至mount
SQL> shutdown abort
ORACLE instance shut down.
SQL> startup mount
ORACLE instance started.
Total System Global Area 1644167168 bytes
Fixed Size 2925024 bytes
Variable Size 1073745440 bytes
Database Buffers 553648128 bytes
Redo Buffers 13848576 bytes
Database mounted.
2)刷新未使用的redo至备库
SQL> alter system flush redo to orcl;
System altered.
3)备库确认是否有GAP,若有,拷贝未归档的丢失的归档日志至备库,并完成注册
SQL> select * from v$archive_gap;
注册语法:
SQL> ALTER DATABASE REGISTER PHYSICAL LOGFILE "filespec1";
4)取消备库的日志应用
SQL> alter database recover managed standby database cancel;
Database altered.
5)备库故障转移至主库
SQL> alter database failover to orcl;
Database altered.
6)打开新主库
SQL> select instance_name,status from v$instance;
INSTANCE_NAME STATUS
---------------- ------------
orcl MOUNTED
SQL> alter database open;
Database altered.
7)打开新主库后,建议做个数据库全备,另外,故障切换后,可以使用闪回数据库或使用RMAN备份将其转换为新的备库。
故障切换的场景比较复杂,可根据实际情况进行处理。
以上,就是使用switchover或failover实现物理备库角色的转换。