搭建19c Oracle ADG环境(DB_NAME与DB_UNIQUE_NAME不同)

Posted dingdingfish

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了搭建19c Oracle ADG环境(DB_NAME与DB_UNIQUE_NAME不同)相关的知识,希望对你有一定的参考价值。

概述

本文描述如何搭建一套ADG(Active Data Guard)环境。主备数据库均为单实例。

本实验过程主要参考以下两文档:

  • https://oracle-base.com/articles/19c/data-guard-setup-using-broker-19c
  • https://www.huaweicloud.com/articles/0473c346e8317d07b30c10c70196e675.html

环境准备

主备数据库环境如下,实例名和数据库名相同,DB_UNIQUE_NAME必须不同。

主数据库:

  • 主机名:dg01
  • 实例名:CDB
  • 数据库名:CDB
  • DB_UNIQUE_NAME: chicago
  • 数据库版本:19.12
  • 操作系统:OL 7.9

从数据库:

  • 主机名:dg02
  • 实例名:暂无,将被设为CDB
  • 数据库名:CDB
  • DB_UNIQUE_NAME: 暂无,将被设为boston
  • 数据库版本:19.12
  • 操作系统:OL 7.9

OCI上的cloud-init 脚本

如果不是用OCI创建环境请跳过此小节。

OCI上提供Image可以快速建立单实例数据库,方法参见这里

主数据库环境cloud-init 脚本:

#!/bin/bash
mount /u01
/u01/ocidb/GenerateNetconfig.sh -a
DBNAME=CDB DBCA_INITORA_PARAMETERS="db_unique_name=chicago" DBCA_CONTAINER_DB=yes DBCA_PLUGGABLE_DB_NAME=orclpdb1 DBCA_PLUGGABLE_DB_COUNT=1 DBCA_SAMPLE_SCHEMA=yes  /u01/ocidb/buildsingle.sh -s

备数据库环境cloud-init 脚本,仅安装数据库软件:

#!/bin/bash
mount /u01
/u01/ocidb/GenerateNetconfig.sh -a

在备数据库服务器上,追加以下设置到oracle用户下的~/.bashrc~/.bash_profile

#
# Oracle user environment setup
#
function addpath {

  [[ ":${PATH}:" =~ :$1: ]] && return
  PATH="$PATH:$1"
}

export ORACLE_BASE=/u01/app/oracle
export ORACLE_HOME=/u01/app/oracle/product/19c/dbhome_1
export ORACLE_UNQNAME=boston
export ORACLE_SID=CDB
# If binary locations do not already exist in
# $PATH add them to the end without duplicates...
addpath ${ORACLE_HOME%/}/bin
addpath ${ORACLE_HOME%/}/OPatch

为便于操作,主备数据库均安装rlwrap,然后在~/.bash_profile中添加以下设置:

alias sqlplus='rlwrap sqlplus'
alias sys='sqlplus / as sysdba'
export SYSPWD='Ora_DB4U'

主库设置

设置FRA

因为FRA的大小和位置都没有设置,于是配置如下。需要先设置大小:

alter system set db_recovery_file_dest_size  = '10G';
! mkdir -p /u01/app/oracle/FRA
alter system set db_recovery_file_dest = '/u01/app/oracle/FRA';

确认:

SQL> show parameter db_recovery

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
db_recovery_file_dest                string      /u01/app/oracle/FRA
db_recovery_file_dest_size           big integer 10G

由于设置了FRA,因此Archive destination由默认的/u01/app/oracle/product/19c/dbhome_1/dbs/arch变为USE_DB_RECOVERY_FILE_DEST

此时系统在FRA目录下自动建立了目录并将归档日志存放其中,目录名与DB_UNIQUE_NAME相同:

[oracle@dg01 ~]$ ls /u01/app/oracle/FRA
CHICAGO
[oracle@dg01 ~]$ ls /u01/app/oracle/FRA/CHICAGO
archivelog
[oracle@dg01 ~]$ ls /u01/app/oracle/FRA/CHICAGO/archivelog
2021_09_16

打开归档

最初归档未开:

SQL> SELECT log_mode FROM v$database;

LOG_MODE
------------
NOARCHIVELOG

执行以下命令打开归档:

shutdown immediate;
startup mount;
alter database archivelog;
alter database open;

确认:

SQL> SELECT log_mode FROM v$database;

LOG_MODE
------------
ARCHIVELOG

SQL> archive log list
Database log mode              Archive Mode
Automatic archival             Enabled
Archive destination            /u01/app/oracle/product/19c/dbhome_1/dbs/arch
Oldest online log sequence     12
Next log sequence to archive   14
Current log sequence           14

开启闪回

是可选的,但是建议打开:

SQL> alter database flashback on;

Database altered.

对目录结构的影响:

[oracle@dg01 ~]$ ls /u01/app/oracle/FRA/
CHICAGO
[oracle@dg01 ~]$ ls /u01/app/oracle/FRA/CHICAGO
flashback

系统视图包括V$FLASHBACK_DATABASE_LOGV$FLASHBACK_DATABASE_LOGFILE等,参考这里

打开force logging

这个属性可以在PDB一级设,不过为方便,此处在CDB一级设置。

SQL> select force_logging from v$database;

FORCE_LOGGING
---------------------------------------
NO

SQL> alter database force logging;

Database altered.

SQL> select force_logging from v$database;

FORCE_LOGGING
---------------------------------------
YES

添加Standby log file

先查看源数据库的redo。共有3个log group,每个group只有一个成员。log文件大小为200MB。log thread为1,因为是单实例。

SQL> 
col member for a60
set lines 120
select m.group#, m.member, g.bytes from v$log g join v$logfile m on m.group#=g.group# order by m.group#, m.member;

    GROUP# MEMBER                                                            BYTES
---------- ------------------------------------------------------------ ----------
         1 /u01/app/oracle/oradata/CHICAGO/redo01.log                    209715200
         2 /u01/app/oracle/oradata/CHICAGO/redo02.log                    209715200
         3 /u01/app/oracle/oradata/CHICAGO/redo03.log                    209715200

SQL> select group#, thread#, members, status from v$log;

    GROUP#    THREAD#    MEMBERS STATUS
---------- ---------- ---------- ----------------
         1          1          1 INACTIVE
         2          1          1 CURRENT
         3          1          1 INACTIVE

按照要求:

Each standby redo log file must be at least as large as the largest redo log file in the redo log of the redo source database. For administrative ease, Oracle recommends that all redo log files in the redo log at the redo source database and the standby redo log at a redo transport destination be of the same size.

另一个非常重要的要求见这里

The standby redo log must have at least one more redo log group than the redo log at the redo source database, for each redo thread at the redo source database.

因此添加3+1个standby log group,大小与主数据库一致:

ALTER DATABASE ADD STANDBY LOGFILE THREAD 1 GROUP 4 ('/u01/app/oracle/oradata/CHICAGO/standby_redo01.log') SIZE 200M;
ALTER DATABASE ADD STANDBY LOGFILE THREAD 1 GROUP 5 ('/u01/app/oracle/oradata/CHICAGO/standby_redo02.log') SIZE 200M;
ALTER DATABASE ADD STANDBY LOGFILE THREAD 1 GROUP 6 ('/u01/app/oracle/oradata/CHICAGO/standby_redo03.log') SIZE 200M;
ALTER DATABASE ADD STANDBY LOGFILE THREAD 1 GROUP 7 ('/u01/app/oracle/oradata/CHICAGO/standby_redo04.log') SIZE 200M;

从系统视图V$STANDBY_LOG可以确认。

SQL> select group#, thread#, dbid, bytes from V$STANDBY_LOG;

    GROUP#    THREAD# DBID                                          BYTES
---------- ---------- ---------------------------------------- ----------
         4          1 UNASSIGNED                                209715200
         5          1 UNASSIGNED                                209715200
         6          1 UNASSIGNED                                209715200
         7          1 UNASSIGNED                                209715200

SQL> 
col member for a60
set lines 120
select group#, type, member from v$logfile where type = 'STANDBY';

    GROUP# TYPE    MEMBER
---------- ------- ------------------------------------------------------------
         4 STANDBY /u01/app/oracle/oradata/CHICAGO/standby_redo01.log
         5 STANDBY /u01/app/oracle/oradata/CHICAGO/standby_redo02.log
         6 STANDBY /u01/app/oracle/oradata/CHICAGO/standby_redo03.log
         7 STANDBY /u01/app/oracle/oradata/CHICAGO/standby_redo04.log

初始化参数

DB_NAME,DB_UNIQUE_NAME都是chicago,实例名为CDB:

SQL> show parameter db_name

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
db_name                              string      CDB

SQL> show parameter db_unique

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
db_unique_name                       string      chicago
SQL> show parameter instance_name

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
instance_name                        string      CDB

主库和备库的数据库名必须一样,DB_UNIQUE_NAME必须不一样。

数据文件的目录是按照DB_UNIQUE_NAME建立的。

[oracle@dg01 ~]$ ls $ORACLE_BASE/oradata
CDB  CHICAGO
[oracle@dg01 ~]$ ls $ORACLE_BASE/oradata/CDB

[oracle@dg01 ~]$ ls $ORACLE_BASE/oradata/CHICAGO
control01.ctl  orclpdb1  redo01.log  redo03.log          standby_redo02.log  standby_redo04.log  system01.dbf  undotbs01.dbf
control02.ctl  pdbseed   redo02.log  standby_redo01.log  standby_redo03.log  sysaux01.dbf        temp01.dbf    users01.dbf

如果主库和备库在同一主机(一般是测试环境),则实例名不能一样;否则无妨。

主数据库设置参数:

alter system set log_archive_config='dg_config=(chicago,boston)' scope=both;
alter system set log_archive_dest_2='service=boston async valid_for=(online_logfile,primary_role) db_unique_name=boston';
ALTER SYSTEM SET STANDBY_FILE_MANAGEMENT=AUTO;

文档中的解释:

Set to AUTO so when data files are added to or dropped from the primary database, corresponding changes are made automatically to the standby database.

服务设置

包括监听和网络服务名,用于两个数据库之间的通讯,以及broker对数据库的监控。

本节的tnsnames.ora和listener.ora都位于目录$ORACLE_HOME/network/admin下。

主库和备库的tnsnames.ora均定义如下:

CHICAGO =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = dg01.sub07281614200.training.oraclevcn.com)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = chicago)
    )
  )

BOSTON =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = dg02.sub07281614200.training.oraclevcn.com)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = boston)
    )
  )

在主库和备库的listener.ora中添加以下静态服务:

SID_LIST_LISTENER =
  (SID_LIST =
        (SID_DESC = (GLOBAL_DBNAME = CHICAGO) (ORACLE_HOME = /u01/app/oracle/product/19c/dbhome_1) (SID_NAME = CDB) )
        (SID_DESC = (GLOBAL_DBNAME = BOSTON) (ORACLE_HOME = /u01/app/oracle/product/19c/dbhome_1) (SID_NAME = CDB) )
        (SID_DESC = (GLOBAL_DBNAME = CHICAGO_DGMGRL) (ORACLE_HOME = /u01/app/oracle/product/19c/dbhome_1) (SID_NAME = CDB) )
        (SID_DESC = (GLOBAL_DBNAME = BOSTON_DGMGRL) (ORACLE_HOME = /u01/app/oracle/product/19c/dbhome_1) (SID_NAME = CDB) )
  )

其中以_DGMGRL是给dgmgrl命令用的,具体参见Oracle Data Guard Broker and Static Service Registration (Doc ID 1387859.1)。如果安装了Clusterware,则可以不用定义。

此时主备数据库服务器上的listener.ora文件如下:

# 主数据库
LISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = TCP)(HOST = dg01.sub07281614200.training.oraclevcn.com)(PORT = 1521))
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
    )
  )

SID_LIST_LISTENER =
  (SID_LIST =
        (SID_DESC = (GLOBAL_DBNAME = CHICAGO) (ORACLE_HOME = /u01/app/oracle/product/19c/dbhome_1) (SID_NAME = CDB) )
        (SID_DESC = (GLOBAL_DBNAME = BOSTON) (ORACLE_HOME = /u01/app/oracle/product/19c/dbhome_1) (SID_NAME = CDB) )
        (SID_DESC = (GLOBAL_DBNAME = CHICAGO_DGMGRL) (ORACLE_HOME = /u01/app/oracle/product/19c/dbhome_1) (SID_NAME = CDB) )
        (SID_DESC = (GLOBAL_DBNAME = BOSTON_DGMGRL) (ORACLE_HOME = /u01/app/oracle/product/19c/dbhome_1) (SID_NAME = CDB) )
  )

# 备数据库
LISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = TCP)(HOST = dg02.sub07281614200.training.oraclevcn.com)(PORT = 1521))
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
    )
  )

SID_LIST_LISTENER =
  (SID_LIST =
        (SID_DESC = (GLOBAL_DBNAME = CHICAGO) (ORACLE_HOME = /u01/app/oracle/product/19c/dbhome_1) (SID_NAME = CDB) )
        (SID_DESC = (GLOBAL_DBNAME = BOSTON) (ORACLE_HOME = /u01/app/oracle/product/19c/dbhome_1) (SID_NAME = CDB) )
        (SID_DESC = (GLOBAL_DBNAME = CHICAGO_DGMGRL) (ORACLE_HOME = /u01/app/oracle/product/19c/dbhome_1) (SID_NAME = CDB) )
        (SID_DESC = (GLOBAL_DBNAME = BOSTON_DGMGRL) (ORACLE_HOME = /u01/app/oracle/product/19c/dbhome_1) (SID_NAME = CDB) )
  )

重启监听使其生效:

lsnrctl stop
lsnrctl start

确保在主备数据库服务器上均可以成功运行以下命令:

$ tnsping boston
$ tnsping chicago

如果不通,有可能是防火墙的问题,需开启1521端口:

firewall-cmd --permanent --zone=public --add-port=1521/tcp
systemctl restart firewalld

SID_LIST_listener参数的说明参见这里

目前监听的状态如下:

# 主数据库服务器
[oracle@dg01 ~]$ lsnrctl status

LSNRCTL for Linux: Version 19.0.0.0.0 - Production on 17-SEP-2021 11:02:45

Copyright (c) 1991, 2021, Oracle.  All rights reserved.

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=dg01.sub07281614200.training.oraclevcn.com)(PORT=1521)))
STATUS of the LISTENER
------------------------
Alias                     LISTENER
Version                   TNSLSNR for Linux: Version 19.0.0.0.0 - Production
Start Date                17-SEP-2021 11:01:45
Uptime                    0 days 0 hr. 1 min. 0 sec
Trace Level               off
Security                  ON: Local OS Authentication
SNMP                      OFF
Listener Parameter File   /u01/app/oracle/product/19c/dbhome_1/network/admin/listener.ora
Listener Log File         /u01/app/oracle/diag/tnslsnr/dg01/listener/alert/log.xml
Listening Endpoints Summary...
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=dg01)(PORT=1521)))
  (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))
Services Summary...
Service "86b637b62fdf7a65e053f706e80a27ca" has 1 instance(s).
  Instance "CDB", status READY, has 1 handler(s) for this service...
Service "BOSTON" has 1 instance(s).
  Instance "CDB", status UNKNOWN, has 1 handler(s) for this service...
Service "BOSTON_DGMGRL" has 1 instance(s).
  Instance "CDB", status UNKNOWN, has 1 handler(s) for this service...
Service "CDBXDB" has 1 instance(s).
  Instance "CDB", status READY, has 1 handler(s) for this service...
Service "CHICAGO" has 2 instance(s).
  Instance "CDB", status UNKNOWN, has 1 handler(s) for this service...
  Instance "CDB", status READY, has 1 handler(s) for this service...
Service "CHICAGO_DGMGRL" has 1 instance(s).
  Instance "CDB", status UNKNOWN, has 1 handler(s) for this service...
Service "cc20261d715f4612e05500001702ecab" has 1 instance(s).
  Instance "CDB", status READY, has 1 handler(s) for this service...
Service "orclpdb1" has 1 instance(s).
  Instance "CDB", status READY, has 1 handler(s) for this service...
The command completed successfully

# 从数据库服务器
[oracle@dg02 ~]$ lsnrctl status

LSNRCTL for Linux: Version 19.0.0.0.0 - Production on 17-SEP-2021 11:03:22

Copyright (c) 1991, 2021, Oracle.  All rights reserved.

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=dg02.sub07281614200.training.oraclevcn.com)(PORT=1521)))
STATUS of the LISTENER
------------------------
Alias                     LISTENER
Version                   TNSLSNR for Linux: Version 19.0.0.0.0 - Production
Start Date                17-SEP-2021 10:58:25
Uptime                    0 days 0 hr. 4 min. 56 sec
Trace Level               off
Security                  ON: Local OS Authentication
SNMP                      OFF
Listener Parameter File   /u01/app/oracle/product/19c/dbhome_1/network/admin/listener.ora
Listener Log File         /u01/app/oracle/diag/tnslsnr/dg02/listener/alert/log.xml
Listening Endpoints Summary...
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=dg02)(PORT=1521)))
  (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))
Services Summary...
Service "BOSTON" has 1 instance(s).
  Instance "CDB", status UNKNOWN, has 1 handler(s) for this service...
Service "BOSTON_DGMGRL" has 1 instance(s).
  Instance "CDB", status UNKNOWN, has 1 handler(s) for this service...
Service "CHICAGO" has 1 instance(s).
  Instance "CDB", status UNKNOWN, has 1 handler(s) for this service...
Service "CHICAGO_DGMGRL" has 1 instance(s).
  Instance "CDB", status UNKNOWN, has 1 handler(s) for this service...
The command completed successfully

备库设置

将主库的口令文件拷贝至备库(推荐),或者使用相同的口令创建口令文件:

cd $ORACLE_HOME/dbs
scp orapwCDB dg02:$ORACLE_HOME/dbs
# 或
orapwd file=/u01/app/oracle/product/19c/dbhome_1/dbs/orapwCDB password=Ora_DB4U entries=10

建立目录:

mkdir -p /u01/app/oracle/admin/boston/adump
mkdir -p /u01/app/oracle/FRA

验证可通过口令文件连接主数据库:

[oracle@dg02 ~]$ sqlplus system/$SYSPWD@chicago
SQL> show parameter db_unique

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
db_unique_name                       string      chicago

通过DUPLICATE创建备库

在备服务器,创建临时参数文件:

echo DB_NAME=CHICAGO > /tmp/initboston.ora

在备服务器:

$ sqlplus / as sysdba
Connected to an idle instance.

SQL> STARTUP NOMOUNT PFILE='/tmp/initboston.ora';
ORACLE instance started.

Total System Global Area  268434272 bytes
Fixed Size                  8895328 bytes
Variable Size             201326592 bytes
Database Buffers           50331648 bytes
Redo Buffers                7880704 bytes

连接RMAN:


[oracle@dg02 ~]$ rman TARGET sys/$SYSPWD@chicago AUXILIARY sys/$SYSPWD@boston

Recovery Manager: Release 19.0.0.0.0 - Production on Thu Sep 16 14:15:30 2021
Version 19.12.0.0.0

Copyright (c) 1982, 2019, Oracle and/or its affiliates.  All rights reserved.

connected to target database: CHICAGO (DBID=1693121009)
connected to auxiliary database: CHICAGO (not mounted)

RMAN>
DUPLICATE TARGET DATABASE 
FOR STANDBY
 FROM active DATABASE
 dorecover
 SPFILE 
SET db_unique_name='boston' 
SET fal_client='boston'
SET fal_server='chicago'
SET standby_file_management='AUTO' 
SET log_file_name_convert='/CHICAGO/','/BOSTON/' 
SET db_file_name_convert = '/CHICAGO/', '/BOSTON/'
SET log_archive_config = 'dg_config=(chicago,boston)'
SET log_archive_dest_2 = ''
SET audit_file_dest = '/u01/app/oracle/admin/boston/adump';

注意命令中的FOR STANDBY,以及目录转换时需要大写。

一切顺利,输出如下,没有报错:

...
Finished Duplicate Db at 17-SEP-21

此克隆把spfile也带过来了,也就是说不光数据,配置也会全部克隆过来:

[oracle@dg02 ~]$ ls $ORACLE_HOME/dbs
hc_CDB.dat  init.ora  lkBOSTON  orapwCDB  spfileCDB.ora

到目前为止,主备数据库除了DB_UNIQUE_NAME外,数据和配置都一样。
打开备库,查看其状态:

SQL> show pdbs

    CON_ID CON_NAME                       OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
         2 PDB$SEED                       MOUNTED
         3 ORCLPDB1                       MOUNTED


SQL> show parameter db_name

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
db_name                              string      CDB

SQL> show parameter db_unique_name

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
db_unique_name                       string      boston
SQL> show parameter instance_name

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
instance_name                        string      CDB

SQL> select status,instance_name from v$instance;

STATUS       INSTANCE_NAME
------------ ----------------
MOUNTED      CDB

SQL> select database_role,switchover_status from v$database;

DATABASE_ROLE    SWITCHOVER_STATUS
---------------- --------------------
PHYSICAL STANDBY NOT ALLOWED

备库执行以下命令:

alter database recover managed standby database using current l

以上是关于搭建19c Oracle ADG环境(DB_NAME与DB_UNIQUE_NAME不同)的主要内容,如果未能解决你的问题,请参考以下文章

ORACLE 19C ADG遇到一次奇怪的ORA-12154错误

ORACLE RAC19C到单实例ADG DUPLICATE ORA-01017 错误的解决办法

oracle 19c adg GAP恢复

oracle 19c adg GAP恢复

oracle 19c adg GAP恢复

Oracle Livelabs: 搭建ADG环境