用户可以使用角色管理服务,进行主、备库的计划中的角色切换,这个叫switchover,或者是非计划中的角色切换,叫failover。
目的:实现主库(orcl)和从库(standby)的切换
主库参数设置检查
--检测fal_client、fal_server、standby_file_management、db_file_name_convert 、log_filename_convert是否合理
SQL> col name format a25
SQL> col VALUE format a20
select a.name,a.value from v$parameter a
where a.name like '%file_name_convert'
or a.NAME LIKE 'fal%'
or a.NAME LIKE 'standby_file%'; NAME VALUE
------------------------- --------------------
db_file_name_convert standby, orcl
log_file_name_convert standby, orcl
fal_client orcl
fal_server standby
standby_file_management AUTO
--standbylog file 是否已经创建
select GROUP#, DBID db_id, THREAD#, SEQUENCE#, BYTES, USED, ARCHIVED, STATUS, FIRST_CHANGE#, NEXT_CHANGE#,LAST_CHANGE# from v$standby_log ; SQL> select * from v$logfile;
主库切换为备库
SQL> select name, LOG_MODE, OPEN_MODE, database_role, SWITCHOVER_STATUS, db_unique_name from v$database; NAME LOG_MODE OPEN_MODE DATABASE_ROLE SWITCHOVER_STATUS DB_UNIQUE_NAME
------------------------- ------------ -------------------- ---------------- -------------------- ------------------------------
ORCL ARCHIVELOG READ WRITE PRIMARY TO STANDBY orcl SQL> alter database commit to switchover to physical standby with session shutdown; 数据库已更改。 SQL> shutdown immediate
ORA-01507: 未装载数据库 ORACLE 例程已经关闭。
SQL> startup mount
ORACLE 例程已经启动。 Total System Global Area 805875712 bytes
Fixed Size 2217672 bytes
Variable Size 595593528 bytes
Database Buffers 201326592 bytes
Redo Buffers 6737920 bytes
数据库装载完毕。
SQL> select name, LOG_MODE, OPEN_MODE, database_role, SWITCHOVER_STATUS, db_unique_name from v$database; NAME LOG_MODE OPEN_MODE DATABASE_ROLE SWITCHOVER_STATUS DB_UNIQUE_NAME
------------------------- ------------ -------------------- ---------------- -------------------- ------------------------------
ORCL ARCHIVELOG MOUNTED PHYSICAL STANDBY TO PRIMARY orcl
--新备库开启实时应用
SQL> alter database recover managed standby database disconnect from session; 数据库已更改。 SQL> select name, LOG_MODE, OPEN_MODE, database_role, SWITCHOVER_STATUS, db_unique_name from v$database; NAME LOG_MODE OPEN_MODE DATABASE_ROLE SWITCHOVER_STATUS DB_UNIQUE_NAME
------------------------- ------------ -------------------- ---------------- -------------------- ------------------------------
ORCL ARCHIVELOG MOUNTED PHYSICAL STANDBY SESSIONS ACTIVE orcl
备库切换成主库
SQL> host echo $ORACLE_SID
standby SQL> alter database commit to switchover to primary with session shutdown; 数据库已更改。 SQL> set line 9999 pagesize 9999
SQL> alter database open;
数据库已更改。
SQL> select name, LOG_MODE, OPEN_MODE, database_role, SWITCHOVER_STATUS, db_unique_name from v$database;
NAME LOG_MODE OPEN_MODE DATABASE_ROLE SWITCHOVER_STATUS DB_UNIQUE_NAME
--------- ------------ -------------------- ---------------- -------------------- ------------------------------
ORCL ARCHIVELOG READ WRITE PRIMARY TO STANDBY standby
至此切换完成
切换回原来的最初的状态
standby(新主库)上的操作
SQL> alter database commit to switchover to physical standby with session shutdown; 数据库已更改。 SQL> shutdown immediate
ORA-01507: 未装载数据库 ORACLE 例程已经关闭。
SQL> startup mount;
orcl(新备库)上的操作
SQL> !echo $ORACLE_SID
orcl SQL> alter database commit to switchover to primary with session shutdown; 数据库已更改。 SQL> alter database open; 数据库已更改。 SQL> select name, LOG_MODE, OPEN_MODE, database_role, SWITCHOVER_STATUS, db_unique_name from v$database; NAME LOG_MODE OPEN_MODE DATABASE_ROLE SWITCHOVER_STATUS DB_UNIQUE_NAME
------------------------- ------------ -------------------- ---------------- -------------------- ------------------------------
ORCL ARCHIVELOG READ WRITE PRIMARY TO STANDBY orcl
standby(新主库)上的操作
SQL> alter database recover managed standby database disconnect from session; 数据库已更改。