环境准备
接着上一篇文章 Oracle11g Data Guard物理备用数据库搭建与配置(第1部分 主数据库实例创建)
在我的测试环境中,我准备了两台CentOS7.4虚拟机,并同时都安装了Oracle11gR2的11.2.0.1.0企业版的数据库软件,其中只有主服务器创建一个数据库实例,备用服务器仅安装Oracle数据库软件。
主数据库:
OS: CentOS7.4
Hostname: hmdb11dg-db1
Oracle Version: 11.2.0.1.0
Oracle SID: HMDG(使用DBCA工具创建的一个数据库)
备数据库:
OS: CentOS7.4
Hostname: hmdb11dg-db2
Oracle Version: 11.2.0.1.0
Oracle SID: HMDG2
注意: 在开始之前,备用服务上还没有将要与主服务器同步备份的数据库实例。
一、主服务器前提配置
1. 启用归档日志
检查主数据是否处于归档日志模式
SQL> select log_mode from v$database;
LOG_MODE
------------
ARCHIVELOG
如果是NOARCHIVELOG模式,则将其修改为ARCHIVELOG模式
SQL> SHUTDOWN IMMEDIATE;
SQL> STARTUP MOUNT;
SQL> ALTER DATABASE ARCHIVELOG;
SQL> ALTER DATABASE OPEN;
2. 启用强制日志
SQL> ALTER DATABASE FORCE LOGGING;
Database altered.
SQL> SELECT NAME,FORCE_LOGGING FROM V$DATABASE;
NAME FOR
--------- ---
HMDG YES
3. 创建备用重做日志
备用重做日志文件的大小要与当前主数据库的在线重做日志文件大小完全匹配。
确定备用重做日志文件组的数量,建议的数量:(每个线程最大的日志数 + 1) * 最大线程数
查看联机重做日志
SQL> set pagesize 100
SQL> set linesize 200
SQL> col GROUP# format 99
SQL> col STATUS format a10
SQL> col TYPE format a10
SQL> col MEMBER format a50
SQL> col IS_RECOVERY_DEST_FILE format a10
SQL> select * from v$logfile;
GROUP# STATUS TYPE MEMBER IS_RECOVER
------ ---------- ---------- -------------------------------------------------- ----------
1 ONLINE /u01/app/oracle/oradata/HMDG/redo01.log NO
2 ONLINE /u01/app/oracle/oradata/HMDG/redo02.log NO
3 ONLINE /u01/app/oracle/oradata/HMDG/redo03.log NO
4 ONLINE /u01/app/oracle/oradata/HMDG/redo04.log NO
这里将创建10组备用重做日志
ALTER DATABASE ADD STANDBY LOGFILE GROUP 5 '/u01/app/oracle/oradata/HMDG/redo05.log' SIZE 500M;
ALTER DATABASE ADD STANDBY LOGFILE GROUP 6 '/u01/app/oracle/oradata/HMDG/redo06.log' SIZE 500M;
ALTER DATABASE ADD STANDBY LOGFILE GROUP 7 '/u01/app/oracle/oradata/HMDG/redo07.log' SIZE 500M;
ALTER DATABASE ADD STANDBY LOGFILE GROUP 8 '/u01/app/oracle/oradata/HMDG/redo08.log' SIZE 500M;
ALTER DATABASE ADD STANDBY LOGFILE GROUP 9 '/u01/app/oracle/oradata/HMDG/redo09.log' SIZE 500M;
ALTER DATABASE ADD STANDBY LOGFILE GROUP 10 '/u01/app/oracle/oradata/HMDG/redo10.log' SIZE 500M;
ALTER DATABASE ADD STANDBY LOGFILE GROUP 11 '/u01/app/oracle/oradata/HMDG/redo11.log' SIZE 500M;
ALTER DATABASE ADD STANDBY LOGFILE GROUP 12 '/u01/app/oracle/oradata/HMDG/redo12.log' SIZE 500M;
ALTER DATABASE ADD STANDBY LOGFILE GROUP 13 '/u01/app/oracle/oradata/HMDG/redo13.log' SIZE 500M;
ALTER DATABASE ADD STANDBY LOGFILE GROUP 14 '/u01/app/oracle/oradata/HMDG/redo14.log' SIZE 500M;
查看备用重做日志
SQL> SELECT GROUP#,THREAD#,SEQUENCE#,ARCHIVED,STATUS FROM V$STANDBY_LOG;
GROUP# THREAD# SEQUENCE# ARC STATUS
------ ---------- ---------- --- ----------
5 0 0 YES UNASSIGNED
6 0 0 YES UNASSIGNED
7 0 0 YES UNASSIGNED
8 0 0 YES UNASSIGNED
9 0 0 YES UNASSIGNED
10 0 0 YES UNASSIGNED
11 0 0 YES UNASSIGNED
12 0 0 YES UNASSIGNED
13 0 0 YES UNASSIGNED
14 0 0 YES UNASSIGNED
10 rows selected.
4. 开启闪回日志
SQL> alter database flashback on;
SQL> select flashback_on from v$database;
FLASHBACK_ON
------------------
YES
二、监听与TNS配置
1. 监听配置
主备数据库必须注册静态监听服务(listener.ora)
$ cat /u01/app/oracle/product/11.2.0/db_1/network/admin/listener.ora
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = hmdb11dg-db1)(PORT = 1521))
)
)
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(GLOBAL_DBNAME = HMDG.DB)
(ORACLE_HOME = /u01/app/oracle/product/11.2.0/db_1)
(SID_NAME = HMDG)
)
)
ADR_BASE_LISTENER = /u01/app/oracle
#备数据库
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = hmdb11dg-db2)(PORT = 1521))
)
)
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(GLOBAL_DBNAME = HMDG2.DB)
(ORACLE_HOME = /u01/app/oracle/product/11.2.0/db_1)
(SID_NAME = HMDG2)
)
)
ADR_BASE_LISTENER = /u01/app/oracle
2. 主备数据库TNS别名连接信息配置
修改两台服务器上的$ORACLE_HOME/network/admin/tnsnames.ora配置文件,主备使用相同的配置
HMDG =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = hmdb11dg-db1)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = HMDG.DB)
)
)
HMDG2 =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = hmdb11dg-db2)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = HMDG2.DB)
)
)
3. 测试使用TNS别名连接数据库
[oracle@hmdb11dg-db1 ~]$ sqlplus system/oracle@HMDG
SQL*Plus: Release 11.2.0.1.0 Production on Thu Mar 22 20:47:28 2018
Copyright (c) 1982, 2009, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL>
三、主数据库初始化参数配置
1. 创建PFILE参数文件
使用如下语句创建PFILE配置文件,该文件自动生成在/u01/app/oracle/product/11.2.0/db_1/dbs目录下
SQL> CREATE PFILE FROM SPFILE;
2. 修改PFILE配置
使用刚刚生成的PFILE文件,修改如下配置
HMDG.__db_cache_size=1023410176
HMDG.__java_pool_size=16777216
HMDG.__large_pool_size=16777216
HMDG.__oracle_base='/u01/app/oracle'#ORACLE_BASE set from environment
HMDG.__pga_aggregate_target=1325400064
HMDG.__sga_target=1962934272
HMDG.__shared_io_pool_size=0
HMDG.__shared_pool_size=872415232
HMDG.__streams_pool_size=0
*.audit_file_dest='/u01/app/oracle/admin/HMDG/adump'
*.audit_trail='db'
*.compatible='11.2.0.0.0'
*.control_files='/u01/app/oracle/oradata/HMDG/control01.ctl','/u01/app/oracle/flash_recovery_area/HMDG/control02.ctl'
*.db_block_size=8192
*.db_domain=''
*.db_name='HMDG'
*.db_unique_name='HMDG'
*.log_archive_config='DG_CONFIG=(HMDG,HMDG2)'
LOG_ARCHIVE_DEST_1=
'LOCATION=/u01/app/oracle/oradata/HMDG/
VALID_FOR=(ALL_LOGFILES,ALL_ROLES)
DB_UNIQUE_NAME=HMDG'
LOG_ARCHIVE_DEST_2=
'SERVICE=HMDG2 ASYNC
VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE)
DB_UNIQUE_NAME=HMDG2'
LOG_ARCHIVE_DEST_STATE_1=ENABLE
LOG_ARCHIVE_DEST_STATE_2=ENABLE
LOG_ARCHIVE_MAX_PROCESSES=30
FAL_SERVER=HMDG2
FAL_CLIENT=HMDG
DB_FILE_NAME_CONVERT='HMDG2','HMDG'
LOG_FILE_NAME_CONVERT='/u01/app/oracle/oradata/HMDG2/','/u01/app/oracle/oradata/HMDG/'
STANDBY_FILE_MANAGEMENT=AUTO
*.db_recovery_file_dest='/u01/app/oracle/flash_recovery_area'
*.db_recovery_file_dest_size=21474836480
*.diagnostic_dest='/u01/app/oracle'
*.dispatchers='(PROTOCOL=TCP) (SERVICE=HMDGXDB)'
*.log_archive_format='%t_%s_%r.dbf'
*.memory_target=3277848576
*.open_cursors=300
*.processes=5000
*.remote_login_passwordfile='EXCLUSIVE'
*.sessions=5505
*.undo_tablespace='UNDOTBS1'
3. 使用刚刚创建的PFILE文件重新启动数据库,并且重新创建一个SPFILE文件
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup pfile='/u01/app/oracle/product/11.2.0/db_1/dbs/initHMDG.ora';
ORACLE instance started.
Total System Global Area 3273641984 bytes
Fixed Size 2217792 bytes
Variable Size 2315258048 bytes
Database Buffers 939524096 bytes
Redo Buffers 16642048 bytes
Database mounted.
Database opened.
SQL> create spfile from pfile='/u01/app/oracle/product/11.2.0/db_1/dbs/initHMDG.ora'; #创建SPFILE文件
File created.
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup
ORACLE instance started.
Total System Global Area 3273641984 bytes
Fixed Size 2217792 bytes
Variable Size 2315258048 bytes
Database Buffers 939524096 bytes
Redo Buffers 16642048 bytes
Database mounted.
Database opened.
注意:一旦由于PFILE参数配置信息错误或者使用PFILE文件启动报错需要修改PFILE文件参数的时候,都必须要重新创建SPFILE文件。(主备数据库都是一样)
4. 备份数据库(在这里是非必须的,因为在本次操作中我使用的是RMAN复制到备用数据库)
[oracle@hmdb11dg-db1 dbs]$ rman target = /
Recovery Manager: Release 11.2.0.1.0 - Production on Thu Mar 22 21:12:06 2018
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
connected to target database: HMDG (DBID=787192145)
RMAN> BACKUP DATABASE PLUS ARCHIVELOG;
5. 创建备用数据库控制文件
SQL> ALTER DATABASE CREATE STANDBY CONTROLFILE AS '/tmp/HMDG2.ctl';
6. 创建备用数据库的PFILE文件
SQL> CREATE PFILE='/tmp/initHMDG2.ora' FROM SPFILE;
7. 创建备用数据的密码文件
[oracle@hmdb11dg-db1 ~]$ cp /u01/app/oracle/product/11.2.0/db_1/dbs/orapwHMDG /tmp/orapwHMDG2
四、备用数据库服务器设置
1. 备用数据库上创建必要的数据库文件目录
$ mkdir /u01/app/oracle/admin
$ mkdir /u01/app/oracle/admin/HMDG2
$ mkdir /u01/app/oracle/admin/HMDG2/{adump,dpdump,pfile,scripts}
$ mkdir -p /u01/app/oracle/oradata/HMDG2
$ mkdir -p /u01/app/oracle/flash_recovery_area/HMDG2
2. 从主数据库上拷贝配置文件到备用数据库(备用服务器上操作)
将控制文件、参数文件和密码文件从主服务器上复制到备用服务器上
$ scp oracle@hmdb11dg-db1:/tmp/HMDG2.ctl /u01/app/oracle/oradata/HMDG2/control01.ctl
$ cp /u01/app/oracle/oradata/HMDG2/control01.ctl /u01/app/oracle/flash_recovery_area/HMDG2/control02.ctl
$ scp oracle@hmdb11dg-db1:/tmp/orapwHMDG2 /u01/app/oracle/product/11.2.0/db_1/dbs/orapwHMDG2
$ scp oracle@hmdb11dg-db1:/tmp/initHMDG2.ora /u01/app/oracle/product/11.2.0/db_1/dbs/initHMDG2.ora
3. 修改备用数据库初始化参数
修改备用服务器的PFILE文件$ORACLE_HOME/dbs/initHMDG2.ora
HMDG.__db_cache_size=939524096
HMDG.__java_pool_size=16777216
HMDG.__large_pool_size=16777216
HMDG.__oracle_base='/u01/app/oracle'#ORACLE_BASE set from environment
HMDG.__pga_aggregate_target=1325400064
HMDG.__sga_target=1962934272
HMDG.__shared_io_pool_size=0
HMDG.__shared_pool_size=956301312
HMDG.__streams_pool_size=0
*.audit_file_dest='/u01/app/oracle/admin/HMDG2/adump'
*.audit_trail='db'
*.compatible='11.2.0.0.0'
*.control_files='/u01/app/oracle/oradata/HMDG2/control01.ctl','/u01/app/oracle/flash_recovery_area/HMDG2/control02.ctl'
*.db_block_size=8192
*.db_domain=''
*.DB_FILE_NAME_CONVERT='HMDG','HMDG2'
*.db_name='HMDG'
*.db_recovery_file_dest='/u01/app/oracle/flash_recovery_area'
*.db_recovery_file_dest_size=21474836480
*.db_unique_name='HMDG2'
*.diagnostic_dest='/u01/app/oracle'
*.dispatchers='(PROTOCOL=TCP) (SERVICE=HMDGXDB)'
*.FAL_CLIENT='HMDG2'
*.FAL_SERVER='HMDG'
*.log_archive_config='DG_CONFIG=(HMDG,HMDG2)'
*.LOG_ARCHIVE_DEST_1='LOCATION=/u01/app/oracle/oradata/HMDG2/
VALID_FOR=(ALL_LOGFILES,ALL_ROLES)
DB_UNIQUE_NAME=HMDG2'
*.LOG_ARCHIVE_DEST_2='SERVICE=HMDG ASYNC
VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE)
DB_UNIQUE_NAME=HMDG'
*.LOG_ARCHIVE_DEST_STATE_1='ENABLE'
*.LOG_ARCHIVE_DEST_STATE_2='ENABLE'
*.log_archive_format='%t_%s_%r.dbf'
*.LOG_ARCHIVE_MAX_PROCESSES=30
*.LOG_FILE_NAME_CONVERT='/u01/app/oracle/oradata/HMDG/','/u01/app/oracle/oradata/HMDG2/'
*.memory_target=3277848576
*.open_cursors=300
*.processes=5000
*.remote_login_passwordfile='EXCLUSIVE'
*.sessions=5505
*.STANDBY_FILE_MANAGEMENT='AUTO'
*.undo_tablespace='UNDOTBS1'
4. 使用刚刚创建的PFILE参数文件启动备用数据库
[oracle@hmdb11dg-db2 ~]$ sqlplus / as sysdba
SQL*Plus: Release 11.2.0.1.0 Production on Thu Mar 22 21:42:24 2018
Copyright (c) 1982, 2009, Oracle. All rights reserved.
Connected to an idle instance.
SQL> STARTUP NOMOUNT PFILE='/u01/app/oracle/product/11.2.0/db_1/dbs/initHMDG2.ora';
ORACLE instance started.
Total System Global Area 3273641984 bytes
Fixed Size 2217792 bytes
Variable Size 2197817536 bytes
Database Buffers 1056964608 bytes
Redo Buffers 16642048 bytes
5. 创建SPFILE文件
SQL> CREATE SPFILE FROM PFILE;
File created.
注意:一旦由于PFILE参数配置信息错误或者使用PFILE文件启动报错需要修改PFILE文件参数的时候,都必须要重新使用该语句创建SPFILE文件。(主备数据库都是一样)
五、在备用数据库上使用rman恢复数据到备用数据库(DUPLICATE)
1. 建立连接
[oracle@hmdb11dg-db2 ~]$ rman TARGET sys/oracle@HMDG AUXILIARY sys/oracle@HMDG2
Recovery Manager: Release 11.2.0.1.0 - Production on Thu Mar 22 21:46:42 2018
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
connected to target database: HMDG (DBID=787192145)
connected to auxiliary database: HMDG (not mounted)
2. 将主数据库复制到备用数据库
在RMAN模式下,发出以下语句将主数据库复制到备数据库
DUPLICATE TARGET DATABASE FOR STANDBY FROM ACTIVE DATABASE;
[oracle@hmdb11dg-db2 dbs]$ rman TARGET sys/oracle@HMDG AUXILIARY sys/oracle@HMDG2
Recovery Manager: Release 11.2.0.1.0 - Production on Fri Mar 23 09:50:19 2018
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
connected to target database: HMDG (DBID=787214401)
connected to auxiliary database: HMDG (not mounted)
RMAN> DUPLICATE TARGET DATABASE FOR STANDBY FROM ACTIVE DATABASE;
Starting Duplicate Db at 23-MAR-18
using target database control file instead of recovery catalog
allocated channel: ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: SID=4708 device type=DISK
contents of Memory Script:
{
backup as copy reuse
targetfile '/u01/app/oracle/product/11.2.0/db_1/dbs/orapwHMDG' auxiliary format
'/u01/app/oracle/product/11.2.0/db_1/dbs/orapwHMDG2' ;
}
executing Memory Script
Starting backup at 23-MAR-18
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=3773 device type=DISK
Finished backup at 23-MAR-18
contents of Memory Script:
{
backup as copy current controlfile for standby auxiliary format '/u01/app/oracle/oradata/HMDG2/control01.ctl';
restore clone controlfile to '/u01/app/oracle/flash_recovery_area/HMDG2/control02.ctl' from
'/u01/app/oracle/oradata/HMDG2/control01.ctl';
}
executing Memory Script
Starting backup at 23-MAR-18
using channel ORA_DISK_1
channel ORA_DISK_1: starting datafile copy
copying standby control file
output file name=/u01/app/oracle/product/11.2.0/db_1/dbs/snapcf_HMDG.f tag=TAG20180323T095223 RECID=3 STAMP=971517146
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:15
Finished backup at 23-MAR-18
Starting restore at 23-MAR-18
using channel ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: copied control file copy
Finished restore at 23-MAR-18
contents of Memory Script:
{
sql clone 'alter database mount standby database';
}
executing Memory Script
sql statement: alter database mount standby database
contents of Memory Script:
{
set newname for tempfile 1 to
"/u01/app/oracle/oradata/HMDG2/temp01.dbf";
switch clone tempfile all;
set newname for datafile 1 to
"/u01/app/oracle/oradata/HMDG2/system01.dbf";
set newname for datafile 2 to
"/u01/app/oracle/oradata/HMDG2/sysaux01.dbf";
set newname for datafile 3 to
"/u01/app/oracle/oradata/HMDG2/undotbs01.dbf";
set newname for datafile 4 to
"/u01/app/oracle/oradata/HMDG2/users01.dbf";
backup as copy reuse
datafile 1 auxiliary format
"/u01/app/oracle/oradata/HMDG2/system01.dbf" datafile
2 auxiliary format
"/u01/app/oracle/oradata/HMDG2/sysaux01.dbf" datafile
3 auxiliary format
"/u01/app/oracle/oradata/HMDG2/undotbs01.dbf" datafile
4 auxiliary format
"/u01/app/oracle/oradata/HMDG2/users01.dbf" ;
sql 'alter system archive log current';
}
executing Memory Script
executing command: SET NEWNAME
renamed tempfile 1 to /u01/app/oracle/oradata/HMDG2/temp01.dbf in control file
executing command: SET NEWNAME
executing command: SET NEWNAME
executing command: SET NEWNAME
executing command: SET NEWNAME
Starting backup at 23-MAR-18
using channel ORA_DISK_1
channel ORA_DISK_1: starting datafile copy
input datafile file number=00003 name=/u01/app/oracle/oradata/HMDG/undotbs01.dbf
output file name=/u01/app/oracle/oradata/HMDG2/undotbs01.dbf tag=TAG20180323T095246
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:01:15
channel ORA_DISK_1: starting datafile copy
input datafile file number=00001 name=/u01/app/oracle/oradata/HMDG/system01.dbf
output file name=/u01/app/oracle/oradata/HMDG2/system01.dbf tag=TAG20180323T095246
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:35
channel ORA_DISK_1: starting datafile copy
input datafile file number=00002 name=/u01/app/oracle/oradata/HMDG/sysaux01.dbf
output file name=/u01/app/oracle/oradata/HMDG2/sysaux01.dbf tag=TAG20180323T095246
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:25
channel ORA_DISK_1: starting datafile copy
input datafile file number=00004 name=/u01/app/oracle/oradata/HMDG/users01.dbf
output file name=/u01/app/oracle/oradata/HMDG2/users01.dbf tag=TAG20180323T095246
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:15
Finished backup at 23-MAR-18
sql statement: alter system archive log current
contents of Memory Script:
{
switch clone datafile all;
}
executing Memory Script
datafile 1 switched to datafile copy
input datafile copy RECID=3 STAMP=971517318 file name=/u01/app/oracle/oradata/HMDG2/system01.dbf
datafile 2 switched to datafile copy
input datafile copy RECID=4 STAMP=971517318 file name=/u01/app/oracle/oradata/HMDG2/sysaux01.dbf
datafile 3 switched to datafile copy
input datafile copy RECID=5 STAMP=971517318 file name=/u01/app/oracle/oradata/HMDG2/undotbs01.dbf
datafile 4 switched to datafile copy
input datafile copy RECID=6 STAMP=971517319 file name=/u01/app/oracle/oradata/HMDG2/users01.dbf
Finished Duplicate Db at 23-MAR-18
如果复制过程中没有报错,就可以立即开启日志重做应用
3. 开启日志重做应用
发出如下命令指示备库开始使用备用日志进行在备库上同步数据(恢复数据)
SQL> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE DISCONNECT FROM SESSION;
#或者
SQL> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE USING CURRENT LOGFILE DISCONNECT FROM SESSION;
#取消申请重做(该命令用于停止恢复)
SQL> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE CANCEL;
4. 测试备用数据的存档操作
默认情况下,当在线重做日志文件变满时,会发送日志切换。要强制进行日志切换以便立即传输重做数据,则在主数据库上使用如下语句强制日志切换
SQL> ALTER SYSTEM SWITCH LOGFILE;
System altered.
5. 在备用数据库上查询现有的归档重做日志文件
SQL> SELECT SEQUENCE#, FIRST_TIME, NEXT_TIME FROM V$ARCHIVED_LOG ORDER BY SEQUENCE#;
SEQUENCE# FIRST_TIM NEXT_TIME
---------- --------- ---------
19 23-MAR-18 23-MAR-18
20 23-MAR-18 23-MAR-18
21 23-MAR-18 23-MAR-18
22 23-MAR-18 23-MAR-18
6. 再次在数据库上强制日志切换
SQL> ALTER SYSTEM SWITCH LOGFILE;
System altered.
7. 验证在备用数据库中是否接收了新的重做日志
SQL> SELECT SEQUENCE#, FIRST_TIME, NEXT_TIME FROM V$ARCHIVED_LOG ORDER BY SEQUENCE#;
SEQUENCE# FIRST_TIM NEXT_TIME
---------- --------- ---------
19 23-MAR-18 23-MAR-18
20 23-MAR-18 23-MAR-18
21 23-MAR-18 23-MAR-18
22 23-MAR-18 23-MAR-18
23 23-MAR-18 23-MAR-18
8. 验证备用数据库是否应用了新的重做日志
SQL> SELECT SEQUENCE#,APPLIED FROM V$ARCHIVED_LOG ORDER BY SEQUENCE#;
SEQUENCE# APPLIED
---------- ---------
19 YES
20 YES
21 YES
22 YES
23 YES
9. 查询主备状态
#在当前主数据库中查询
SQL> SELECT SWITCHOVER_STATUS FROM V$DATABASE;
SWITCHOVER_STATUS
--------------------
TO STANDBY
#在当前备用数据库中查询
SQL> SELECT SWITCHOVER_STATUS FROM V$DATABASE;
SWITCHOVER_STATUS
--------------------
NOT ALLOWED
此时,说明当前主备数据库状态都为正常。其中主数据状态TO STANDBY说明可以随时切换为备用数据库。
五、手动切换测试(主备切换)
在主数据库(DB1)上操作
在当前主数据库中查询主备状态
SQL> SELECT SWITCHOVER_STATUS FROM V$DATABASE;
SWITCHOVER_STATUS
--------------------
TO STANDBY
当主数据库的状态为TO STANDBY时,表示可以切换到备用数据库
在当前主数据库(DB1)上发出切换到备数据库角色申请
SQL> ALTER DATABASE COMMIT TO SWITCHOVER TO PHYSICAL STANDBY WITH SESSION SHUTDOWN;
Database altered.
在备数据库(DB2)上操作
此时查看备数据库(DB2)的状态
SQL> SELECT SWITCHOVER_STATUS FROM V$DATABASE;
SWITCHOVER_STATUS
--------------------
TO PRIMARY
当此时备数据库的状态为TO PRIMARY时,表示可以切换到主数据库角色
在当前备数据库(DB2)上发出切换到主数据库角色申请
SQL> ALTER DATABASE COMMIT TO SWITCHOVER TO PRIMARY WITH SESSION SHUTDOWN;
Database altered.
继续在备数据(DB2)上完成以下指令
SQL> ALTER DATABASE OPEN;
-- 或者
SQL> SHUTDOWN IMMEDIATE;
SQL> STARTUP;
在原来的主数据(DB1)上继续执行以下命令
SQL> SHUTDOWN IMMEDIATE;
ORA-01507: database not mounted
ORACLE instance shut down.
SQL> STARTUP NOMOUNT;
ORACLE instance started.
Total System Global Area 3273641984 bytes
Fixed Size 2217792 bytes
Variable Size 2264926400 bytes
Database Buffers 989855744 bytes
Redo Buffers 16642048 bytes
SQL> ALTER DATABASE MOUNT STANDBY DATABASE;
Database altered.
发出如下命令让当前备库(DB1)开始使用备用日志进行同步数据(恢复数据)
SQL> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE USING CURRENT LOGFILE DISCONNECT FROM SESSION;
Database altered.
最后查看切换以后的主备状态与主备角色
-- DB2上
SQL> SELECT SWITCHOVER_STATUS FROM V$DATABASE;
SWITCHOVER_STATUS
--------------------
TO STANDBY
SQL> select database_role from v$database;
DATABASE_ROLE
----------------
PHYSICAL STANDBY
-- DB1上
SQL> SELECT SWITCHOVER_STATUS FROM V$DATABASE;
SWITCHOVER_STATUS
--------------------
NOT ALLOWED
SQL> select database_role from v$database;
DATABASE_ROLE
----------------
PRIMARY
以上状态说明主备角色已经正常切换
注意:在正常切换后,主数据库状态可能需要等待几分钟时间才显示为TO STANDBY状态,这是因为主数据在执行日志切换,需要等待切换完成
在当前主数据库(DB2)上强制日志切换
SQL> ALTER SYSTEM SWITCH LOGFILE;
System altered.
至此主备切换测试完成