目录
1. 背景
在两台服务器上面安装好了操作系统和数据库。该案例中我将使用OEL7和Oracle Database 19c
主服务器(ol7-19-dg1.test)上运行一个实例
备服务器(ol7-19-dg2.test)上只安装了软件
两台服务器在监听端口上可以正常通信。如果使用默认的1521端口,节点1应该能够在1521端口上和节点2通信,反之亦然。检查网络和本地防火墙没有阻塞通信
2. 主库设置
2.1. logging设置
检查主数据库是否处于归档模式
select log_mode from v$database;
LOG_MODE------------
NOARCHIVELOG
SQL>
如果不在归档模式,切换为归档模式
shutdown immediate;
startup mount;alter database archivelog;alter database open;
执行以下命令强制logging
alter database force logging;-- Make sure at least one logfile is present.alter system switch logfile;
为了便于switchover,在primary数据库上创建standby redo log。standby redo log至少和最大的redo log一样大,并且应该比redo log多一组。这里,以下的standby redo log必须在两个服务器上创建。
-- If Oracle Managed Files (OMF) is used.alter database add standby logfile thread 1 group 10 size 50m;alter database add standby logfile thread 1 group 11 size 50m;alter database add standby logfile thread 1 group 12 size 50m;alter database add standby logfile thread 1 group 13 size 50m;
-- If Oracle Managed Files (OMF) is not used.alter database add standby logfile thread 1 group 10 ('/u01/oradata/cdb1/standby_redo01.log') size 50m;alter database add standby logfile thread 1 group 11 ('/u01/oradata/cdb1/standby_redo02.log') size 50m;alter database add standby logfile thread 1 group 12 ('/u01/oradata/cdb1/standby_redo03.log') size 50m;alter database add standby logfile thread 1 group 13 ('/u01/oradata/cdb1/standby_redo04.log') size 50m;
如果想要闪回数据库,现在在primary上启用该功能,这样的话,在standby上也会被起用。如后续所示,这是非常有用的。
alter database flashback on;
2.2. 初始化参数设置
检查初始化参数db_name和db_unique_name。该案例中,primary数据库上均设置为cdb1
SQL> show parameter db_name
NAME TYPE VALUE------------------------------------ ----------- ------------------------------
db_name string cdb1
SQL> show parameter db_unique_name
NAME TYPE VALUE------------------------------------ ----------- ------------------------------
db_unique_name string cdb1
SQL>
standby数据库的db_name必须和primary的一样,但是db_unique_name需要不同。例如,这里standby数据库的db_unique_name就是cdb1_stby
确保standby_file_management参数设置正常
alter system set standby_file_management=auto;
2.3. service设置
primary和standby数据库的以下内容需要在两台主机的文件"$ORACLE_HOME/network/admin/tnsnames.ora"中保存。可以使用netca或手动创建。以下内容是在设置过程中使用的。注意这里使用的是SID,而不是service_name。这对于borker在数据库关闭时连接到数据库而言非常重要,所以服务这里就不再设置。
cdb1 =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = ol7-19-dg1)(PORT = 1521))
)
(CONNECT_DATA =
(SID = cdb1)
)
)
cdb1_stby =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = ol7-19-dg2)(PORT = 1521))
)
(CONNECT_DATA =
(SID = cdb1)
)
)
主服务器的文件"$ORACLE_HOME/network/admin/listener.ora"中添加以下设置
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = ol7-19-dg1)(PORT = 1521))
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
)
)
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(GLOBAL_DBNAME = cdb1_DGMGRL)
(ORACLE_HOME = /u01/app/oracle/product/19.0.0/db_1)
(SID_NAME = cdb1)
(ENVS="TNS_ADMIN=/u01/app/oracle/product/19.0.0/db_1/network/admin")
)
)
ADR_BASE_LISTENER = /u01/app/oracle
standby服务器的文件"$ORACLE_HOME/network/admin/listener.ora"中添加以下设置。因为borker需要数据库关闭时依然能够连接到数据库,我们不能依赖自动注册的监听,因此要显式指定数据库。
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = ol7-19-dg2)(PORT = 1521))
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
)
)
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(GLOBAL_DBNAME = cdb1_stby_DGMGRL)
(ORACLE_HOME = /u01/app/oracle/product/19.0.0/db_1)
(SID_NAME = cdb1)
(ENVS="TNS_ADMIN=/u01/app/oracle/product/19.0.0/db_1/network/admin")
)
)
ADR_BASE_LISTENER = /u01/app/oracle
一旦listener.ora文件修改完成,重启两个服务器上的监听。
lsnrctl stop
lsnrctl start
3. 备库设置
3.1. 准备做duplicate
为standby数据库创建参数文件/tmp/initcdb1_stby.ora,其中内容如下
*.db_name='cdb1'
在standby服务器上创建必要的目录
mkdir -p /u01/app/oracle/oradata/cdb1/pdbseed
mkdir -p /u01/app/oracle/oradata/cdb1/pdb1
mkdir -p /u01/app/oracle/fast_recovery_area/cdb1
mkdir -p /u01/app/oracle/admin/cdb1/adump
创建密码文件,其中SYS密码要和primary数据库的一样
$ orapwd file=/u01/app/oracle/product/19.0.0/db_1/dbs/orapwcdb1 password=Password1 entries=10
3.2. 使用duplicate创建standby
在standby服务器上使用临时参数文件启动辅助实例。
$ export ORACLE_SID=cdb1
$ sqlplus / as sysdba
SQL> STARTUP NOMOUNT PFILE='/tmp/initcdb1_stby.ora';
连接到rman,并同时连接到target和auxiliary。不要使用OS认证
$ rman TARGET sys/Password1@cdb1 AUXILIARY sys/Password1@cdb1_stby
执行以下duplicate命令
duplicate target database
for standby
from active database
dorecover
spfile
set db_unique_name='cdb1_stby' COMMENT 'Is standby'
nofilenamecheck;
如果想要设置文件路径转换或者修改任意初始化参数,可以在duplicate中使用set命令。
duplicate target database
for standby
from active database
dorecover
spfile
set db_unique_name='cdb1_stby' COMMENT 'Is standby'
set db_file_name_convert='/original/directory/path1/','/new/directory/path1/','/original/directory/path2/','/new/directory/path2/'
set log_file_name_convert='/original/directory/path1/','/new/directory/path1/','/original/directory/path2/','/new/directory/path2/'
set job_queue_processes='0'
nofilenamecheck;
不同子句的简要解释如下:
for standby: 这是告诉duplicate命令,它是用来做standby的,所以不需要强制DBID改变
from active database: duplicate会直接使用源数据库的数据文件,而不用使用备份步骤
dorecover: duplicate将包含恢复步骤,将standby恢复到当前时间点
spfile: 在拷贝源库spfile的时候,能够重新设置其中的设置
nofilenamecheck: 目标文件位置不检查
一旦以上命令完成,就能开始使用broker
4. 启用borker
此时,我们有一个primary数据库和一个standby数据库,所以现在需要开始使用dataguard broker来管理它们。同时连接到primary和standby两个数据库,执行以下命令。
alter system set dg_broker_start=true;
在primary服务器上,执行以下命令注册primary服务器到broker中
$ dgmgrl sys/Password1@cdb1
DGMGRL for Linux: Release 19.0.0.0.0 - Production on Tue Feb 26 22:39:33 2018
Version 19.2.0.0.0
Copyright (c) 1982, 2019, Oracle and/or its affiliates. All rights reserved.
Welcome to DGMGRL, type "help" for information.
Connected as SYSDBA.
DGMGRL> create configuration my_dg_config as primary database is cdb1 connect identifier is cdb1;
Configuration "my_dg_config" created with primary database "cdb1"
DGMGRL>
添加standby数据库
DGMGRL> add database cdb1_stby as connect identifier is cdb1_stby maintained as physical;
Database "cdb1_stby" added
DGMGRL>
启用新的配置
DGMGRL> enable configuration;
Enabled.
DGMGRL>
以下命令显示如何使用broker检查配置和数据库状态
DGMGRL> show configuration;
Configuration - my_dg_config
Protection Mode: MaxPerformance
Members:
cdb1 - Primary database
cdb1_stby - Physical standby database
Fast-Start Failover: DISABLED
Configuration Status:
SUCCESS (status updated 26 seconds ago)
DGMGRL> show database cdb1;
Database - cdb1
Role: PRIMARY
Intended State: TRANSPORT-ON
Instance(s):
cdb1
Database Status:
SUCCESS
DGMGRL> show database cdb1_stby;
Database - cdb1_stby
Role: PHYSICAL STANDBY
Intended State: APPLY-ON
Transport Lag: 0 seconds (computed 1 second ago)
Apply Lag: 0 seconds (computed 1 second ago)
Average Apply Rate: 5.00 KByte/s
Real Time Query: OFF
Instance(s):
cdb1
Database Status:
SUCCESS
DGMGRL>
5. 停止/启动恢复进程
mrp进程可以使用以下命令在standby数据库的SQL*Plus中关闭和停止
-- Stop managed recovery.alter database recover managed standby database cancel;
-- Start managed recovery.alter database recover managed standby database disconnect;
6. 数据库切换
一个数据库可以是两个互斥独占的模式(primary或standby)之一。这些角色可以在运行时进行修改,修改过程没有数据丢失或重置redo log。这个过程称为switchover,并且可以使用以下命令完成。连接到primary数据库(cdb1),然后switchover到standby数据库(cdb1_stby)
$ dgmgrl sys/Password1@cdb1
DGMGRL for Linux: Release 19.0.0.0.0 - Production on Tue Feb 26 22:39:33 2018
Version 19.2.0.0.0
Copyright (c) 1982, 2019, Oracle and/or its affiliates. All rights reserved.
Welcome to DGMGRL, type "help" for information.
Connected as SYSDBA.
DGMGRL> switchover to cdb1_stby;
Performing switchover NOW, please wait...
Operation requires a connection to instance "cdb1" on database "cdb1_stby"
Connecting to instance "cdb1"...
Connected as SYSDBA.
New primary database "cdb1_stby" is opening...
Operation requires start up of instance "cdb1" on database "cdb1"
Starting instance "cdb1"...
ORACLE instance started.
Database mounted.
Switchover succeeded, new primary is "cdb1_stby"
DGMGRL>
现在检查下原primary。连接到新primary数据库(cdb1_stby)并切换到新standby数据库(cdb1)
$ dgmgrl sys/Password1@cdb1_stby
DGMGRL for Linux: Release 19.0.0.0.0 - Production on Tue Feb 26 22:53:36 2018
Version 19.2.0.0.0
Copyright (c) 1982, 2019, Oracle and/or its affiliates. All rights reserved.
Welcome to DGMGRL, type "help" for information.
Connected as SYSDBA.
DGMGRL> switchover to cdb1;
Performing switchover NOW, please wait...
Operation requires a connection to instance "cdb1" on database "cdb1"
Connecting to instance "cdb1"...
Connected as SYSDBA.
New primary database "cdb1" is opening...
Operation requires start up of instance "cdb1" on database "cdb1_stby"
Starting instance "cdb1"...
ORACLE instance started.
Database mounted.
Switchover succeeded, new primary is "cdb1"
DGMGRL>
7. 数据库故障转移
如果primary数据库不可用,standby数据库可以使用以下命令强制激活为primary数据库。连接到standby数据库(cdb1_stby)并进行failover
$ dgmgrl sys/Password1@cdb1_stby
DGMGRL for Linux: Release 19.0.0.0.0 - Production on Tue Feb 26 22:53:36 2018
Version 19.2.0.0.0
Copyright (c) 1982, 2019, Oracle and/or its affiliates. All rights reserved.
Welcome to DGMGRL, type "help" for information.
Connected as SYSDBA.
DGMGRL> failover to cdb1_stby;
Performing failover NOW, please wait...
Failover succeeded, new primary is "cdb1_stby"
DGMGRL>
由于standby数据库已经变成了primary数据库,应该立即对其进行备份。
原primary数据库可以配置为standby数据库。如果primary数据库上配置数据库闪回,可以使用以下命令轻松完成。
DGMGRL> reinstate database cdb1;
Reinstating database "cdb1", please wait...
Operation requires shut down of instance "cdb1" on database "cdb1"
Shutting down instance "cdb1"...
ORACLE instance shut down.
Operation requires start up of instance "cdb1" on database "cdb1"
Starting instance "cdb1"...
ORACLE instance started.
Database mounted.
Continuing to reinstate database "cdb1" ...
Reinstatement of database "cdb1" succeeded
DGMGRL>
如果闪回数据库没有启用,需要像standby一样手动重新创建cdb1.基本过程就是之前的反向操作。
# 1) Cleanup the old instance.
sqlplus / as sysdba <<EOF
shutdown immediate;exit;
EOF
rm -Rf /u01/app/oracle/oradata/cdb1/*
rm -Rf /u01/app/oracle/fast_recovery_area/cdb1
rm -Rf /u01/app/oracle/fast_recovery_area/cdb1_stby
rm -Rf /u01/app/oracle/admin/cdb1
mkdir -p /u01/app/oracle/fast_recovery_area/cdb1
mkdir -p /u01/app/oracle/admin/cdb1/adump
mkdir -p /u01/app/oracle/oradata/cdb1/pdbseed
mkdir -p /u01/app/oracle/oradata/cdb1/pdb1
rm $ORACLE_HOME/dbs/spfilecdb1.ora
export ORACLE_SID=cdb1
sqlplus / as sysdba <<EOF
startup nomount pfile='/tmp/initcdb1_stby.ora';exit;
EOF
# 2) Connect to RMAN.
$ rman target sys/Password1@cdb1_stby auxiliary sys/Password1@cdb1
# 3) Duplicate the database.
duplicate target database
for standby
from active database
dorecover
spfile
set db_unique_name='cdb1' COMMENT 'Is standby'
nofilenamecheck;
# 4) Connect to DGMDRL on the current primary.
$ dgmgrl sys/Password1@cdb1_stby
# 5) Enable the new standby.
DGMGRL> enable database cdb1;
8. 闪回数据库
这在之前的章节中已经提到了,不过依然值得在这里再次介绍该功能。尽管switchover/switchback对primary和standby数据库都很安全,failover能够将源primary数据库无法变成standby数据库。如果闪回没有使用,源primary数据库必须删除,然后以standby的方式重建。
可选的操作是在priamry数据库上启用闪回数据库(如果需要,可以在standby上启用该功能),即使在failover的情况下,primary依然能够闪回到failover之前的时间点,快速转变成standby数据库。
9. 备数据库只读和ADG
一旦standby数据库配置完成,它是可以以只读模式打开,允许查询访问的。这通常用于将报表负载卸载到standby服务器,进而能够释放primary服务器上的资源。当以只读模式打开,归档日志基础发送,但是mrp进程就会被停止,这样standby数据库在重新启动mrp之前会一直增加延迟。
将standby数据库切换为只读模式,命令下入
shutdown immediate;
startup mount;alter database open read only;
重新启动mrp进程,操作如下
shutdown immediate;
startup mount;alter database recover managed standby database disconnect from session;
在11g中,Oracle引入了ADG功能。这允许standby数据库以只读模式打开,但是依然应用redo信息。这就意味着standby可以用于查询,同时还能更新最新的数据。该功能需要license,但是以下命令能够启用adg
shutdown immediate;
startup mount;alter database open read only;alter database recover managed standby database disconnect from session;
10. 快照备数据库
11g中引入的该功能,snapshot standby能够让standby数据库以读写模式打开。当切换回standby模式时,在读写模式下所做的所有变更都会丢失。这是使用闪回数据库实现的,但是standby database不需要显式启用数据库闪回就能使用该功能,尽管二者的工作方式相同。
连接到primary(cdb1)数据库,将standby数据库(cdb1_stby)转换为snapshot数据库
$ dgmgrl sys/Password1@cdb1
DGMGRL for Linux: Release 19.0.0.0.0 - Production on Tue Feb 26 22:53:36 2018
Version 19.2.0.0.0
Copyright (c) 1982, 2019, Oracle and/or its affiliates. All rights reserved.
Welcome to DGMGRL, type "help" for information.
Connected as SYSDBA.
DGMGRL> convert database cdb1_stby to snapshot standby;
Converting database "cdb1_stby" to a Snapshot Standby database, please wait...
Database "cdb1_stby" converted successfully
DGMGRL>
当完成了snapshot standby的转换,可以将其重新转换为standby数据库
$ dgmgrl sys/Password1@cdb1
DGMGRL for Linux: Release 19.0.0.0.0 - Production on Tue Feb 26 22:53:36 2018
Version 19.2.0.0.0
Copyright (c) 1982, 2019, Oracle and/or its affiliates. All rights reserved.
Welcome to DGMGRL, type "help" for information.
Connected as SYSDBA.
DGMGRL> convert database cdb1_stby to physical standby;
Converting database "cdb1_stby" to a Physical Standby database, please wait...
Operation requires shut down of instance "cdb1" on database "cdb1_stby"
Shutting down instance "cdb1"...
Database closed.
Database dismounted.
ORACLE instance shut down.
Operation requires start up of instance "cdb1" on database "cdb1_stby"
Starting instance "cdb1"...
ORACLE instance started.
Database mounted.
Continuing to convert database "cdb1_stby" ...
Database "cdb1_stby" converted successfully
DGMGRL>
standby就会再次启动介质恢复和归档传输。注意闪回数据库依然没有启用。
DGMGRL> show configuration;
Configuration - my_dg_config
Protection Mode: MaxPerformance
Members:
cdb1 - Primary database
cdb1_stby - Physical standby database
Fast-Start Failover: DISABLED
Configuration Status:
SUCCESS (status updated 38 seconds ago)
DGMGRL>