分类: Oracle
2017-05-06 17:23:50
Oracle 11g Realese 2 Data Guard使用 Data Guard Broker
引用
Oracle Data Guard 是Oracle的备用数据库解决方案的名称,用于灾难恢复和高可用性,本文是在这里描述的手动11GR2数据保护设置的方案,但是这里的数据保护设置和管理是使用Data Guard Broker执行的
1.假设
2.主库服务器设置
2.1登陆设置
2.2初始化参数设置
2.3监听与本地服务命名设置
3.备库服务器设置
3.1 准备复制
3.2 使用duplicate创建复制
4.开启Broker
5.数据库切换
6.数据库故障转移
7.闪回数据库
8.Read-Only Standby and Active Data Guard
9.备库快照
1.假设
a) 在两个服务器上(物理机/VM主机),其中安装了操作系统并且安装了Oracle数据库。在本例中使用Oracle Linux 7.2和Oracle数据库11.2.0.4。
b) 主库服务器(ol7-112-dg1.herbert.com)正常运行一个数据库实例。
c) 备库服务器(ol7-112-dg2.herbert.com)安装数据库软件
2.主库服务器设置
2.1登陆
? 检查主库是否运行在归档模式中
SELECT log_mode FROM v$database;
LOG_MODE ------------ NOARCHIVELOG
SQL> |
? 如果不是运行在归档模式,请使用如下方式切换至归档模式
SHUTDOWN IMMEDIATE; STARTUP MOUNT; ALTER DATABASE ARCHIVELOG; ALTER DATABASE OPEN;
|
? 通过以下命令开启Force logging(非必需)
ALTER DATABASE FORCE LOGGING; -- Make sure at leat one logfile is present. ALTER SYSTEM SWITCH LOGFILE;
|
在主数据库上创建备用重做日志(在做切换时)。备用的重做日志应该至少和最大的联机重做日志一样的大小,并且每个线程应该有一个额外的组,比较联机重做日志。
? 在本例中,两台服务器上创建以下备用重做日志
ALTER DATABASE ADD STANDBY LOGFILE ('/u01/app/oracle/oradata/db11g/standby_redo01.log') SIZE 50M; ALTER DATABASE ADD STANDBY LOGFILE ('/u01/app/oracle/oradata/db11g/standby_redo02.log') SIZE 50M; ALTER DATABASE ADD STANDBY LOGFILE ('/u01/app/oracle/oradata/db11g/standby_redo03.log') SIZE 50M; ALTER DATABASE ADD STANDBY LOGFILE ('/u01/app/oracle/oradata/db11g/standby_redo04.log') SIZE 50M; |
如果想要使用flashback数据库,可以在当前的主服务器上进行启用,因此也可以在备用服务器进行启用
? 启用闪回数据库
ALTER DATABASE FLASHBACK ON; |
2.2初始化参数设置
检查DB_NAME 和 DB_UNIQUE_NAME参数设置。
在本例中它们都设置在db11g这个实例上
SQL> show parameter db_name NAME TYPE VALUE ------------------------------------ ----------- ------------------------------ db_name string db11g
SQL> show parameter db_unique_name NAME TYPE VALUE ------------------------------------ ----------- ------------------------------ db_unique_name string db11g SQL> |
备库的DB_NAME必须 与主数据库的DB_NAME一致,且必须具有不同的DB_UNIQUE_NAME值。在 本例中,备用数据库将具有“db11g_stby”的值。
设置适当的远程存档日志存放地址,在本例中,将使用fast recovery area做为本地位置
可以根据需要显示的指定一个存放位置
? 确保STANDBY_FILE_MANAGEMENT参数的设置
ALTER SYSTEM SET STANDBY_FILE_MANAGEMENT=AUTO; |
2.3监听与本地服务命名设置
? 主库/备库TNSNAMES.ORA文件配置
db11g = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = ol7-112-dg1)(PORT = 1521)) ) (CONNECT_DATA = (SID = db11g) ) )
db11g_stby = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = ol7-112-dg2)(PORT = 1521)) ) (CONNECT_DATA = (SID = db11g) ) ) |
在$ORACLE_HOME /network/admin/tnsnames.ora文件中同时设置主库和备库的连接信息
可以使用Network Configuration工具或者在运行NETCA工具手动进行创建
注意:在监听配置中使用ORACLE SID 而非SERVICE NAME,因为Broker程序在数据库关闭连接到数据库时,将会提示服务不存在的报错
? 主库LISTENER.ORA文件配置
LISTENER = (DESCRIPTION_LIST = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = ol7-112-dg1)(PORT = 1521)) (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521)) ) )
SID_LIST_LISTENER = (SID_LIST = (SID_DESC = (GLOBAL_DBNAME = db11g_DGMGRL) (ORACLE_HOME = /u01/app/oracle/product/11.2.0.4/db_1) (SID_NAME = db11g) ) )
ADR_BASE_LISTENER = /u01/app/oracle |
由于Broker程序需要在关闭时连接到数据库,所以不能采用Listener自动注册监听的方式,因为在本例中显示的进行静态注册
? 备库的Listener.ora文件配置
LISTENER = (DESCRIPTION_LIST = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = ol7-112-dg2)(PORT = 1521)) (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521)) ) )
SID_LIST_LISTENER = (SID_LIST = (SID_DESC = (GLOBAL_DBNAME = db11g_stby_DGMGRL) (ORACLE_HOME = /u01/app/oracle/product/11.2.0.4/db_1) (SID_NAME = db11g) ) )
ADR_BASE_LISTENER = /u01/app/oracle |
如果主库/备库的Listener.ora和Tnsnames.ora文件配置完毕,在两台服务器上重启监听服务
? 重启监听程序并测试连接
lsnrctl stop lsnrctl start
tnsping ol7-112-dg1 tnsping ol7-112-dg2 |
3.备库服务器设置
3.1 准备复制
? 为备库创建一个initdb11g_stdy的参数文件
*.db_name='db11g' |
? 在备库上创建必要文件目录
$ mkdir -p /u01/app/oracle/oradata/db11g $ mkdir -p /u01/app/oracle/fast_recovery_area/db11g $ mkdir -p /u01/app/oracle/admin/db11g/adump |
? 备库上创建密码文件,与主库的用户密码保持一致
$orapwd file=/u01/app/oracle/product/11.2.0.4/db_1/dbs/orapwdb11g password=Password1 entries=10 |
建议采用scp复制主库密码文件的方式传输至备库相同的目录下
3.2 使用duplicate创建复制
? 启用备库的辅助实例,使用刚刚创建的临时参数文件
$ export ORACLE_SID=db11g $ sqlplus / as sysdba
SQL> STARTUP NOMOUNT PFILE='/tmp/initdb11g_stby.ora'; |
? 连接到RMAN工具
$ rman TARGET sys/Password123@db11g AUXILIARY sys/Password123@db11g_stby |
为目标库和辅助实例指定一个完整的连接字符串。不要尝试使用OS身份进行验证。
? 使用DUPLICATE命令
DUPLICATE TARGET DATABASE FOR STANDBY FROM ACTIVE DATABASE DORECOVER SPFILE SET db_unique_name='DB11G_STBY' COMMENT 'Is standby' NOFILENAMECHECK; |
如果需要对文件位置进行转换,或者更改任何初始化参数,可以在使用SET命令的复制过程中进行操作。
DUPLICATE TARGET DATABASE FOR STANDBY FROM ACTIVE DATABASE DORECOVER SPFILE SET db_unique_name='DB11G_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命令
? FROM ACTIVE DATABASE:复制将直接从源数据文件中创建,没有额外的备份步骤
? DORECOVER:复制包含恢复步骤,将备库的数据检查时间点与主库的时间点一致
? SPFILE: 当从源服务器复制时,允许重置spfile中的参数值
? NOFILENAMECHECK: 目标文件位置不会被检查
命令执行完成后,我们就可以开始配置Broker
4.开启Broker
此时,我们已经有一个主数据库和一个备库,所以现在我们需要开始使用Data Guard Broker 来进行管理
? 连接两个数据库(主库和备库)并执行以下操作
ALTER SYSTEM SET dg_broker_start=true; |
? 在主服务器上,执行以下命令,向Broker服务中注册主库
$ dgmgrl sys/Password1@db11g DGMGRL for Linux: Version 11.2.0.4.0 - 64bit Production
Copyright (c) 2000, 2009, Oracle. All rights reserved.
Welcome to DGMGRL, type "help" for information. Connected. DGMGRL> CREATE CONFIGURATION my_dg_config AS PRIMARY DATABASE IS db11g CONNECT IDENTIFIER IS db11g; Configuration "my_dg_config" created with primary database "db11g" DGMGRL> |
? 添加备库信息
DGMGRL> ADD DATABASE db11g_stby AS CONNECT IDENTIFIER IS db11g_stby MAINTAINED AS PHYSICAL; Database "db11g_stby" added DGMGRL> |
? 启用新的配置
DGMGRL> ENABLE CONFIGURATION; Enabled. DGMGRL> |
? 使用下面的命令显示如何从Broker检查数据库的配置和状态
DGMGRL> SHOW CONFIGURATION;
Configuration - my_dg_config
Protection Mode: MaxPerformance Databases: db11g - Primary database db11g_stby - Physical standby database
Fast-Start Failover: DISABLED
Configuration Status: SUCCESS
DGMGRL> SHOW DATABASE db11g;
Database - db11g
Role: PRIMARY Intended State: TRANSPORT-ON Instance(s): db11g
Database Status: SUCCESS
DGMGRL> SHOW DATABASE db11g_stby;
Database - db11g_stby
Role: PHYSICAL STANDBY Intended State: APPLY-ON Transport Lag: 0 seconds (computed 0 seconds ago) Apply Lag: 0 seconds (computed 0 seconds ago) Apply Rate: 0 Byte/s Real Time Query: OFF Instance(s): db11g
Database Status: SUCCESS
DGMGRL> |
5.数据库切换
数据库可以是两种exclusive modes (主或备用)中的一种。这些角色可以在运行时更改,而不会丢失数据或重新设置redo log,这个过程称为切换。
连接到主数据库(db11g)并切换到备用数据库(db11g_stby)
? 执行以下命令
$ dgmgrl sys/Password1@db11g DGMGRL for Linux: Version 11.2.0.4.0 - 64bit Production
Copyright (c) 2000, 2009, Oracle. All rights reserved.
Welcome to DGMGRL, type "help" for information. Connected. DGMGRL> SWITCHOVER TO db11g_stby; Performing switchover NOW, please wait... Operation requires a connection to instance "db11g" on database "db11g_stby" Connecting to instance "db11g"... Connected. New primary database "db11g_stby" is opening... Operation requires startup of instance "db11g" on database "db11g" Starting instance "db11g"... ORACLE instance started. Database mounted. Switchover succeeded, new primary is "db11g_stby" DGMGRL> |
连接到新的主服务器(db11g_stby)并切换到新的备用数据库(db11g)。
? 连接回现有的主库
$ dgmgrl sys/Password1@db11g_stby DGMGRL for Linux: Version 11.2.0.4.0 - 64bit Production
Copyright (c) 2000, 2009, Oracle. All rights reserved.
Welcome to DGMGRL, type "help" for information. Connected. DGMGRL> SWITCHOVER TO db11g; Performing switchover NOW, please wait... Operation requires a connection to instance "db11g" on database "db11g" Connecting to instance "db11g"... Connected. New primary database "db11g" is opening... Operation requires startup of instance "db11g" on database "db11g_stby" Starting instance "db11g"... ORACLE instance started. Database mounted. Switchover succeeded, new primary is "db11g" DGMGRL> |
6.数据库故障转移
如果主数据库不可用,备用数据库可以使用以下语句激活为主数据库
? 连接备库(db11g_stby)和故障转移
$ dgmgrl sys/Password1@db11g_stby DGMGRL for Linux: Version 11.2.0.4.0 - 64bit Production
Copyright (c) 2000, 2009, Oracle. All rights reserved.
Welcome to DGMGRL, type "help" for information. Connected. DGMGRL> FAILOVER TO db11g_stby; Performing failover NOW, please wait... Failover succeeded, new primary is "db11g_stby" DGMGRL> |
如果主库故障转移完成,因为备库现在是主数据库,所以应该对当前主库立即备份
现在可以将原始主数据库配置为备用数据库。
如果在主数据库上启用了flashback数据库,那么可以相对轻松地使用以下命令进行操作。
DGMGRL> REINSTATE DATABASE db11g; Reinstating database "db11g", please wait... Operation requires shutdown of instance "db11g" on database "db11g" Shutting down instance "db11g"... Database closed. Database dismounted. ORACLE instance shut down. Operation requires startup of instance "db11g" on database "db11g" Starting instance "db11g"... ORACLE instance started. Database mounted. Continuing to reinstate database "db11g" ... Operation requires shutdown of instance "db11g" on database "db11g" Shutting down instance "db11g"... ORA-01109: database not open
Database dismounted. ORACLE instance shut down. Operation requires startup of instance "db11g" on database "db11g" Starting instance "db11g"... ORACLE instance started. Database mounted. Continuing to reinstate database "db11g" ... Reinstatement of database "db11g" succeeded DGMGRL> |
如果不启用flashback数据库,则必须手动重新创建db11g作为备库。
基本流程与之前所做的相反
# 1) Cleanup the old instance.
sqlplus
/ as sysdba < SHUTDOWN IMMEDIATE; EXIT; EOF
rm -Rf /u01/app/oracle/oradata/db11g/* rm -Rf /u01/app/oracle/fast_recovery_area/db11g rm -Rf /u01/app/oracle/admin/db11g mkdir -p /u01/app/oracle/fast_recovery_area/db11g mkdir -p /u01/app/oracle/admin/db11g/adump rm $ORACLE_HOME/dbs/spfiledb11g.ora
export ORACLE_SID=db11g
sqlplus
/ as sysdba < STARTUP NOMOUNT PFILE='/tmp/initdb11g_stby.ora'; EXIT; EOF
# 2) Connect to RMAN. $ rman TARGET sys/Password1@db11g_stby AUXILIARY sys/Password1@db11g
# 3) Duplicate the database. DUPLICATE TARGET DATABASE FOR STANDBY FROM ACTIVE DATABASE DORECOVER SPFILE SET db_unique_name='DB11G' COMMENT 'Is standby' NOFILENAMECHECK;
# 4) Connect to DGMDRL on the current primary. $ dgmgrl sys/Password1@db11g_stby
# 5) Enable the new standby. DGMGRL> ENABLE DATABASE db11g; |
7.闪回数据库
在之前已经提到过,但值得注意的是,再次使用Flashback数据库。尽管切换/故障转移对主数据库和备库都是安全的,但故障转移使原始的主数据库无法转换为备用数据库。如果不启用flashback数据库,则必须取消原始的主服务器,并将其重新创建为备用数据库。
另一种方法是在主服务器上启用flashback数据库(如果需要的话),所以在故障转移时,主可以在故障转移之前闪回,然后迅速转换到备用数据库,如上所示。
8.Read-Only Standby and Active Data Guard
一旦配置了备用数据库,就可以以只读模式打开,以允许查询访问。这通常用于向备用服务器发送连接请求,从而释放主服务器上的资源。当以只读模式打开时,归档日志传递将继续,但托管恢复被停止,因此备用数据库将越来越超时,直到恢复托管恢复。
? 执行以下操作将备库切换为只读模式
SHUTDOWN IMMEDIATE; STARTUP MOUNT; ALTER DATABASE OPEN READ ONLY; |
? 执行以下操作管理恢复
SHUTDOWN IMMEDIATE; STARTUP MOUNT; ALTER DATABASE RECOVER MANAGED STANDBY DATABASE DISCONNECT FROM SESSION; |
在11g,Oracle引入了主动数据保护功能。这允许备库以只读模式打开,但仍然应用重做信息。这意味着备库可以用于查询,这个特性有许可的含义。
? 下面的命令显示了如何启用活动数据保护
SHUTDOWN IMMEDIATE; STARTUP MOUNT; ALTER DATABASE OPEN READ ONLY; ALTER DATABASE RECOVER MANAGED STANDBY DATABASE DISCONNECT FROM SESSION; |
9.备库快照
在11g中引入快照备库,让备库在读写模式中打开。当切换回待机模式时,在读写模式下的所有更改都将丢失。这是使用flashback数据库实现的,但是备库不需要显式地启用flashback数据库来利用这个特性,认为它的工作原理是一样的。
? 连接到主数据库(db11g),并将备库(db11g_stby)转换为快照备份
$ dgmgrl sys/Password1@db11g DGMGRL for Linux: Version 11.2.0.4.0 - 64bit Production
Copyright (c) 2000, 2009, Oracle. All rights reserved.
Welcome to DGMGRL, type "help" for information. Connected. DGMGRL> CONVERT DATABASE db11g_stby TO SNAPSHOT STANDBY; Converting database "db11g_stby" to a Snapshot Standby database, please wait... Database "db11g_stby" converted successfully DGMGRL> |
? 当完成快照备份之后,将它转换回备库。
$ dgmgrl sys/Password1@db11g DGMGRL for Linux: Version 11.2.0.4.0 - 64bit Production
Copyright (c) 2000, 2009, Oracle. All rights reserved.
Welcome to DGMGRL, type "help" for information. Connected. DGMGRL> CONVERT DATABASE db11g_stby TO PHYSICAL STANDBY; Converting database "db11g_stby" to a Snapshot Standby database, please wait... Database "db11g_stby" converted successfully DGMGRL> CONVERT DATABASE db11g_stby TO PHYSICAL STANDBY; Converting database "db11g_stby" to a Physical Standby database, please wait... Operation requires shutdown of instance "db11g" on database "db11g_stby" Shutting down instance "db11g"... Database closed. Database dismounted. ORACLE instance shut down. Operation requires startup of instance "db11g" on database "db11g_stby" Starting instance "db11g"... ORACLE instance started. Database mounted. Continuing to convert database "db11g_stby" ... Operation requires shutdown of instance "db11g" on database "db11g_stby" Shutting down instance "db11g"... ORA-01109: database not open
Database dismounted. ORACLE instance shut down. Operation requires startup of instance "db11g" on database "db11g_stby" Starting instance "db11g"... ORACLE instance started. Database mounted. Database "db11g_stby" converted successfully DGMGRL> |
备库再次在恢复,并恢复了归档。
注意,flashback数据库仍然没有启用。
DGMGRL> SHOW CONFIGURATION;
Configuration - my_dg_config
Protection Mode: MaxPerformance Databases: db11g - Primary database db11g_stby - Physical standby database
Fast-Start Failover: DISABLED
Configuration Status: SUCCESS
DGMGRL> |