一 角色转换介绍
数据库有两种角色,分别是Primary和Standby,它运行在两种角色之一,Data Guard角色的改变既可以通过执行SQL语句动态的改变,也可以通过使用Data Guard broker接口改变,Oracle Data Guard支持下面两种方式的角色转换:
- Switchover
- Failover
二 角色转换演示
Switchover演示 1、查询主库的状态
SQL> select name,log_mode,open_mode,protection_mode,database_role,switchover_status from v$database;注:SWITCHOVER_STATUS是STANDBY或SESSION ACTIVE显示主库可以切换到备库角色,如果不是这两者之一,则不能进行主备切换。 2、启动主库的切换
NAME LOG_MODE OPEN_MODE PROTECTION_MODE DATABASE_ROLE SWITCHOVER_STATUS
--------- ------------ -------------------- -------------------- ---------------- --------------------
SZPRI ARCHIVELOG READ WRITE MAXIMUM PERFORMANCE PRIMARY TO STANDBY
SQL> alter database commit to switchover to physical standby with session shutdown ;注:在切换至备库前,当前控制文件被备份到当前会话的Trace文件,如果需要,可以重建当前控制文件。如果SWITCHOVER_STATUS的值是STANDBY,那么with session shutdown可以省略。本语句执行后,数据库处于NOMOUNT状态。 3、启动数据库至MOUNT状态
Database altered.
SQL> startup mount;4、在新的备库启动日志应用
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.
SQL> shutdown immediate5、验证备库的状态
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.
SQL> select name,log_mode,open_mode,protection_mode,database_role,switchover_status from v$database;6、切换备库为主库
NAME LOG_MODE OPEN_MODE PROTECTION_MODE DATABASE_ROLE SWITCHOVER_STATUS
--------- ------------ -------------------- -------------------- ---------------- --------------------
SZPRI ARCHIVELOG READ ONLY WITH APPLY MAXIMUM PERFORMANCE PHYSICAL STANDBY TO PRIMARY
SQL> alter database commit to switchover to primary with session shutdown;7、验证切完后数据同步
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
- 主库
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.