Oracle Livelabs实验: 搭建ADG环境

Posted dingdingfish

tags:

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

本文是Oracle LiveLabs实验:Setting Up Active Data Guard For On-Premises 的过程记录。

实验步骤请参考这里

因为是利用你自己的OCI云环境搭建,因此实验没有终止时间。

首先创建2个虚拟机primary和standby,primary上创建数据库,standby上仅安装数据库软件。

这两个环境是利用OCI创建的,使用了Oracle应用市场上的Oracle Database Image。

虚机primary的初始化脚本如下:

#cloud-config
runcmd:
   - mount /u01
   - /u01/ocidb/GenerateNetconfig.sh > /u01/ocidb/netconfig.ini
   - SIDNAME=ORCL DBNAME=ORCL DBCA_PLUGGABLE_DB_NAME=orclpdb /u01/ocidb/buildsingle.sh -s

虚机standby的初始化脚本如下:

#cloud-config
runcmd:
   - mount /u01
   - /u01/ocidb/GenerateNetconfig.sh > /u01/ocidb/netconfig.ini
   - sudo /bin/chown -HRf oracle:oinstall /u01/app
   - sudo -u oracle /u01/app/oracle/product/19c/dbhome_1/oui/bin/runInstaller -silent -ignoreSysPrereqs -waitforcompletion -attachHome INVENTORY_LOCATION='/u01/app/oraInventory' ORACLE_HOME='/u01/app/oracle/product/19c/dbhome_1' ORACLE_HOME_NAME='OraDB19Home1' ORACLE_BASE='/u01/app/oracle'   -local
   - sudo -u oracle /u01/app/oraInventory/orainstRoot.sh

其中runInstaller使用OUI静态安装方式,可参考这里

standby上没有建库,因此很快就绪。

主数据库服务器配置

primary上建库就绪需要约30分钟:

[opc@primary ~]$ tail -f /u01/ocidb/buildsingle*.log
==> /u01/ocidb/buildsingle_createdbpostsql_2021Dec14_12_53_12.log <==
2021-12-14 12:53:12:[createdbpostsql:Start:primary] Running DBCA post creation scripts

INFO (node:primary): Not executing /u01/app/oracle/product/19c/dbhome_1/OPatch/datapatch since DBCA runs it automatically on release 18c and higher; detected Database major release (19)
2021-12-14 12:53:12:[createdbpostsql:Done :primary] Running DBCA post creation scripts
2021-12-14 12:53:12:[createdbpostsql:Time :primary] Completed successfully in 0 seconds (0h:00m:00s)

==> /u01/ocidb/buildsingle.log <==

INFO (node:primary): Current Single Instance state (12:53:13)...
oracle   13706     1  0 12:23 ?        00:00:00 /u01/app/oracle/product/19c/dbhome_1/bin/tnslsnr LISTENER -inherit
oracle   18737     1  0 12:52 ?        00:00:00 ora_dbw0_ORCL

INFO (node:primary): Single Instance running (see output above)

2021-12-14 12:53:13:[singlestate:Time :primary] Completed successfully in 0 seconds (0h:00m:00s)
2021-12-14 12:53:13:[buildsingle:Done :primary] Building 19c Single Instance
2021-12-14 12:53:13:[buildsingle:Time :primary] Completed successfully in 1836 seconds (0h:30m:36s)

确认数据库已启动:

[opc@primary ~]$ ps -ef | grep smon
oracle   18753     1  0 12:52 ?        00:00:00 ora_smon_ORCL

主库开启归档:

SQL> archive log list
Database log mode              No Archive Mode
Automatic archival             Disabled
Archive destination            /u01/app/oracle/product/19c/dbhome_1/dbs/arch
Oldest online log sequence     13
Current log sequence           15
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.

SQL> startup mount
ORACLE instance started.

Total System Global Area 4630509232 bytes
Fixed Size                  9143984 bytes
Variable Size             855638016 bytes
Database Buffers         3758096384 bytes
Redo Buffers                7630848 bytes
Database mounted.

SQL> alter database archivelog;
Database altered.

SQL> alter database open;
Database altered.

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     13
Next log sequence to archive   15
Current log sequence           15

主库开启闪回:

SQL> !mkdir -p /u01/app/oracle/fra/ORCL

SQL> ALTER SYSTEM SET DB_RECOVERY_FILE_DEST_SIZE = 10G SCOPE=BOTH SID='*';
System altered.

SQL> ALTER SYSTEM SET DB_RECOVERY_FILE_DEST = '/u01/app/oracle/fra/ORCL' SCOPE=BOTH SID='*';
System altered.

SQL> alter database flashback on;
Database altered.

启用force logging:

SQL> alter database force logging;
Database altered.

按照文档,启用force logging的目的如下:

In FORCE LOGGING mode, Oracle Database logs all changes in the database except changes in temporary tablespaces and temporary segments. This setting takes precedence over and is independent of any NOLOGGING or FORCE LOGGING settings you specify for individual tablespaces and any NOLOGGING settings you specify for individual database objects.

修改Redo Log大小:

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

    GROUP#      BYTES STATUS
---------- ---------- ----------------
         1  209715200 INACTIVE
         2  209715200 INACTIVE
         3  209715200 CURRENT

SQL> 
alter database add logfile group 4 '/u01/app/oracle/oradata/ORCL/redo04.log' size 1024M;
alter database add logfile group 5 '/u01/app/oracle/oradata/ORCL/redo05.log' size 1024M;
alter database add logfile group 6 '/u01/app/oracle/oradata/ORCL/redo06.log' size 1024M;

Database altered.
Database altered.
Database altered.

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

    GROUP#      BYTES STATUS
---------- ---------- ----------------
         1  209715200 INACTIVE
         2  209715200 INACTIVE
         3  209715200 CURRENT
         4 1073741824 UNUSED
         5 1073741824 UNUSED
         6 1073741824 UNUSED

6 rows selected.

-- switch log group,目的是为了删除老的log group
SQL> 
alter system switch logfile;
alter system checkpoint;

System altered.
System altered.

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

    GROUP#      BYTES STATUS
---------- ---------- ----------------
         1  209715200 INACTIVE
         2  209715200 INACTIVE
         3  209715200 INACTIVE
         4 1073741824 CURRENT
         5 1073741824 UNUSED
         6 1073741824 UNUSED

6 rows selected.

-- 删除老的redo log group
SQL>
alter database drop logfile group 1;
alter database drop logfile group 2;
alter database drop logfile group 3;

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

    GROUP#      BYTES STATUS
---------- ---------- ----------------
         4 1073741824 CURRENT
         5 1073741824 UNUSED
         6 1073741824 UNUSED

创建Standby Log,要比Primary redo log多一个:

SQL>
alter database add standby logfile thread 1 '/u01/app/oracle/oradata/ORCL/srl_redo01.log' size 1024M;
alter database add standby logfile thread 1 '/u01/app/oracle/oradata/ORCL/srl_redo02.log' size 1024M;
alter database add standby logfile thread 1 '/u01/app/oracle/oradata/ORCL/srl_redo03.log' size 1024M;
alter database add standby logfile thread 1 '/u01/app/oracle/oradata/ORCL/srl_redo04.log' size 1024M;

-- 为何是1、2、3、7?因为4、5、6已占用
SQL> select group#,thread#,bytes from v$standby_log;

    GROUP#    THREAD#      BYTES
---------- ---------- ----------
         1          1 1073741824
         2          1 1073741824
         3          1 1073741824
         7          1 1073741824

修改初始化参数:

alter system set STANDBY_FILE_MANAGEMENT=AUTO scope=both;
alter system set DB_LOST_WRITE_PROTECT=TYPICAL scope=both;
alter system set FAST_START_MTTR_TARGET=300 scope=both;

STANDBY_FILE_MANAGEMENT概念参见这里

DB_LOST_WRITE_PROTECT概念参见这里

FAST_START_MTTR_TARGET参见这里 , 此处设为300秒。

主备数据库服务器均开启1521端口:

sudo firewall-cmd --zone=public --add-port=1521/tcp --permanent
sudo firewall-cmd --reload
sudo firewall-cmd --list-all

VCN上已经开启1521端口,因此此处无需设置。

查询主备数据库服务器的IP地址和主机名:

# 主数据库服务器
[opc@primary ~]$ hostname -A
primary.subnet1.primaryvcn.oraclevcn.com
[opc@primary ~]$ hostname
primary
[opc@primary ~]$ hostname -I
10.0.1.185

# 备数据库服务器
[opc@standby ~]$ hostname -A
standby.subnet1.standbyvcn.oraclevcn.com
[opc@standby ~]$ hostname
standby
[opc@standby ~]$ hostname -I
10.0.1.205

然后将以下内容添加到主备服务器的/etc/hosts文件:

10.0.1.185 primary primary.subnet1.primaryvcn.oraclevcn.com
10.0.1.205 standby standby.subnet1.standbyvcn.oraclevcn.com

备数据库服务器配置

在~oracle/.bash_profile中添加以下内容:

export ORACLE_BASE=/u01/app/oracle; 
export ORACLE_HOME=/u01/app/oracle/product/19c/dbhome_1; 
export PATH=$ORACLE_HOME/bin:$PATH
export LD_LIBRARY_PATH=$ORACLE_HOME/lib
export ORACLE_SID=ORCL

使其生效:

$ source .bash_profile

启动监听:

$ lsnrctl start

配置static listener,理由参见MOS 1387859.1 。在文件$ORACLE_HOME/network/admin/listener.ora中添加以下内容:

SID_LIST_LISTENER=
  (SID_LIST=
    (SID_DESC=
     (GLOBAL_DBNAME=ORCL)
     (ORACLE_HOME=/u01/app/oracle/product/19c/dbhome_1)
     (SID_NAME=ORCL)
    )
    (SID_DESC=
     (GLOBAL_DBNAME=ORCL_DGMGRL)
     (ORACLE_HOME=/u01/app/oracle/product/19c/dbhome_1)
     (SID_NAME=ORCL)
    )
  )

重启监听:

$ lsnrctl reload

在主数据库服务器上的tnsnames.ora文件中添加以下内容:

# $ORACLE_HOME/network/admin/tnsnames.ora

ORCLSTBY =
  (DESCRIPTION =
   (SDU=65536)
   (ADDRESS_LIST =
    (ADDRESS = (PROTOCOL = TCP)(HOST = standby)(PORT = 1521))
   )
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = ORCLSTBY)
      (UR=A)
    )
  )

在备数据库服务器上的tnsnames.ora文件中添加以下内容:

LISTENER_ORCLSTBY =
  (ADDRESS = (PROTOCOL = TCP)(HOST = standby)(PORT = 1521))

ORCLSTBY =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = standby)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = ORCLSTBY)
    )
  )

ORCL =
  (DESCRIPTION =
   (SDU=65536)
   (ADDRESS_LIST =
    (ADDRESS = (PROTOCOL = TCP)(HOST = primary)(PORT = 1521))
   )
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = ORCL)
      (UR=A)
    )
  )

在备数据库服务器,以oracle用户创建以下目录:

mkdir -p /u01/app/oracle/oradata/ORCLSTBY/pdbseed
mkdir -p /u01/app/oracle/oradata/ORCLSTBY/orclpdb
mkdir -p /u01/app/oracle/admin/ORCLSTBY/adump
mkdir -p /u01/app/oracle/admin/ORCLSTBY/dpdump
mkdir -p /u01/app/oracle/admin/ORCLSTBY/pfile

在备数据库服务器,新建文件/u01/app/oracle/product/19c/dbhome_1/dbs/initORCLSTBY.ora,并添加以下内容:

DB_NAME=ORCL
DB_UNIQUE_NAME=ORCLSTBY

在备数据库服务器上,拷贝主数据库服务器上的口令文件:

$ scp primary:/u01/app/oracle/product/19c/dbhome_1/dbs/orapwORCL $ORACLE_HOME/dbs/

在备数据库服务器上,启动数据库到nomount状态:

$ sqlplus / as sysdba
SQL> startup nomount pfile='/u01/app/oracle/product/19c/dbhome_1/dbs/initORCLSTBY.ora'
ORACLE instance started.

Total System Global Area  251656864 bytes
Fixed Size                  8895136 bytes
Variable Size             184549376 bytes
Database Buffers           50331648 bytes
Redo Buffers                7880704 bytes

启动RMAN,从主库复制数据库:

$ rman target sys/Ora_DB4U@ORCL auxiliary sys/Ora_DB4U@ORCLSTBY

Recovery Manager: Release 19.0.0.0.0 - Production on Tue Dec 14 15:13:49 2021
Version 19.11.0.0.0

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

connected to target database: ORCL (DBID=1619057509)
connected to auxiliary database: ORCL (not mounted)

RMAN>
run 
duplicate target database for standby from active database
spfile
parameter_value_convert 'ORCL','ORCLSTBY'
set db_name='ORCL'
set db_unique_name='ORCLSTBY'
set db_create_file_dest='/u01/app/oracle/oradata/ORCLSTBY'
set db_recovery_file_dest='/u01/app/oracle/oradata/ORCLSTBY'
set db_file_name_convert='/ORCL/','/ORCLSTBY/'
set log_file_name_convert='/ORCL/','/ORCLSTBY/'
;


...
Finished Duplicate Db at 14-DEC-21

在主备数据库均运行以下命令:

show parameter dg_broker_config_file;
show parameter dg_broker_start;
alter system set dg_broker_start=true;
select pname from v$process where pname like 'DMON%';

输出如下:

-- 主库
SQL> show parameter dg_broker_config_file;

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
dg_broker_config_file1               string      /u01/app/oracle/product/19c/dbhome_1/dbs/dr1ORCL.dat
dg_broker_config_file2               string      /u01/app/oracle/product/19c/dbhome_1/dbs/dr2ORCL.dat

SQL> show parameter dg_broker_start;

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
dg_broker_start                      boolean     FALSE

SQL> alter system set dg_broker_start=true;

System altered.

SQL> select pname from v$process where pname like 'DMON%';

PNAME
-----
DMON

-- 备库
SQL> show parameter dg_broker_config_file;

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
dg_broker_config_file1               string      /u01/app/oracle/product/19c/dbhome_1/dbs/dr1ORCLSTBY.dat
dg_broker_config_file2               string      /u01/app/oracle/product/19c/dbhome_1/dbs/dr2ORCLSTBY.dat

SQL> show parameter dg_broker_start;

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
dg_broker_start                      boolean     TRUE

SQL> alter system set dg_broker_start=true;

System altered.

SQL> select pname from v$process where pname like 'DMON%';

PNAME
-----
DMON

在DGMGRL中注册ADG配置:

[oracle@primary dbs]$ dgmgrl sys/Ora_DB4U@ORCL
DGMGRL for Linux: Release 19.0.0.0.0 - Production on Tue Dec 14 15:24:07 2021
Version 19.11.0.0.0

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

Welcome to DGMGRL, type "help" for information.
Connected to "ORCL"
Connected as SYSDBA.
DGMGRL> CREATE CONFIGURATION adgconfig AS PRIMARY DATABASE IS ORCL CONNECT IDENTIFIER IS ORCL;
Configuration "adgconfig" created with primary database "orcl"
DGMGRL> ADD DATABASE ORCLSTBY AS CONNECT IDENTIFIER IS ORCLSTBY MAINTAINED AS PHYSICAL;
Database "orclstby" added
DGMGRL> ENABLE CONFIGURATION;
Enabled.
DGMGRL> SHOW CONFIGURATION;

Configuration - adgconfig

  Protection Mode: MaxPerformance
  Members:
  orcl     - Primary database
    orclstby - Physical standby database
      Warning: ORA-16854: apply lag could not be determined

Fast-Start Failover:  Disabled

Configuration Status:
WARNING   (status updated 5 seconds ago)

测试配置,在主库创建用户:

[oracle@primary dbs]$ sqlplus / as sysdba

SQL> show pdbs

    CON_ID CON_NAME                       OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
         2 PDB$SEED                       READ ONLY  NO
         3 ORCLPDB                        READ WRITE NO

alter session set container=orclpdb;
create user testuser identified by testuser;
grant connect,resource to testuser;
alter user testuser quota unlimited on users;
connect testuser/testuser@orclpdb
create table test(a number,b varchar2(20));
insert into test values(1,'line1');
commit;

然后在备库:

$ sqlplus / as sysdba
SQL> select open_mode,database_role from v$database;

OPEN_MODE            DATABASE_ROLE
-------------------- ----------------
MOUNTED              PHYSICAL STANDBY

SQL> alter database open;
Database altered.

SQL> alter pluggable database orclpdb open;
Pluggable database altered.

SQL> show pdbs

    CON_ID CON_NAME                       OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
         2 PDB$SEED                       READ ONLY  NO
         3 ORCLPDB                        READ ONLY  NO

SQL> select open_mode,database_role from v$database;

OPEN_MODE            DATABASE_ROLE
-------------------- ----------------
READ ONLY            PHYSICAL STANDBY

-- https://oracle-base.com/articles/9i/data-guard
SQL> alter database recover managed standby database cancel;
Database altered.

SQL> alter database recover managed standby database using current logfile disconnect;
Database altered.

SQL> select open_mode,database_role from v$database;

OPEN_MODE            DATABASE_ROLE
-------------------- ----------------
READ ONLY WITH APPLY PHYSICAL STANDBY

SQL> connect testuser/testuser@localhost:1521/orclpdb
Connected.

SQL> select * from test;

         A B
---------- --------------------
         1 line1

从主数据库服务器下载测试脚本:

wget https://objectstorage.us-ashburn-1.oraclecloud.com/p/Bq05Vhib-p_vraOu-wFpTEmyydA4d8qekXWXcb6W6M3pL43LVSAS2eFwKpYvAVxQ/n/c4u04/b/data-management-library-files/o/workload.sh
wget https://objectstorage.us-ashburn-1.oraclecloud.com/p/KNpGKB2VdoBWC5VWOHvD5vHg9P1OO5mqBJUxNonwY9LzaMaZ8Kcu7w3wBq9xgebW/n/c4u04/b/data-management-library-files/o/scn.sql

这两个脚本和多租户实验中的几乎一样:

[oracle@primary ~]$ cat workload.sh
echo ""
echo "  NOTE:"
echo "  To break out of this batch"
echo "  job, please issue CTL-C "
echo ""
echo "...sleeping 5 seconds"
echo ""
sleep 5

  sqlplus -S /nolog << EOF
  connect testuser/testuser@orclpdb;
  drop table sale_orders;
  create table sale_orders(ORDER_ID number, ORDER_DATE varchar2(9), CUSTOMER_ID number);
EOF

c=1
while [ $c -le 1000 ]
do
  sqlplus -S /nolog  << EOF
  connect testuser/testuser@orclpdb;
  insert all
    into sale_orders (ORDER_ID, ORDER_DATE, CUSTOMER_ID) VALUES (3041,'10-MAY-20', 13287)
    into sale_orders (ORDER_ID, ORDER_DATE, CUSTOMER_ID) VALUES (3041,'10-MAY-20', 13287)
    into sale_orders (ORDER_ID, ORDER_DATE, CUSTOMER_ID) VALUES (3041,'10-MAY-20', 13287)
    into sale_orders (ORDER_ID, ORDER_DATE, CUSTOMER_ID) VALUES (3041,'10-MAY-20', 13287)
    into sale_orders (ORDER_ID, ORDER_DATE, CUSTOMER_ID) VALUES (3041,'10-MAY-20', 13287)
    into sale_orders (ORDER_ID, ORDER_DATE, CUSTOMER_ID) VALUES (3041,'10-MAY-20', 13287)
    into sale_orders (ORDER_ID, ORDER_DATE, CUSTOMER_ID) VALUES (3041,'10-MAY-20', 13287)
    into sale_orders (ORDER_ID, ORDER_DATE, CUSTOMER_ID) VALUES (3035,'10-MAY-20', 13287)
    into sale_orders (ORDER_ID, ORDER_DATE, CUSTOMER_ID) VALUES (3041,'10-MAY-20', 13287)
    into sale_orders (ORDER_ID, ORDER_DATE, CUSTOMER_ID) VALUES (3041,'10-MAY-20', 13287)
  select 1 from dual;
  commit;
  select count(*) from sale_orders;
  @scn.sql
EOF
sleep 1
(( c++))

scn.sql如下:

$ cat scn.sql
conn system/Ora_DB4U@orclpdb
select current_scn, to_char(sysdate, 'YYYYMMDD-HH12MISS') time from v$database;

在主库运行脚本:

chmod a+x workload.sh
./workload.sh

在备库可以看到变化:

SQL> connect testuser/testuser@localhost:1521/orclpdb
Connected.

以上是关于Oracle Livelabs实验: 搭建ADG环境的主要内容,如果未能解决你的问题,请参考以下文章

Oracle LiveLabs实验:Oracle Database Hybrid Active Data Guard

Oracle LiveLabs实验: Oracle多租户基础

Oracle LiveLabs实验: Oracle多租户基础

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

Oracle LiveLabs实验:Oracle RAC Fundamentals

Oracle LiveLabs实验:Install Oracle Database 21c