主库 备库
ip 192.168.5.150 192.168.5.151
监听 动态/静态 静态
omf 启用 启用
db_recovery 启用 启用




主库配置

1.启动强制日志

  1. SQL> alter database force logging;

2.启动归档

修改归档文件格式

  1. SQL> alter system set log_archive_format='%t_%s_%r.arc' scope=spfile;

3.修改归档路径

  1. mkdir /opt/oracle/arc
  2. SQL> alter system set log_archive_dest_1='location=/opt/oracle/arc' scope=spfile;
  3. -----------------------------------------
  4. 使用快速闪回去存放归档
  5. alter system set log_archive_dest_1='LOCATION=USE_DB_RECOVERY_FILE_DEST';

4.启用归档,闪回数据库

  1. shutdown immediate;
  2. startup mount;
  3. alter database archivelog ;
  4. alter database flashback on;
  5. alter database open;

5.启用dg_broker_start

  1. show parameter dg_broker_start;
  2. alter system set dg_broker_start=TRUE scope=both;

6.添加DG_BROKER_CONFIG_FILE文件

  1. ALTER SYSTEM SET DG_BROKER_CONFIG_FILE1=$ORACLE_BASE/admin/db_unique_name/dr1db_unique_name.dat;
  2. ALTER SYSTEM SET DG_BROKER_CONFIG_FILE2=$ORACLE_BASE/admin/db_unique_name/dr2db_unique_name.dat;
  3. ALTER SYSTEM SET DG_BROKER_CONFIG_FILE1 = '+DG/DIRECTORY/DR1.DAT' SCOPE=BOTH;
  4. ALTER SYSTEM SET DG_BROKER_CONFIG_FILE2 = '+DG/DIRECTORY/DR2.DAT' SCOPE=BOTH;

7.清除之前手动配置的所有log_archive_dest_n 参数和log_archive_config参数

  1. alter system set log_archive_dest_n='' scope=spfile;
  2. alter system set log_archive_config='' scope=spfile;

8.创建监听

  1. DGORCL =
  2. (DESCRIPTION =
  3. (ADDRESS_LIST =
  4. (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.5.151)(PORT = 1521))
  5. )
  6. (CONNECT_DATA =
  7. (SERVICE_NAME = ORCL)
  8. )
  9. )
  10. ORCL =
  11. (DESCRIPTION =
  12. (ADDRESS_LIST =
  13. (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.5.150)(PORT = 1521))
  14. )
  15. (CONNECT_DATA =
  16. (SERVER = DEDICATED)
  17. (SERVICE_NAME = orcl)
  18. )
  19. )

9.创建DataGuard Broker配置

  1. show configuration
  2. create configuration 'dgorcl' as primary database is 'orcl' connect identifier is 'orcl';
  3. #添加备库必须在duplicate同步备库完成之后,并且添加standby日志之后
  4. add database 'DGORCL' as connect identifier is 'dgorcl' maintained as physical;
  5. enable configuration;

备库设置

1.设置初始化参数

  1. *.audit_file_dest='/opt/oracle/admin/DGORCL/adump'
  2. *.compatible='19.0.0'
  3. *.control_files='/opt/oracle/oradata/DGORCL/controlfile/o1_mf_gyhdym0y_.ctl','/opt/oracle/fast_recovery_area/DGORCL/controlfile/o1_mf_gyhdym26_.ctl'#Restore Controlfile
  4. *.db_create_file_dest='/opt/oracle/oradata'
  5. *.db_file_name_convert='/opt/oracle/oradata/ORCL/datafile','/opt/oracle/oradata/DGORCL/datafile'
  6. *.db_name='ORCL'
  7. *.db_recovery_file_dest='/opt/oracle/fast_recovery_area'
  8. *.db_recovery_file_dest_size=20g
  9. *.db_unique_name='dgorcl'
  10. *.log_file_name_convert='/opt/oracle/oradata/ORCL/onlinelog','/opt/oracle/oradata/DGORC/onlinelog','/opt/oracle/fast_recovery_area/ORCL/onlinelog','/opt/oracle/fast_recovery_area/DGORC/onlinelog'
  11. *.pga_aggregate_target=850m
  12. *.remote_login_passwordfile='EXCLUSIVE'
  13. *.sga_target=2600m
  14. *.standby_file_management='AUTO'
  15. *.dg_broker_start=TRUE

2.创建备库目录文件

  1. mkdir /opt/oracle/oradata/DGORCL -p
  2. mkdir /opt/oracle/admin/DGORCL/adump -p
  3. mkdir /opt/oracle/oradata/DGORCL/datafile
  4. mkdir /opt/oracle/oradata/DGORCL/onlinelog
  5. mkdir /opt/oracle/oradata/DGORCL/onlinelog -p
  6. mkdir /opt/oracle/fast_recovery_area/DGORCL/controlfile/ -p

3.启用dg_broker

  1. alter system set dg_broker_start=TRUE scope=both;

4.拷贝密码文件

注意oracle_sid 大小写

  1. scp orapwORCL oracle@192.168.5.151:/opt/oracle/product/19c/dbhome_1/dbs/

5.创建服务监听

  1. DGORCL =
  2. (DESCRIPTION =
  3. (ADDRESS_LIST =
  4. (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.5.151)(PORT = 1521))
  5. )
  6. (CONNECT_DATA =
  7. (SERVICE_NAME = ORCL)
  8. )
  9. )
  10. ORCL =
  11. (DESCRIPTION =
  12. (ADDRESS_LIST =
  13. (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.5.150)(PORT = 1521))
  14. )
  15. (CONNECT_DATA =
  16. (SERVER = DEDICATED)
  17. (SERVICE_NAME = orcl)
  18. )
  19. )

6.创建静态监听

  1. SID_LIST_LISTENER =
  2. (SID_LIST =
  3. (SID_DESC =
  4. (GLOBAL_DBNAME = ORCL)
  5. (ORACLE_HOME = /opt/oracle/product/19c/dbhome_1)
  6. (SID_NAME = dgorcl)
  7. )
  8. )
  9. LISTENER =
  10. (DESCRIPTION_LIST =
  11. (DESCRIPTION =
  12. (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.5.151)(PORT = 1521))
  13. )
  14. (DESCRIPTION =
  15. (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
  16. )
  17. )
  18. ADR_BASE_LISTENER = /opt/oracle

7.rman duplicate 同步

  1. rman target sys/Oracle123@orcl
  2. connect auxiliary sys/Oracle123@DGORCL
  3. duplicate target database for standby from active database nofilenamecheck;

8.修改备库为open

  1. alter database open;

9.备库添加standby logfile

  1. select decode(g.THREAD#, null, vs.THREAD#, g.THREAD#) THREAD#,
  2. decode(g.GROUP#, null, vs.GROUP#, g.GROUP#) GROUP#,
  3. decode(g.BYTES, null, vs.BYTES / 1024 / 1024, g.BYTES / 1024 / 1024) BYTES,
  4. decode(g.STATUS, null, vs.STATUS, g.STATUS) STATUS,
  5. decode(g.ARCHIVED, null, vs.ARCHIVED, g.ARCHIVED) ARCHIVED,
  6. --decode(g.GROUP#,null,'STANDBY REDO','REDO') type,
  7. gf.TYPE,
  8. gf.MEMBER
  9. from v$logfile gf
  10. left join v$log g
  11. on gf.GROUP# = g.GROUP#
  12. left join v$standby_log vs
  13. on gf.GROUP# = vs.GROUP#
  14. order by 1, 2;
  15. alter database add standby logfile group 4 ;
  16. alter database add standby logfile group 5 ;
  17. alter database add standby logfile group 6 ;
  18. alter database add standby logfile group 7 ;

dg broker切换和故障转移操作

1.验证主数据库和目标备用数据库是否处于以下状态:

  1. 主数据库已启用且处于状态。TRANSPORT-ON
  2. 目标备用数据库已启用,处于状态。APPLY-ON

将主数据库日志发送到备库

  1. ALTER SYSTEM FLUSH REDO to orcl;
  2. 或者切换日志文件
  3. alter system switch logfile;

手动切换

  1. validate database 'orcl';
  2. validate database 'dgorcl';
  3. switchover to dgorcl;

故障切换

ORA-16600: not connected to target standby database for failover
注意:故障切换在备库执行,主库执行会报ORA-16600:

  1. FAILOVER TO dgorcl;
  2. FAILOVER TO dgorcl IMMEDIATE;

回复故障数据
1.重建dg
2.命令回复
如果数据库可以恢复,数据库将显示以下状态:

  1. ORA-16661: the standby database needs to be reinstated

如果必须从新主数据库的副本重新创建数据库,它将具有以下状

  1. ORA-16795: the standby database needs to be re-created

要使命令成功,必须在故障转移之前在数据库上启用闪回数据库,并且必须在该数据库上有足够的闪回日志。此外,要恢复的数据库和新的主数据库必须具有网络连接
要恢复数据库:

  1. 将数据库重新启动到装载状态
  2. 连接到新的主数据库
  3. 使用云控制或 DGMGRL 恢复数据库
  1. 1.重启故障数据库 mount
  2. shutdown immediate;
  3. startup mount;
  4. 2.进去现在主库dgmgrl
  5. dgmgrl sys/Oracle123@dgorcl
  6. REINSTATE database orcl;

dg broker 管理

1. 数据库状态和说明

数据库角色 状态名称 描述
主要 TRANSPORT-ON 重做传输服务设置为在主数据库打开以进行读/写访问时将重做数据传输到备用数据库或远同步实例。
如果这是 Oracle RAC 数据库,则所有在读/写模式下打开的实例都将运行重做传输服务。
这是首次启用主数据库时的默认状态。
主要 TRANSPORT-OFF 重做传输服务在主数据库上停止。
如果这是 Oracle RAC 数据库,则重做传输服务不会在任何实例上运行。
物理备用 APPLY-ON 在物理备用数据库上启动”重做应用”。
如果备用数据库是 Oracle RAC 数据库,则代理将完全在一个称为应用实例的备用实例上启动 Redo 应用。如果此实例失败,代理将自动选择另一个已装载或打开只读的实例。然后,此新实例将成为应用实例。
从 Oracle 数据库 12c版本 2 (12.2.0.1) 开始,可以将 Redo Apply 设置为在每个活动运行的物理备用实例上运行。如果数据库已设置为在多个实例上运行 Redo 应用,则可以使用 Data Guard 代理属性限制在 Oracle RAC 物理备用数据库上”重新应用”中涉及的实例数。ApplyInstances
APPLY-ON是首次启用物理备用数据库时的默认状态。
如果已购买 Oracle 活动数据防护选项的许可证,则可以在 Redo 应用处于活动状态时打开物理备用数据库。此功能称为实时查询。有关详细信息,请参阅Oracle 数据保护概念和管理
物理备用 APPLY-OFF 重做应用已停止。
如果这是 Oracle RAC 数据库,则在将数据库状态更改为 之前,没有运行应用服务的实例。APPLY-ON
逻辑待机 APPLY-ON SQL Apply 在逻辑备用数据库打开且逻辑备用数据库保护处于打开状态时启动。
如果这是 Oracle RAC 数据库,则 SQL Apply 正在一个实例(应用实例)上运行。如果此实例失败,代理将自动选择另一个打开的实例。此新实例将成为应用实例。
这是逻辑备用数据库首次启用时的默认状态。
逻辑待机 APPLY-OFF SQL 应用已停止。逻辑备用数据库保护处于打开状态。
如果这是 Oracle RAC 数据库,则在将状态更改为 之前,没有运行 SQL Apply 的实例。APPLY-ON

2.数据库状态转换

  1. EDIT DATABASE 'North_Sales' SET STATE='TRANSPORT-OFF'; #关闭"关闭重做传输服务"到所有备用数据库
  2. SHOW DATABASE 'North_Sales';
  3. EDIT DATABASE 'South_Sales' SET PROPERTY 'LogShipping'='OFF'; #5 关闭重做传输服务到特定备用数据库
  4. SHOW DATABASE 'South_Sales' 'LogShipping';

3.查看/修改数据库属性

  1. SHOW DATABASE 'North_Sales' 'InconsistentLogXptProps';
  2. EDIT DATABASE 'North_Sales' SET PROPERTY LogXptMode='SYNC';

4.修改应用延迟

应用延迟是由于传播和应用重做方面的延迟而使备用数据库或远同步实例中的数据滞后于主数据库中的数据程度的度量

  1. EDIT DATABASE 'South_Sales' SET PROPERTY 'ApplyLagThreshold'=15;

5.管理数据保护模式

注意:
在以下情况下无法使用最大保护模式:

  • 如果配置中的唯一备用数据库是快照备用数据库
  • 如果远同步实例是从主数据库同步模式接收重做的唯一配置成员

    Oracle 数据保护保护模式和要求

保护模式 重做运输 需要备用重做日志文件? 可用于快速启动故障转移?
MAXPROTECTION SYNC 是的 是的
MAXAVAILABILITY SYNC,FASTSYNC 是的 是的
MAXPERFORMANCE ASYNC 是的 是的

如果计划将整个 Oracle 数据防护配置设置为在最大可用性模式下运行,则必须使用 命令设置重做传输服务的模式

  1. EDIT DATABASE 'South_Sales' SET PROPERTY LogXptMode='SYNC';
  2. 或者
  3. EDIT DATABASE 'North_Sales' SET PROPERTY RedoRoutes = '(LOCAL : South_Sales SYNC)';
  4. EDIT CONFIGURATION SET PROTECTION MODE AS MAXAVAILABILITY; #修改为最大保护模式

6.管理数据库转换

与物理或逻辑备用数据库一样,快照备用数据库接收并存档从主数据库重做数据。但是,与物理或逻辑备用数据库不同,快照备用数据库不应用它接收的重做数据。在首先放弃对快照备用数据库所做的任何本地更新后,在快照备用数据库转换回物理备用数据库之前,不会应用快照备用数据库接收的重做数据。
物理备用数据库转换为快照备用数据库,必须启用闪回数据库

  1. CONVERT DATABASE 'dgorcl' TO SNAPSHOT STANDBY; #物理备用数据库转换为快照备用数据库
  2. CONVERT DATABASE 'South_Sales' TO PHYSICAL STANDBY;快照转换回物理备用数据库

7.查询数据库状态

通过 DGMGRL 命令行接口直接访问以下属性:

  • LogXptStatus
  • InconsistentLogXptProps

  • ``` DGMGRL> SHOW DATABASE ‘North_Sales’;

Database - North_Sales Role: PRIMARY Intended State: TRANSPORT-OFF Instance(s): north_sales1 Error: ORA-16737: the redo transport service for standby database “South_Sales” has an error

  1. north_sales2
  2. Error: ORA-16737: the redo transport service for standby
  3. database "South_Sales" has an error
  4. Warning: ORA-16715: redo transport-related property
  5. ReopenSecs of standby "South_Sales" is inconsistent

Database Status: ERROR

  1. 要进一步检查有关数据库状态的详细信息,可以使用以下可监视属性:
  2. - `LogXptStatus` 列出在主数据库的所有实例上检测到的所有日志传输错误。<br />
  3. - `InconsistentLogXptProps` 列出备用数据库的所有重做与传输相关的属性,这些属性在代理配置文件和重做传输设置之间具有不一致的值。<br />

DGMGRL> SHOW DATABASE ‘North_Sales’ ‘LogXptStatus’; LOG TRANSPORT STATUS PRIMARY_INSTANCE_NAME STANDBY_DATABASE_NAME STATUS north_sales1 South_Sales ORA-12541: TNS:no listener north_sales2 South_Sales ORA-12541: TNS:no listener

DGMGRL> SHOW DATABASE ‘North_Sales’ ‘InconsistentLogXptProps’; INCONSISTENT LOG TRANSPORT PROPERTIES INSTANCE_NAME STANDBY_NAME PROPERTY_NAME MEMORY_VALUE BROKER_VALUE north_sales2 South_Sales ReopenSecs 600 300

  1. <a name="yNOph"></a>
  2. ### 8.修改dgmgrl数据库监听

edit database orcl set property StaticConnectIdentifier=’(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=192.168.5.151)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=dgorcl)(INSTANCE_NAME=dgorcl)(SERVER=DEDICATED)))’;

  1. <a name="4HoNw"></a>
  2. ### 异常处理
  3. <a name="wu522"></a>
  4. #### 1、ORA-16857: member disconnected from redo source for longer than specified threshold
  5. 原因:通常是主库redo与备库standby redo 大小不一致或者group number不匹配.<br />解决方案 :取消备库同步,删除备库standby file 重新创建standby file ,在激活备库同步<br />注意:db_create_file_dest和db_recovery_file_dest 参数值是否启用,如果启用需要主备库同时启用,standby logfile同一个组会创建两个成员在这两个位置。

alter database recover managed standby database cancel;

alter database drop standby logfile group 4; alter database drop standby logfile group 5; alter database drop standby logfile group 6; alter database drop standby logfile group 7;

select decode(g.THREAD#, null, vs.THREAD#, g.THREAD#) THREAD#, decode(g.GROUP#, null, vs.GROUP#, g.GROUP#) GROUP#, decode(g.BYTES, null, vs.BYTES / 1024 / 1024, g.BYTES / 1024 / 1024) BYTES, decode(g.STATUS, null, vs.STATUS, g.STATUS) STATUS, decode(g.ARCHIVED, null, vs.ARCHIVED, g.ARCHIVED) ARCHIVED, —decode(g.GROUP#,null,’STANDBY REDO’,’REDO’) type, gf.TYPE, gf.MEMBER from v$logfile gf left join v$log g on gf.GROUP# = g.GROUP# left join v$standby_log vs on gf.GROUP# = vs.GROUP# order by 1, 2;

alter database add standby logfile group 4 ; alter database add standby logfile group 5 ; alter database add standby logfile group 6 ; alter database add standby logfile group 7 ;

alter database recover managed standby database disconnect from session;

  1. <a name="iA8EO"></a>
  2. #### 2、ORA-02097: parameter cannot be modified because specified value is invalid,ORA-16604: Data Guard broker configuration file inaccessible
  3. 原因:通常是dg_broker_config_file2和dg_broker_config_file1配置路径不存在,导致创建文件,创建好路径即可.
  4. <a name="UeNUe"></a>
  5. #### 3、Error: ORA-16587: ambiguous object specified to Data Guard broker
  6. 原因:通过log_archive_dest_n里面配置db_unique_name名字重复导致,改成正确的即可.
  7. <a name="ihsbB"></a>
  8. #### 4、ORA-16853: apply lag has exceeded specified threshold
  9. 原因:解决备库是否开启实时应用日志、检查主备库redo,standby redo大小是否一直以及mrp是否正常启动
  10. <a name="hkAUT"></a>
  11. #### 5.ORA-01275: 自动进行备用文件管理时, 不允许进行 DROP LOGFILE 操作。

alter system set standby_file_management=manual scope=both; alter system set standby_file_management=auto scope=both;

  1. <a name="Vl86m"></a>
  2. #### 6. ORA-16672: switchover not permitted to standby database with non-zero DelayMins

edit database orcl set property DelayMins=0; edit database dgorcl set property DelayMins=0;

  1. <a name="on1kE"></a>
  2. #### 7. ORA-01152: file 1 was not restored from a sufficiently old backup 不能open 备库
  3. 拷贝主库的归档日志文件到备库archivelog文件下

Check the standby status:

SQL>select open_mode from v$database;

SQL>select sequence#,applied from v$archived_log order by sequence#;

SQL>SELECT THREAD#, LOW_SEQUENCE#, HIGH_SEQUENCE# FROM V$ARCHIVE_GAP;

SQL>select process,status from v$managed_standby;

—-If you find the MRP0 status is WAIT_FOR_GAP, so, we can apply manually thearchivelog to standby.

Solution:

Copythe archive log from the primary to standby.

Inthe standby:

$sqlplus / as sysdba

SQL>recover managed standby database cancel;

SQL>recover automatic standby database;

SQL>alter database recover managed standby database disconnect from session;

recover automatic standby database; AUTO

alter database open;

  1. <a name="z3dga"></a>
  2. #### 8.Warning: ORA-16854: apply lag could not be determined
  3. 切换主库redo 日志

alter system SWITCH LOGFILE; ```