Data Guard配置由一个主库和一个或多个备库组成,可以在视图V$Database的Database_Role字段查看数据库的当前角色。
一 角色转换介绍
数据库有两种角色,分别是Primary和Standby,它运行在两种角色之一,Data Guard角色的改变既可以通过执行SQL语句动态的改变,也可以通过使用Data Guard broker接口改变,Oracle Data Guard支持下面两种方式的角色转换:
- Switchover
允许主库与其中一个备库交换角色,在切换过程中不会有数据丢失。切换之后,每个数据库将使用新的角色继续参与Data Guard配置。
- Failover
当主库发生故障后,将备库角色切换为主库,如果主库在故障前不是以最大保护模式或者最大可用性模式运行,可能会出现数据丢失的情况。如果主库启用了闪回数据库,那么主库一旦被修复,它就可以恢复为新的主库的备库,从而继续提供服务。
二 角色转换演示
Switchover演示
1、查询主库的状态
SQL> select name,log_mode,open_mode,protection_mode,database_role,switchover_status from v$database;
NAME LOG_MODE OPEN_MODE PROTECTION_MODE DATABASE_ROLE SWITCHOVER_STATUS
--------- ------------ -------------------- -------------------- ---------------- --------------------
SZPRI ARCHIVELOG READ WRITE MAXIMUM PERFORMANCE PRIMARY TO STANDBY
注:SWITCHOVER_STATUS是STANDBY或SESSION ACTIVE显示主库可以切换到备库角色,如果不是这两者之一,则不能进行主备切换。
2、启动主库的切换
SQL> alter database commit to switchover to physical standby with session shutdown ;
Database altered.
注:在切换至备库前,当前控制文件被备份到当前会话的Trace文件,如果需要,可以重建当前控制文件。如果SWITCHOVER_STATUS的值是STANDBY,那么with session shutdown可以省略。本语句执行后,数据库处于NOMOUNT状态。
3、启动数据库至MOUNT状态
SQL> startup mount;
ORACLE instance started.
Total System Global Area 830930944 bytes
Fixed Size 2257800 bytes
Variable Size 536874104 bytes
Database Buffers 289406976 bytes
Redo Buffers 2392064 bytes
Database mounted.
4、在新的备库启动日志应用
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup
ORACLE instance started.
Total System Global Area 830930944 bytes
Fixed Size 2257800 bytes
Variable Size 536874104 bytes
Database Buffers 289406976 bytes
Redo Buffers 2392064 bytes
Database mounted.
Database opened.
SQL> alter database recover managed standby database disconnect using current logfile;
Database altered.
5、验证备库的状态
SQL> select name,log_mode,open_mode,protection_mode,database_role,switchover_status from v$database;
NAME LOG_MODE OPEN_MODE PROTECTION_MODE DATABASE_ROLE SWITCHOVER_STATUS
--------- ------------ -------------------- -------------------- ---------------- --------------------
SZPRI ARCHIVELOG READ ONLY WITH APPLY MAXIMUM PERFORMANCE PHYSICAL STANDBY TO PRIMARY
6、切换备库为主库
SQL> alter database commit to switchover to primary with session shutdown;
Database altered.
SQL> alter database open;
Database altered.
SQL> select name,log_mode,open_mode,protection_mode,database_role,switchover_status from v$database;
NAME LOG_MODE OPEN_MODE PROTECTION_MODE DATABASE_ROLE SWITCHOVER_STATUS
--------- ------------ -------------------- -------------------- ---------------- --------------------
SZPRI ARCHIVELOG READ WRITE MAXIMUM PERFORMANCE PRIMARY SESSIONS ACTIVE
7、验证切完后数据同步
- 主库
SQL> select name from v$datafile;
NAME
--------------------------------------------------
/u01/app/oracle/oradata/szpri/system01.dbf
/u01/app/oracle/oradata/szpri/sysaux01.dbf
/u01/app/oracle/oradata/szpri/undotbs01.dbf
/u01/app/oracle/oradata/szpri/users01.dbf
/u01/app/oracle/oradata/szpri/example01.dbf
SQL> create tablespace test datafile '/u01/app/oracle/oradata/szpri/test01.dbf' size 50M;
Tablespace created.
SQL> select name from v$datafile;
NAME
--------------------------------------------------
/u01/app/oracle/oradata/szpri/system01.dbf
/u01/app/oracle/oradata/szpri/sysaux01.dbf
/u01/app/oracle/oradata/szpri/undotbs01.dbf
/u01/app/oracle/oradata/szpri/users01.dbf
/u01/app/oracle/oradata/szpri/example01.dbf
/u01/app/oracle/oradata/szpri/test01.dbf
6 rows selected.
- 备库
SQL> select name from v$datafile;
NAME
--------------------------------------------------
/u01/app/oracle/oradata/szpri/system01.dbf
/u01/app/oracle/oradata/szpri/sysaux01.dbf
/u01/app/oracle/oradata/szpri/undotbs01.dbf
/u01/app/oracle/oradata/szpri/users01.dbf
/u01/app/oracle/oradata/szpri/example01.dbf
/u01/app/oracle/oradata/szpri/test01.dbf
6 rows selected.