Oracle 11G 配置 ADG

Oracle Data Guard企业数据的高可用性、数据保护和灾难恢复,简称:DG。DG提供一组全面的服务,用于创建、维护、管理和监视一个或多个备用数据库;使生产数据库能够在灾难和数据损坏时得到有效保护和幸存。DG将备库作为生产副本的方式进行维护。的那个生产数据库由于计划或计划外的停机而变得不可用时,DG可以将任何备用数据库切换到主库角色,从而最小化停机时间。DG可以与传统的备份、恢复和集群技术一起使用,目的就是提供高水平的数据保护和数据可用性。DG可以将资源密集型备份和报告操作转移到备用系统优化生产库性能。那么DG有哪些特点呢?

灾难恢复、数据保护和高可用性:Data Guard提供了高效、全面的灾难恢复和高可用性解决方案;易于管理的切换和故障转移功能、允许在主数据库和备用数据库之间进行角色转换、从而最小化主数据库在计划内和计划外时的停机时间。

完整的数据保护:数据保护可以确保零数据丢失、即使面对不可预见的灾难;备用数据库可以防止数据损失和用户错误。由于主库接收的Redo重做数据会在备用库验证、所以主库的存储级物理损坏不会传播到备库、可以防止导致主库永久损坏的逻辑损坏或用户错误。

有效利用系统资源:使用从主库接收到的重做数据更新的备库表可用于其他任务,如备份、报告、求和和查询;从而减少主库执行这些任务所需的主数据库工作负载,节省宝贵的CPU和I/O周期。

灵活性以平衡可用性和性能需求:Oracle Data Guard提供了最大的保护、最大的可用性和最大的性能模式、帮助企业平衡数据可用性和系统性能需求。

自动间隙检测:如果主数据库和一个或多个备用数据库之间失联(例如:网络故障),Redo数据无法正常传送。一旦重新连接,DG会自动检测丢失的归档重做日志文件(称为GAP),自动将丢失的归档重做日志文件传输到备库,备用数据库与主数据库是同步的,DBA无需手动干预。

集中简单管理(Data Guard Broker):数据保护代理提供图形界面和命令行界面,以便在数据保护配置中跨多个数据库自动化管理和操作任务;代理还监视单个数据保护配置中的所有系统。

与Oracle数据库集成:数据保护是Oracle数据库企业版的一个特性,不需要像RAC一样单独安装。

自动转换角色:当启用快速启动故障转移时,在主站点发生灾难时;数据保护代理自动故障转移到同步备用站点,不需要DBA的干预。此外、还会自动通知应用程序角色转换。

1、Oracle Data Guard基本概念

Oracle Data Guard 是在主节点与备用节点之间通过日志同步来保证数据的同步,它可以实现数据快速切换与灾难性恢复。Data Guard 只是在软件上对数据库进行设置,并不需要额外购买任何组件。用户能够在对主数据库影响很小的情况下实现主备数据库的同步。RAC 主要是实例级的冗余方案、而 Data Guard 是数据级的冗余方案。 Data Guard 的实现方式有两种:Physical Standby 和 Logical Standby 。

image-20210813151739393

1.1、物理备用数据库

在Physical Standby模式中、Standby database 是主数据库的物理备份;而Redo 改变是把改变的日志从主库传递到备库(standby database);然后改变的redo在备库上被应用。也就是说在磁盘上的数据库结构与主数据库在块对块的基础上相同,提供与主库物理上相同的副本。数据库模式(包括索引)都是相同的,物理备用数据库通过Redo Apply与主数据库保持同步;Redo Apply恢复从主数据库接收到的重做数据,并将Redo应用与物理备库。从Oracle11G版本(11.1)开始,物理备库可以在只读模式打开时接收和应用重做Redo。因此,可以同时使用物理备用数据库进行数据保护和报告。

image-20210705212558287

image-20210813151558264

注:物理备用数据库进行的是主数据库数据块的备份、使用数据库恢复功能来应用变化。

1.2、逻辑备用数据库

数据的物理组织和结构可能不同,如:平台异构。主库与备库逻辑信息相同。逻辑备库通过SQL Apply与主库同步,后者将从主数据库接收的重做中的数据转换为SQL语句,然后在备用数据库上执行SQL语句。

将主库的Redo Log传递到备库后,再利用logminer的工具,从Redo Log中解析出SQL语句;在备库执行,以保证和主库同步。(主库和备库可以是不同的环境,备库可以处于读写状态)逻辑备库可以看作是一个单独的库,数据库名和DBID和主库都不一样;物理的备库和主库的数据库名为DBID都是一样的。

除了灾难恢复外,用户可以随时访问用于查询和报告目的的逻辑备库。使用逻辑备库,可升级Oracle数据库软件和补丁集,几乎不需要停机。因此,可以同时使用逻辑备用数据库进行数据保护和数据库升级。

image-20210705213419207

也可以把逻辑备用数据理解为是主库对象的一个子集合、日志从主库传到备库。Redo被转换为SQL,然后再备库上执行。逻辑Standby也要通过Primary数据库(或其备份,或其复制库,如物理Standby)创建,因此在创建之初与物理Standby数据库类似。不过由于逻辑Standby通过SQL应用的方式应用REDO数据,因此逻辑Standby的物理文件结构,甚至数据的逻辑结构都可以与Primary不一致。

image-20210813151609684

注:逻辑备用数据库是一个开发独立的以及活动的数据库、当重做数据通过SQL进行应用的时候可以进行报表查询;可以生成额外的索引和物化视图以获取更好的查询性能。

1.3、备用数据库快照

快照备用数据库是完全可更新的备用数据库;与物理或逻辑备库一样,快照备库从主库接收并归档Redo数据。与物理或逻辑备库不同,快照备用数据库不应用他接收到的重做数据。快照备库接收到的重做数据在将快照备库转换回物理备库之前不会应用;转换物理备库之前,将首先丢弃对快照备库所做的任何本地更新。

那么问题就来了、在 Physical Standby 和 Logical Standby 模式中我们该如何选择呢?选择重做应用还是SQL应用?

重做应用:物理备用数据库从主数据库进行块到块的复制,可以以只读查询的方式打开,在11G中支持实时报告;在角色变化时,确保备用数据库是主数据库的完全备份;可以用于快速备份、有更高的性能。

SQL应用:逻辑的,备用数据库从主数据库进行的事务到事务的拷贝。允许创建额外的对象,对对象做出修改;在创建对象的时候可以跳过应用以读写的方式打开(在表中以SQL应用维护的数据不会发生变化),支持实时报告,对数据类型的支持有限制。

通俗点讲就是、选择重做应用还是SQL应用就取决于您的实际业务需求了。

2、DG的三种模式

2.1、最大性能模式

这是默认的保护模式,它在不影响主数据库性能的情况下提供最高级别的数据保护。这是通过允许事务在由哪些事务生成的所有重做数据被写入在线日志后立即提交来实现的。重做数据写入一个或多个备用数据库,但这是根据事务承诺异步实现的,因此主数据库性能不受向备用数据库写入重做数据延迟的影响。主库把归档的 archived log通过arch进程传递给从库,在这种方式下,主库运行性能最高,但是不能保证数据不丢失,且丢失的数据受redo log的大小影响。在redo log过大的情况下,可能一天都没有归档一个日志,可以通过手工切换日志的方式来减小数据的丢失。

这种模式保证主库性能最大化,主备库之间数据是异步传输的。即,主备日志归档以后才会传输到备用库,在备库上使用归档日志文件做恢复操作。

2.2、最大可用模式

此模式在不影响主数据库可用性的情况下,提供了最高级别的数据保护。事务只有在将恢复这些事务所需的所有重做数据写入至少一个同步备用数据库的在线重做日志和备用重做日志之后才会提交。如果主数据库不能将其重做流写入至少一个同步备用数据库,则其操作方式就像除于最大性能模式,以保持主数据库的可用性;直到再次能够将其重做流写入同步备用数据库为止。这种保护模式可以确保零数据丢失,除非出现某些双重故障,例如备用数据库失败后主数据库失败。此模式提供的数据保护略少于最大可用性模式,对主数据库性能的影响也很小。在最大可用模式下,如果和从库的连接正常,运行方式等同Maximum protection模式,事务也是主从库同时提交。如果从库和主库失去联系,则主库自动切换到Maximum performance模式下运行,保证主库具有最大的可用性。

这种模式和”最大保护”基本上差不多。正常情况下,主备库之间是同步的。当网络或者备库出现问题时,不会影响到主库的宕机,主库会自动转换到”最大性能”模式,等待备库可用时,将归档传输到备库做恢复。可以把这种模式理解为”最大保护”和”最大性能”两种模式的中间体。

2.3、最大保护模式

此模式确保在主数据库失败时不会发生数据丢失。为了提供这种级别的保护,在事务提交之前,必须将恢复事务所需的重做数据写入至少一个同步备用数据库上的Redo日志和备用Redo日志。为了确保不会发生数据丢失,如果主数据库不能将其重做流写入至少一个同步备用数据库,则主数据库将关闭;而不是继续处理事务。换句话说就是可以保证从库和主库数据完全一样,做到zero data loss.事务同时在主从两边提交完成,才算事务完成。如果从库宕机或者网络出现问题,主从库不能通讯,主库也立即宕机。在这种方式下,具有最高的保护等级。但是这种模式对主库性能影响很大,要求高速的网络连接。

总结:目前国内基本上是最大性能模式,虽然最大可用模式也挺好的,如果网络没有问题最大可用模式和最大保护模式一样;如果网络不好最大可用模式和最大性能模式一样。首先最大保护模式在只有一台standby database 的情况下一般不会使用的,一方面对主库的性能影响比较的大;另一方面要保证快速安全的网络速度,如果网络断开或者standby database 失效的话,那么会引起主库的down机。虽然说可以最大保护数据,但是还是不安全,如果有多台standby database 的话可以考虑。最大性能模式虽然对主库的性能影响不大,但是对数据的保护不好啊,一般日志默认大小是100M;如果主库的磁盘全坏了,那至少要损失100m 的日志数据啊,这就起不到保护数据的作用。现在比较好的就是最大可用模式,在正常情况下运行在最大保护模式下,如果网络或者standby dababase 有问题的时候会自动切换到最大性能模式下,但是我在测试的时候发现如果我把standby 以read only open 的时候发现主库就不传送日志了,stanndby database 就失效了。至于需要用什么模式、那就需要看具体的生产需求了。

最安全的模式,这种模式主备库之间数据是同步的。即主库提交的同时,备库会做相应的恢复。最大限度的保证了数据完整性。不允许数据的丢失。如果主备库之间网络,或者备库出现问题会直接影响主库操作。导致主库宕机。因此一般不会选择最大保护模式。

注:影响DG保护模式选择的最大因素就是网络质量,如果网络质量比较好,比如本地的局域网,则可以选择最高可用模式。如果网络质量一般,则选择最大性能模式。一般不会选择最大保护模式,最大保护模式损害了系统的可用性。

DG 的相关后台进程:

ARCH(archiver)                             # 心跳检测
LNS(log-write network-server)              # log传送
RFS(remote file server)                    # 远程接数据
FAL(Fetch Archive Log)                     # 解决Redo的间隔GAP
MRP(Managed Recovery Process)              # 日志被应用,恢复的过程
LSP(Logical Standby Process)               # 逻辑数据库的后台进程,SQL转换

3、日志传输

日志传输服务(又称为重做传输服务)旨在将主数据库的变更传播给备用数据库,其传播方式有二 — 传送归档日志 (ARCH),或者在 Logwriter 进程处理重做数据的同时持续传送重做数据 (LNS)。

日志应用服务(对物理备用数据库又称为“重做应用”,对逻辑备用数据库又称作“SQL 应用”)负责将重做信息从归档重做日志文件或备用重做日志文件应用到备用数据库。

日志传输方式有两种、分别是ARCH和LGWR。

ARCH:把完整的REDO文件COPY到Standby数据库服务器上。

image-20210813151621464

LGWR(ASYNC):Redo Written by LGWR to local disk;LGWR将Redo写到本地LOG文件;LNSn进程读取Redo,传输到Standby服务器上。

image-20210813151633257

LGWR(SYNC):Redo written to Standby by LGWR – modes are:LGWR将Redo信息直接写到Standby数据库服务器上。

image-20210813151644191

4、安装数据库

4.1、基础环境

Primary Standby
操作系统 CentOS Linux release 7.6.1810 (Core) CentOS Linux release 7.6.1810 (Core)
服务器名称 oracle11gdg01 oracle11gdg02
IP地址规划 172.16.200.50 172.16.200.51
数据库版本 oracle 11g r2 11.2.0.4 oracle 11g r2 11.2.0.4
db_name primary primary
db_unique_name primary standby
instance_name primary standby
service_name primary standby
数据库安装 安装数据库软件+创建监听+安装数据库 安装数据库软件+创建监听

注:如果安装完成以后不确定上面的基本参数、可以通过下面的命令进行查询。

# 安装完成后可以通过以下命令查询
select dbid,name,open_mode,db_unique_name from v$database;
select instance_name from v$instance;

4.2、环境变量

主库基本环境变量:

[oracle@oracle11gdg01 ~]$ cat .bash_profile 
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/.local/bin:$HOME/bin

export PATH

export ORACLE_BASE=/u01/app/oracle
export ORACLE_HOME=$ORACLE_BASE/product/11.2.0/db_1
export ORACLE_SID=primary
export LD_LIBRARY_PATH=$ORACLE_HOME/bin:/bin:/usr/bin:/usr/local/bin:
export CLASSPATH=$ORACLE_HOME/JRE:$ORACLE_HOME/jlib:$ORACLE_HOME/rdbms/jlib
export PATH=$ORACLE_HOME/bin:$PATH
[oracle@oracle11gdg01 ~]$ 

备库基本环境变量:

[oracle@oracle11gdg02 ~]$ cat .bash_profile 
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/.local/bin:$HOME/bin

export PATH

export ORACLE_BASE=/u01/app/oracle
export ORACLE_HOME=$ORACLE_BASE/product/11.2.0/db_1
export ORACLE_SID=standby
export LD_LIBRARY_PATH=$ORACLE_HOME/bin:/bin:/usr/bin:/usr/local/bin:
export CLASSPATH=$ORACLE_HOME/JRE:$ORACLE_HOME/jlib:$ORACLE_HOME/rdbms/jlib
export PATH=$ORACLE_HOME/bin:$PATH
[oracle@oracle11gdg02 ~]$ 

4.3、环境依赖

yum install -y binutils compat-libcap1 compat-libstdc++-33 compat-libstdc++-33.i686 gcc gcc-c++ glibc glibc.i686 glibc-devel glibc-devel.i686 ksh libgcc libgcc.i686 libstdc++ libstdc++.i686 libstdc++-devel libstdc++-devel.i686 libaio libaio.i686 libaio-devel libaio-devel.i686 libXext libXext.i686 libXtst libXtst.i686 libX11 libX11.i686 libXau libXau.i686 libxcb libxcb.i686 libXi libXi.i686 make sysstat unixODBC unixODBC-devel readline libtermcap-devel bc compat-libstdc++ elfutils-libelf elfutils-libelf-devel fontconfig-devel libXi libXtst libXrender libXrender-devel libgcc librdmacm-devel libstdc++ libstdc++-devel net-tools nfs-utils python python-configshell python-rtslib python-six targetcli smartmontools

4.4、创建用户和组

/usr/sbin/groupadd oinstall
/usr/sbin/groupadd -g 502 dba
/usr/sbin/groupadd -g 503 oper
/usr/sbin/useradd -u 502 -g oinstall -G dba,oper oracle
su oracle

4.5、创建所需目录

mkdir -p /u01/app/oracle
chown -R oracle:oinstall /u01/app
chmod -R 775 /u01/app

注:这里我是在vsphere环境下测试的ADG、我添加了200G数据盘并挂载到 /u01 目录下;如果你和我一样、请提前挂载磁盘。

4.6、关闭防火墙和SeLinux

# 停止并禁用防火墙
[root@oracle11gdg01 ~]# systemctl stop firewalld
[root@oracle11gdg01 ~]# systemctl disable firewalld
[root@oracle11gdg01 ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)
[root@oracle11gdg01 ~]# 

# 禁用SeLinux
[root@oracle11gdg01 ~]# cat /etc/selinux/config 

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disable
# SELINUXTYPE= can take one of three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted 


[root@oracle11gdg01 ~]# setenforce 0

4.7、配置hosts

[oracle@oracle11gdg01 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
172.16.200.50   oracle11gdg01
172.16.200.51   oracle11gdg02

[oracle@oracle11gdg01 ~]# 

4.8、配置CentOS内核参数

/etc/security/limits.conf
#Oracle
oracle              soft    nproc   2047
oracle              hard    nproc   16384
oracle              soft    nofile  1024
oracle              hard    nofile  65536
oracle              soft    stack   10240 
# End of file

# 添加/修改下列内容:(注意!下面的参数,若是已经存在,则直接修改数值,不要再添加同样的参数,相同的参数只能有一个!如果需要修改的参数已经大于下面的数字,则不用修改,请仔细核对!)
/etc/sysctl.conf
fs.aio-max-nr = 1048576
fs.file-max = 6815744
kernel.shmall = 2097152
kernel.shmmax = 4294967295
kernel.shmmni = 4096
kernel.sem = 250 32000 100 128
net.ipv4.ip_local_port_range = 9000 65500
net.core.rmem_default = 262144
net.core.rmem_max = 4194304
net.core.wmem_default = 262144
net.core.wmem_max = 1048576
# /sbin/sysctl -p
# 注:保存退出后,别忘了用sysctl -p命令使参数生效!

4.9、安装Oracle11G

基础环境、环境变量和用户等相关操作完成以后、我们就可以开始安装 oracle11g 数据库软件了(主库和备库端都要做下列操作)。这里我把安装包上传到oracle用户的 /data 目录下面,解压之后开始安装:

[oracle@oracle11gdg01 data]$ ls
database
p13390677_112040_Linux-x86-64_1of7.zip
p13390677_112040_Linux-x86-64_2of7.zip
[oracle@oracle11gdg01 data]$ cd database/
[oracle@oracle11gdg01 database]$ ls
install      response  runInstaller  stage
readme.html  rpm       sshsetup      welcome.html
[oracle@oracle11gdg01 database]$ pwd
/home/oracle/data/database
[oracle@oracle11gdg01 database]$ 


[oracle@oracle11gdg01 database]$ ./runInstaller -jreLoc /etc/alternatives/jre_1.8.0
Starting Oracle Universal Installer...

安装过程:略

注意:主库在安装完软件后使用 netca 和 dbca 命令创建监听和建库、推荐不要启用归档模式可以会节省时间;备库端不要建库,我们使用 netca 命令开启监听程序。

5、配置DataGuard

5.1、启动归档模式(主库操作)

# 查看当前是否归档模式、如果不是请执行下面的命令启用归档模式
[oracle@oracle11gdg01 ~]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.4.0 Production on Tue Aug 17 10:26:23 2021

Copyright (c) 1982, 2013, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

# 查看当前是否归档模式
SQL> archive log list;
Database log mode              No Archive Mode
Automatic archival             Disabled
Archive destination            USE_DB_RECOVERY_FILE_DEST
Oldest online log sequence     4
Current log sequence           6
SQL> select log_mode from v$database;

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

SQL> 

# 关闭数据库
SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.

# 启动实例
SQL> startup mount;
ORACLE instance started.

Total System Global Area 3273641984 bytes
Fixed Size                  2257680 bytes
Variable Size            1912605936 bytes
Database Buffers         1342177280 bytes
Redo Buffers               16601088 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            USE_DB_RECOVERY_FILE_DEST
Oldest online log sequence     4
Next log sequence to archive   6
Current log sequence           6
SQL> 

5.2、配置强制日志模式(主库操作)

# 查看当前是否强制日志模式、如果不是请执行下面的命令启动强制日志模式
[oracle@oracle11gdg01 ~]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.4.0 Production on Tue Aug 17 10:24:35 2021

Copyright (c) 1982, 2013, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

# 查看当前是否强制日志模式
SQL> select name,log_mode,force_logging from v$database;

NAME      LOG_MODE     FOR
--------- ------------ ---
PRIMARY   NOARCHIVELOG NO

# 开启强制日志模式
SQL> alter database force logging;

Database altered.

# 验证是否开启强制日志模式
SQL> select name,log_mode,force_logging from v$database;

NAME      LOG_MODE     FOR
--------- ------------ ---
PRIMARY   NOARCHIVELOG YES

SQL> 

5.3、创建密码文件(主库操作)

[oracle@oracle11gdg01 ~]$ cd $ORACLE_HOME/dbs
[oracle@oracle11gdg01 dbs]$ ls
hc_primary.dat  hc_PRIMARY.dat  init.ora  lkPRIMARY  orapwprimary  spfileprimary.ora
[oracle@oracle11gdg01 dbs]$ 

# 这条命令可以手动生成密码文件,force=y的意思是强制覆盖当前已有的密码文件(如果有可以不建立)
orapwd file=orapwprimary password='oracle' force=y;


[oracle@oracle11gdg01 dbs]$ scp orapwprimary 172.16.200.51:$ORACLE_HOME/dbs/orapwstandby
oracle@172.16.200.51's password: 
orapwprimary                                                                                                               100% 1536   536.0KB/s   00:00    
[oracle@oracle11gdg01 dbs]$ 

[root@oracle11gdg02 ~]# cd $ORACLE_HOME/dbs
[root@oracle11gdg02 dbs]# ls
init.ora  orapwstandby
[root@oracle11gdg02 dbs]# 

注:这里我们直接把主库创建的密码文件传输到备库的指定目录;当然如果你不想拷贝也可以用上面的命令手动创建,前提是主库和备库的密码要一致。

5.4、创建standby redolog日志组(主库操作)

创建 standby redolog 日志组的时候、这里有几个要点要注意一下。

1:standby redo log 的文件大小与 primary 数据库 online redo log 文件大小相同;

2:standby redo log日志文件组的个数依照下面的原则进行计算:Standby redo log 组数公式 >= (每个instance日志组个数+1)instance个数;假如只有一个节点,这个节点有三组redolog,所以 Standby redo log 组数 >= (3+1)1 4;所以至少需要创建 4 组Standby redo log。

[oracle@oracle11gdg01 ~]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.4.0 Production on Tue Aug 17 10:37:54 2021

Copyright (c) 1982, 2013, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

# 查看当前线程与日志组的对应关系及日志组的大小
SQL> select thread#,group#,bytes/1024/1024 from v$log;

   THREAD#     GROUP# BYTES/1024/1024
---------- ---------- ---------------
         1          1              50
         1          2              50
         1          3              50

SQL> 

SQL> col member for a50;
SQL> select group#,member from v$logfile;

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

SQL> 

# 创建命令如下(这里我们还把redo日志创建到 /u01/app/oracle/oradata/primary/ 目录下面)
alter database add standby logfile group 4 '/u01/app/oracle/oradata/primary/redo04.log' size 50m;
alter database add standby logfile group 5 '/u01/app/oracle/oradata/primary/redo05.log' size 50m;
alter database add standby logfile group 6 '/u01/app/oracle/oradata/primary/redo06.log' size 50m;
alter database add standby logfile group 7 '/u01/app/oracle/oradata/primary/redo07.log' size 50m; 


[oracle@oracle11gdg01 primary]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.4.0 Production on Tue Aug 17 10:42:54 2021

Copyright (c) 1982, 2013, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

# 新建4个日志组作为standby redolog日志组,大小与原来的日志组一致;由于已经存在group1-3,,所以group号只能从4开始
SQL> alter database add standby logfile group 4 '/u01/app/oracle/oradata/primary/redo04.log' size 50m;
alter database add standby logfile group 5 '/u01/app/oracle/oradata/primary/redo05.log' size 50m;
alter database add standby logfile group 6 '/u01/app/oracle/oradata/primary/redo06.log' size 50m;
alter database add standby logfile group 7 '/u01/app/oracle/oradata/primary/redo07.log' size 50m; 

Database altered.

SQL> 

Database altered.

SQL> 
Database altered.

SQL> 

Database altered.

SQL>

# 查看standby 日志组的信息
SQL> select group#,sequence#,status, bytes/1024/1024 from v$standby_log;

    GROUP#  SEQUENCE# STATUS     BYTES/1024/1024
---------- ---------- ---------- ---------------
         4          0 UNASSIGNED              50
         5          0 UNASSIGNED              50
         6          0 UNASSIGNED              50
         7          0 UNASSIGNED              50

# 查看当前有哪些日志组及其成员
SQL> set pagesize 100
SQL> col member for a60
SQL> select group#,member from v$logfile order by group#;

    GROUP# MEMBER
---------- ------------------------------------------------------------
         1 /u01/app/oracle/oradata/primary/redo01.log
         2 /u01/app/oracle/oradata/primary/redo02.log
         3 /u01/app/oracle/oradata/primary/redo03.log
         4 /u01/app/oracle/oradata/primary/redo04.log
         5 /u01/app/oracle/oradata/primary/redo05.log
         6 /u01/app/oracle/oradata/primary/redo06.log
         7 /u01/app/oracle/oradata/primary/redo07.log

7 rows selected.

SQL> 

5.5、修改主库pfile参数文件(主库操作)

[oracle@oracle11gdg01 primary]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.4.0 Production on Tue Aug 17 10:44:10 2021

Copyright (c) 1982, 2013, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

# 查看spfile的路径
SQL> show parameter spfile;

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
spfile                               string      /u01/app/oracle/product/11.2.0
                                                 /db_1/dbs/spfileprimary.ora
# 用spfile创建一个pfile,用于修改
SQL> create pfile from spfile;

File created.

SQL> 

# 创建完成以后我们可以到 oracle 用户下的 /u01/app/oracle/product/11.2.0/db_1/dbs 目录进行查看
[oracle@oracle11gdg01 ~]$ cd $ORACLE_HOME/dbs
[oracle@oracle11gdg01 dbs]$ ls
hc_primary.dat  hc_PRIMARY.dat  init.ora  lkPRIMARY  orapwprimary  spfileprimary.ora
[oracle@oracle11gdg01 dbs]$ ls
hc_primary.dat  hc_PRIMARY.dat  init.ora  initprimary.ora  lkPRIMARY  orapwprimary  spfileprimary.ora
[oracle@oracle11gdg01 dbs]$ 

注:

主库pfile详细参数如下:

[root@oracle11gdg01 dbs]# pwd
/u01/app/oracle/product/11.2.0/db_1/dbs
[root@oracle11gdg01 dbs]# ls
hc_primary.dat  hc_PRIMARY.dat  init.ora  initprimary.ora  lkPRIMARY  orapwprimary  snapcf_primary.f  spfileprimary.ora
[root@oracle11gdg01 dbs]# cat initprimary.ora 
primary.__db_cache_size=1442840576
primary.__java_pool_size=33554432
primary.__large_pool_size=50331648
primary.__oracle_base='/u01/app/oracle'#ORACLE_BASE set from environment
primary.__pga_aggregate_target=1325400064
primary.__sga_target=1962934272
primary.__shared_io_pool_size=0
primary.__shared_pool_size=402653184
primary.__streams_pool_size=0
*.audit_file_dest='/u01/app/oracle/admin/primary/adump'    # 备库修改(确定有此目录)
*.audit_trail='db'
*.compatible='11.2.0.4.0'
*.control_files='/u01/app/oracle/oradata/primary/control01.ctl','/u01/app/oracle/fast_recovery_area/primary/control02.ctl'    # 备库修改(确定有此目录)
*.db_block_size=8192
*.db_domain=''
*.db_name='primary'         # 主库和备库的db_name必须一致,db_unique_name可以不一致
*.db_recovery_file_dest='/u01/app/oracle/fast_recovery_area'   # 备库修改(确定有此目录)
*.db_recovery_file_dest_size=4385144832
*.diagnostic_dest='/u01/app/oracle'
*.dispatchers='(PROTOCOL=TCP) (SERVICE=primaryXDB)'    # 备库修改
*.memory_target=3279945728
*.open_cursors=300
*.processes=300
*.remote_login_passwordfile='EXCLUSIVE'
*.sessions=335
*.undo_tablespace='UNDOTBS1'

# 以下内容备库按照实际修改
*.db_unique_name='primary'    # 主库和备库的db_name必须一致,db_unique_name可以不一致
*.fal_server='standby'        # 备份数据库的SID
*.fal_client='primary'        # 主数据库的SID
*.log_archive_max_processes=4
*.log_archive_config='DG_CONFIG=(primary,standby)'
# 第一个目录是备份数据库数据文件路径(备份服务器上有此目录),第二个是主数据库数据文件路径
*.db_file_name_convert='/u01/app/oracle/oradata/primary','/u01/app/oracle/oradata/standby' 
# 主数据库的归档日志路径和SID
*.log_archive_dest_1='LOCATION=/u01/app/oracle/product/11.2.0/db_1/archivelog VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=primary'
# 备份数据库的SID
*.log_archive_dest_2='SERVICE=standby LGWR SYNC AFFIRM VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=standby'
*.log_archive_dest_state_1='ENABLE'
*.log_archive_dest_state_2='ENABLE'
# 第一个目录是备份数据库数据文件路径(备份服务器上有此目录),第二个是主数据库数据文件路径
*.log_file_name_convert='/u01/app/oracle/oradata/standby','/u01/app/oracle/oradata/primary'
*.standby_file_management='AUTO'

修改完毕之后、保存退出。

5.6、重建主库spfile文件(主库操作)

然后我们用修改过的 pfile 重新创建一个 spfile,用于重启数据库(此步骤只在主库上做)

[oracle@oracle11gdg01 primary]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.4.0 Production on Tue Aug 17 11:14:09 2021

Copyright (c) 1982, 2013, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

# 关闭数据库
SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.

# 用修改过的pfile重新创建一个spfile
SQL> create spfile from pfile;

File created.

# 首先需要把数据库启动到mount状态
SQL> startup mount; 
ORACLE instance started.

Total System Global Area 3273641984 bytes
Fixed Size                  2257680 bytes
Variable Size            1912605936 bytes
Database Buffers         1342177280 bytes
Redo Buffers               16601088 bytes
Database mounted.

# OPEN数据库
SQL> alter database open; 

Database altered.

# 查看当前数据库是否使用spfile启动
SQL> show parameter spfile;

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
spfile                               string      /u01/app/oracle/product/11.2.0
                                                 /db_1/dbs/spfileprimary.ora
SQL> 

注:如上,若能看到spfile的路径,则证明数据库是使用spfile启动的,若没有值,则说明是用pfile启动的;这里一定要再次确定主库已经启用归档模式和强制日志模式。

5.7、配置监听文件(主备库操作)

修改主库监听文件:

[root@oracle11gdg01 admin]# pwd
/u01/app/oracle/product/11.2.0/db_1/network/admin
[root@oracle11gdg01 admin]# ls
listener.ora  samples  shrept.lst  tnsnames.ora
[root@oracle11gdg01 admin]# 
[oracle@oracle11gdg01 admin]$ cat listener.ora 
# listener.ora Network Configuration File: /u01/app/oracle/product/11.2.0/db_1/network/admin/listener.ora
# Generated by Oracle configuration tools.

LISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = TCP)(HOST = oracle11gdg01)(PORT = 1521))
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
    )
  )

SID_LIST_LISTENER = 
  (SID_LIST = 
    (SID_DESC = 
      (GLOBAL_DBNAME=primary)
      (SID_NAME = primary)
      (ORACLE_HOME = /u01/app/oracle/product/11.2.0/db_1)       
    ) 
  ) 

ADR_BASE_LISTENER = /u01/app/oracle

修改备库监听文件:

[root@oracle11gdg02 admin]# cat listener.ora 
# listener.ora Network Configuration File: /u01/app/oracle/product/11.2.0/db_1/network/admin/listener.ora
# Generated by Oracle configuration tools.

LISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = TCP)(HOST = oracle11gdg02)(PORT = 1521))
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
    )
  )

SID_LIST_LISTENER = 
  (SID_LIST = 
    (SID_DESC = 
      (GLOBAL_DBNAME=standby)
      (SID_NAME = standby)
      (ORACLE_HOME = /u01/app/oracle/product/11.2.0/db_1)       
    ) 
  ) 

ADR_BASE_LISTENER = /u01/app/oracle

注:这里的主备库监听文件一定要严格按照上面的配置内容来、除了 GLOBAL_DBNAME,SID_NAME 和 HOST 需要修改以为、其他的任何参数不要动。

配置完成以后我们用下面的命令分别启动主备库的监听,主库启动监听:

[oracle@oracle11gdg01 admin]$ lsnrctl stop

LSNRCTL for Linux: Version 11.2.0.4.0 - Production on 17-AUG-2021 11:24:07

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

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=oracle11gdg01)(PORT=1521)))
The command completed successfully
[oracle@oracle11gdg01 admin]$ lsnrctl start

LSNRCTL for Linux: Version 11.2.0.4.0 - Production on 17-AUG-2021 11:24:14

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

Starting /u01/app/oracle/product/11.2.0/db_1/bin/tnslsnr: please wait...

TNSLSNR for Linux: Version 11.2.0.4.0 - Production
System parameter file is /u01/app/oracle/product/11.2.0/db_1/network/admin/listener.ora
Log messages written to /u01/app/oracle/diag/tnslsnr/oracle11gdg01/listener/alert/log.xml
Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=oracle11gdg01)(PORT=1521)))
Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=oracle11gdg01)(PORT=1521)))
STATUS of the LISTENER
------------------------
Alias                     LISTENER
Version                   TNSLSNR for Linux: Version 11.2.0.4.0 - Production
Start Date                17-AUG-2021 11:24:14
Uptime                    0 days 0 hr. 0 min. 0 sec
Trace Level               off
Security                  ON: Local OS Authentication
SNMP                      OFF
Listener Parameter File   /u01/app/oracle/product/11.2.0/db_1/network/admin/listener.ora
Listener Log File         /u01/app/oracle/diag/tnslsnr/oracle11gdg01/listener/alert/log.xml
Listening Endpoints Summary...
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=oracle11gdg01)(PORT=1521)))
  (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))
Services Summary...
Service "primary" has 1 instance(s).
  Instance "primary", status UNKNOWN, has 1 handler(s) for this service...
The command completed successfully
[oracle@oracle11gdg01 admin]$ lsnrctl status

LSNRCTL for Linux: Version 11.2.0.4.0 - Production on 17-AUG-2021 11:24:25

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

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=oracle11gdg01)(PORT=1521)))
STATUS of the LISTENER
------------------------
Alias                     LISTENER
Version                   TNSLSNR for Linux: Version 11.2.0.4.0 - Production
Start Date                17-AUG-2021 11:24:14
Uptime                    0 days 0 hr. 0 min. 11 sec
Trace Level               off
Security                  ON: Local OS Authentication
SNMP                      OFF
Listener Parameter File   /u01/app/oracle/product/11.2.0/db_1/network/admin/listener.ora
Listener Log File         /u01/app/oracle/diag/tnslsnr/oracle11gdg01/listener/alert/log.xml
Listening Endpoints Summary...
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=oracle11gdg01)(PORT=1521)))
  (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))
Services Summary...
Service "primary" has 1 instance(s).
  Instance "primary", status UNKNOWN, has 1 handler(s) for this service...
The command completed successfully
[oracle@oracle11gdg01 admin]$ 

备库启动监听:

[root@oracle11gdg02 admin]# lsnrctl stop

LSNRCTL for Linux: Version 11.2.0.4.0 - Production on 17-AUG-2021 17:23:53

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

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=oracle11gdg02)(PORT=1521)))
The command completed successfully
[root@oracle11gdg02 admin]# lsnrctl start

LSNRCTL for Linux: Version 11.2.0.4.0 - Production on 17-AUG-2021 17:23:58

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

Starting /u01/app/oracle/product/11.2.0/db_1/bin/tnslsnr: please wait...

TNSLSNR for Linux: Version 11.2.0.4.0 - Production
System parameter file is /u01/app/oracle/product/11.2.0/db_1/network/admin/listener.ora
Log messages written to /u01/app/oracle/diag/tnslsnr/oracle11gdg02/listener/alert/log.xml
Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=oracle11gdg02)(PORT=1521)))
Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=oracle11gdg02)(PORT=1521)))
STATUS of the LISTENER
------------------------
Alias                     LISTENER
Version                   TNSLSNR for Linux: Version 11.2.0.4.0 - Production
Start Date                17-AUG-2021 17:23:58
Uptime                    0 days 0 hr. 0 min. 0 sec
Trace Level               off
Security                  ON: Local OS Authentication
SNMP                      OFF
Listener Parameter File   /u01/app/oracle/product/11.2.0/db_1/network/admin/listener.ora
Listener Log File         /u01/app/oracle/diag/tnslsnr/oracle11gdg02/listener/alert/log.xml
Listening Endpoints Summary...
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=oracle11gdg02)(PORT=1521)))
  (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))
Services Summary...
Service "standby" has 1 instance(s).
  Instance "standby", status UNKNOWN, has 1 handler(s) for this service...
The command completed successfully
[root@oracle11gdg02 admin]# lsnrctl status

LSNRCTL for Linux: Version 11.2.0.4.0 - Production on 17-AUG-2021 17:24:27

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

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=oracle11gdg02)(PORT=1521)))
STATUS of the LISTENER
------------------------
Alias                     LISTENER
Version                   TNSLSNR for Linux: Version 11.2.0.4.0 - Production
Start Date                17-AUG-2021 17:23:58
Uptime                    0 days 0 hr. 0 min. 28 sec
Trace Level               off
Security                  ON: Local OS Authentication
SNMP                      OFF
Listener Parameter File   /u01/app/oracle/product/11.2.0/db_1/network/admin/listener.ora
Listener Log File         /u01/app/oracle/diag/tnslsnr/oracle11gdg02/listener/alert/log.xml
Listening Endpoints Summary...
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=oracle11gdg02)(PORT=1521)))
  (DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))
Services Summary...
Service "standby" has 1 instance(s).
  Instance "standby", status UNKNOWN, has 1 handler(s) for this service...
The command completed successfully
[root@oracle11gdg02 admin]# 

注:分别在主库备库上看到 Service “standby” has 1 instance(s). Instance “standby”, status UNKNOWN, has 1 handler(s) for this service… The command completed successfully 的信息则代表监听启动成功。

5.8、配置TNS文件(主备库操作)

修改主库TNS文件:

[oracle@oracle11gdg01 admin]$ cat tnsnames.ora 
# tnsnames.ora Network Configuration File: /u01/app/oracle/product/11.2.0/db_1/network/admin/tnsnames.ora
# Generated by Oracle configuration tools.

PRIMARY =
  (DESCRIPTION =
    (ADDRESS_LIST = 
      (ADDRESS = (PROTOCOL = TCP)(HOST = 172.16.200.50)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = primary)
    )
  )

STANDBY =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 172.16.200.51)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = standby)
    )
  )
[oracle@oracle11gdg01 admin]$ 

修改备库TNS文件:

[root@oracle11gdg02 admin]# cat tnsnames.ora 
# tnsnames.ora Network Configuration File: /u01/app/oracle/product/11.2.0/db_1/network/admin/tnsnames.ora
# Generated by Oracle configuration tools.

PRIMARY =
  (DESCRIPTION =
    (ADDRESS_LIST = 
      (ADDRESS = (PROTOCOL = TCP)(HOST = 172.16.200.50)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = primary)
    )
  )

STANDBY =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 172.16.200.51)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = standby)
    )
  )
[root@oracle11gdg02 admin]# 

注:这里的HOST我采用了IP地址、当然也可以采用 hostname 。一定要保证主库和备库的 tnsnames.ora 文件中的内容完全相同,当然你也可以把修改后的文件用 scp 命令直接传给备库。

TNS配置完成以后我们使用下面的命令来验证主备节点的网络连通性:

[oracle@oracle11gdg01 admin]$ tnsping primary

TNS Ping Utility for Linux: Version 11.2.0.4.0 - Production on 17-AUG-2021 11:25:20

Copyright (c) 1997, 2013, Oracle.  All rights reserved.

Used parameter files:


Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 172.16.200.50)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = primary)))
OK (0 msec)
[oracle@oracle11gdg01 admin]$ tnsping standby

TNS Ping Utility for Linux: Version 11.2.0.4.0 - Production on 17-AUG-2021 11:25:26

Copyright (c) 1997, 2013, Oracle.  All rights reserved.

Used parameter files:


Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 172.16.200.51)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = standby)))
OK (0 msec)
[oracle@oracle11gdg01 admin]$ 
[root@oracle11gdg02 admin]# tnsping primary

TNS Ping Utility for Linux: Version 11.2.0.4.0 - Production on 17-AUG-2021 17:25:31

Copyright (c) 1997, 2013, Oracle.  All rights reserved.

Used parameter files:


Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 172.16.200.50)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = primary)))
OK (10 msec)
[root@oracle11gdg02 admin]# tnsping standby

TNS Ping Utility for Linux: Version 11.2.0.4.0 - Production on 17-AUG-2021 17:25:35

Copyright (c) 1997, 2013, Oracle.  All rights reserved.

Used parameter files:


Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 172.16.200.51)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = standby)))
OK (0 msec)
[root@oracle11gdg02 admin]# 

注:一定要能保证主备节点都能互相测试通过。

我们也可以使用 sqlplus 来验证一下主备节点是否可以正常访问:

[oracle@oracle11gdg02 admin]$ sqlplus sys/z48107660K@primary as sysdba

SQL*Plus: Release 11.2.0.4.0 Production on Tue Aug 17 17:26:51 2021

Copyright (c) 1982, 2013, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> exit
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
[oracle@oracle11gdg02 admin]$ sqlplus sys/z48107660K@standby as sysdba

SQL*Plus: Release 11.2.0.4.0 Production on Tue Aug 17 17:26:59 2021

Copyright (c) 1982, 2013, Oracle.  All rights reserved.

Connected to an idle instance.

SQL> exit
Disconnected
[oracle@oracle11gdg02 admin]$ 
[oracle@oracle11gdg01 admin]$ sqlplus sys/z48107660K@primary as sysdba

SQL*Plus: Release 11.2.0.4.0 Production on Tue Aug 17 11:27:36 2021

Copyright (c) 1982, 2013, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> exit
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
[oracle@oracle11gdg01 admin]$ sqlplus sys/z48107660K@standby as sysdba

SQL*Plus: Release 11.2.0.4.0 Production on Tue Aug 17 11:27:46 2021

Copyright (c) 1982, 2013, Oracle.  All rights reserved.

Connected to an idle instance.

SQL> exit
Disconnected
[oracle@oracle11gdg01 admin]$ 

注:如果测试的过程中有任何问题、请及时修正、不然DG不会正常启动。

5.9、修改备库pfile参数文件(备库操作)

在修改备库参数之前我们需要先来创建几个备库上会用到的目录(这里要根据实际情况一个个参数进行确认、缺一不可):

[oracle@oracle11gdg02 dbs]$ mkdir -p /u01/app/oracle/admin/standby/adump
[oracle@oracle11gdg02 dbs]$ mkdir -p /u01/app/oracle/oradata/standby/
[oracle@oracle11gdg02 dbs]$ mkdir -p /u01/app/oracle/fast_recovery_area/standby/
[oracle@oracle11gdg02 dbs]$ mkdir -p /u01/app/oracle/product/11.2.0/db_1/archivelog

然后我们在主库端把 pfile 拷贝给备库端的$ORACLE_HOME/dbs目录下,重命名并修改备库参数:

[root@oracle11gdg02 dbs]# pwd
/u01/app/oracle/product/11.2.0/db_1/dbs
[root@oracle11gdg02 dbs]# ls
hc_standby.dat  init.ora  initstandby.ora  lkPRIMARY  lkSTANDBY  orapwstandby  spfilestandby.ora
[root@oracle11gdg02 dbs]# 
primary.__db_cache_size=1442840576
primary.__java_pool_size=33554432
primary.__large_pool_size=50331648
primary.__oracle_base='/u01/app/oracle'#ORACLE_BASE set from environment
primary.__pga_aggregate_target=1325400064
primary.__sga_target=1962934272
primary.__shared_io_pool_size=0
primary.__shared_pool_size=402653184
primary.__streams_pool_size=0
*.audit_file_dest='/u01/app/oracle/admin/standby/adump'
*.audit_trail='db'
*.compatible='11.2.0.4.0'
*.control_files='/u01/app/oracle/oradata/standby/control01.ctl','/u01/app/oracle/fast_recovery_area/standby/control02.ctl'
*.db_block_size=8192
*.db_domain=''
*.db_name='primary'
*.db_recovery_file_dest='/u01/app/oracle/fast_recovery_area'
*.db_recovery_file_dest_size=4385144832
*.diagnostic_dest='/u01/app/oracle'
*.dispatchers='(PROTOCOL=TCP) (SERVICE=standbyXDB)'
*.memory_target=3279945728
*.open_cursors=300
*.processes=300
*.remote_login_passwordfile='EXCLUSIVE'
*.sessions=335
*.undo_tablespace='UNDOTBS1'

*.db_unique_name='standby'
*.fal_server='primary'
*.fal_client='standby'
*.log_archive_max_processes=4
*.log_archive_config='DG_CONFIG=(primary,standby)'
*.db_file_name_convert='/u01/app/oracle/oradata/standby','/u01/app/oracle/oradata/primary' 
*.log_archive_dest_1='LOCATION=/u01/app/oracle/product/11.2.0/db_1/archivelog VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=standby'
*.log_archive_dest_2='SERVICE=primary LGWR SYNC AFFIRM VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=primary'
*.log_archive_dest_state_1='ENABLE'
*.log_archive_dest_state_2='ENABLE'
*.log_file_name_convert='/u01/app/oracle/oradata/primary','/u01/app/oracle/oradata/standby'
*.standby_file_management='AUTO'

注意:整个搭建过程最需要留意的就是主库和备库的 pfile 配置,建议修改完后仔细对照主备库 pfile 的区别。

5.10、重建备库spfile文件(备库操作)

[oracle@oracle11gdg02 ~]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.4.0 Production on Tue Aug 17 19:20:06 2021

Copyright (c) 1982, 2013, Oracle.  All rights reserved.

Connected to an idle instance.

# 用修改后的pfile创建一个spfile,用于启动数据库
SQL> create spfile from pfile;

File created.

# 将数据库启动到nomount状态
SQL> startup nomount;
ORACLE instance started.

Total System Global Area 3273641984 bytes
Fixed Size                  2257680 bytes
Variable Size            1912605936 bytes
Database Buffers         1342177280 bytes
Redo Buffers               16601088 bytes
SQL> 

# 我们可以在 /u01/app/oracle/product/11.2.0/db_1/dbs 目录中看到上面生成的 spfile 文件
[oracle@oracle11gdg02 dbs]$ ls
init.ora  initstandby.ora  orapwstandby
[oracle@oracle11gdg02 dbs]$ ls
init.ora  initstandby.ora  orapwstandby  spfilestandby.ora
[oracle@oracle11gdg02 dbs]$ 

5.11、RMAN恢复数据到备库(备库操作)

现在我们就可以利用 RMAN 在备库上恢复主库了。

# 一定要加nocatalog,否则在执行duplicate时会报错
[oracle@oracle11gdg02 archivelog]$ rman target sys/z48107660K@primary auxiliary sys/z48107660K@standby nocatalog

Recovery Manager: Release 11.2.0.4.0 - Production on Tue Aug 17 20:09:08 2021

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

connected to target database: PRIMARY (DBID=1918297569)
using target database control file instead of recovery catalog
connected to auxiliary database: PRIMARY (not mounted)

# 使用duplicate复制数据库,复制内容包括standby控制文件和standby日志组
RMAN> duplicate target database for standby from active database nofilenamecheck;

# 详细过程如下(可忽略)
Starting Duplicate Db at 17-AUG-21
allocated channel: ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: SID=357 device type=DISK

contents of Memory Script:
{
   backup as copy reuse
   targetfile  '/u01/app/oracle/product/11.2.0/db_1/dbs/orapwprimary' auxiliary format 
 '/u01/app/oracle/product/11.2.0/db_1/dbs/orapwstandby'   ;
}
executing Memory Script

Starting backup at 17-AUG-21
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=184 device type=DISK
Finished backup at 17-AUG-21

contents of Memory Script:
{
   backup as copy current controlfile for standby auxiliary format  '/u01/app/oracle/oradata/standby/control01.ctl';
   restore clone controlfile to  '/u01/app/oracle/fast_recovery_area/standby/control02.ctl' from 
 '/u01/app/oracle/oradata/standby/control01.ctl';
}
executing Memory Script

Starting backup at 17-AUG-21
using channel ORA_DISK_1
channel ORA_DISK_1: starting datafile copy
copying standby control file
output file name=/u01/app/oracle/product/11.2.0/db_1/dbs/snapcf_primary.f tag=TAG20210817T140917 RECID=3 STAMP=1080828558
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:01
Finished backup at 17-AUG-21

Starting restore at 17-AUG-21
using channel ORA_AUX_DISK_1

channel ORA_AUX_DISK_1: copied control file copy
Finished restore at 17-AUG-21

contents of Memory Script:
{
   sql clone 'alter database mount standby database';
}
executing Memory Script

sql statement: alter database mount standby database

contents of Memory Script:
{
   set newname for tempfile  1 to 
 "/u01/app/oracle/oradata/primary/temp01.dbf";
   switch clone tempfile all;
   set newname for datafile  1 to 
 "/u01/app/oracle/oradata/primary/system01.dbf";
   set newname for datafile  2 to 
 "/u01/app/oracle/oradata/primary/sysaux01.dbf";
   set newname for datafile  3 to 
 "/u01/app/oracle/oradata/primary/undotbs01.dbf";
   set newname for datafile  4 to 
 "/u01/app/oracle/oradata/primary/users01.dbf";
   backup as copy reuse
   datafile  1 auxiliary format 
 "/u01/app/oracle/oradata/primary/system01.dbf"   datafile 
 2 auxiliary format 
 "/u01/app/oracle/oradata/primary/sysaux01.dbf"   datafile 
 3 auxiliary format 
 "/u01/app/oracle/oradata/primary/undotbs01.dbf"   datafile 
 4 auxiliary format 
 "/u01/app/oracle/oradata/primary/users01.dbf"   ;
   sql 'alter system archive log current';
}
executing Memory Script

executing command: SET NEWNAME

renamed tempfile 1 to /u01/app/oracle/oradata/primary/temp01.dbf in control file

executing command: SET NEWNAME

executing command: SET NEWNAME

executing command: SET NEWNAME

executing command: SET NEWNAME

Starting backup at 17-AUG-21
using channel ORA_DISK_1
channel ORA_DISK_1: starting datafile copy
input datafile file number=00001 name=/u01/app/oracle/oradata/primary/system01.dbf
output file name=/u01/app/oracle/oradata/primary/system01.dbf tag=TAG20210817T140928
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:07
channel ORA_DISK_1: starting datafile copy
input datafile file number=00002 name=/u01/app/oracle/oradata/primary/sysaux01.dbf
output file name=/u01/app/oracle/oradata/primary/sysaux01.dbf tag=TAG20210817T140928
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:07
channel ORA_DISK_1: starting datafile copy
input datafile file number=00003 name=/u01/app/oracle/oradata/primary/undotbs01.dbf
output file name=/u01/app/oracle/oradata/primary/undotbs01.dbf tag=TAG20210817T140928
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:01
channel ORA_DISK_1: starting datafile copy
input datafile file number=00004 name=/u01/app/oracle/oradata/primary/users01.dbf
output file name=/u01/app/oracle/oradata/primary/users01.dbf tag=TAG20210817T140928
channel ORA_DISK_1: datafile copy complete, elapsed time: 00:00:01
Finished backup at 17-AUG-21

sql statement: alter system archive log current

contents of Memory Script:
{
   switch clone datafile all;
}
executing Memory Script

datafile 1 switched to datafile copy
input datafile copy RECID=3 STAMP=1080850183 file name=/u01/app/oracle/oradata/primary/system01.dbf
datafile 2 switched to datafile copy
input datafile copy RECID=4 STAMP=1080850183 file name=/u01/app/oracle/oradata/primary/sysaux01.dbf
datafile 3 switched to datafile copy
input datafile copy RECID=5 STAMP=1080850183 file name=/u01/app/oracle/oradata/primary/undotbs01.dbf
datafile 4 switched to datafile copy
input datafile copy RECID=6 STAMP=1080850183 file name=/u01/app/oracle/oradata/primary/users01.dbf
Finished Duplicate Db at 17-AUG-21

RMAN> 

注:至此恢复数据库结束(这里我遇到一个小问题、第一次恢复我这边是失败的。经过查询我发现原因如下:上面我明明配置了备库的数据文件目录为 /u01/app/oracle/oradata/standby/ ,而且我也执行了 alter system set db_file_name_convert=’/u01/app/oracle/oradata/standby’,’/u01/app/oracle/oradata/primary’ scope=spfile; 命令;但是实际恢复的时候数据跑到了 /u01/app/oracle/oradata/primary/ 目录下面;这时我在备库创建了 /u01/app/oracle/oradata/primary/ 目录,然后重新尝试恢复才成功完成;有遇到这个问题的小伙伴可以和我联系一起探讨一下)。

5.12、启动备库

SQL> shutdown immediate;
ORA-01507: database not mounted

ORACLE instance shut down.
SQL> startup mount;
ORACLE instance started.

Total System Global Area 3273641984 bytes
Fixed Size                  2257680 bytes
Variable Size            1912605936 bytes
Database Buffers         1342177280 bytes
Redo Buffers               16601088 bytes
Database mounted.
SQL> select status from v$instance;

STATUS
------------
MOUNTED

# 备库启动日志应用(启用备库前确认归档日志是否都已拷贝)
# 停止日志应用的命令是:alter database recover managed standby database cancel;
SQL> alter database recover managed standby database disconnect from session;

Database altered.

SQL> 

至此整个配置过程完成,下面我们来验证一下主备数据库是否能正常同步数据。

6、主备库数据测试

我们可以先来看看主备数据库的状态:

# 查看主库状态
SQL> select name from v$tablespace;

NAME
------------------------------
SYSTEM
SYSAUX
UNDOTBS1
USERS
TEMP

SQL> select    open_mode,           --数据库打开模式,如果实时同步,则为:read only with apply,取消同步则为:read only
  2            database_role,       --数据库角色,是主库还是备库
  3            protection_mode,     --保护模式
  4            protection_level     --保护级别
  5  from  v$database;

OPEN_MODE            DATABASE_ROLE    PROTECTION_MODE      PROTECTION_LEVEL
-------------------- ---------------- -------------------- --------------------
READ WRITE           PRIMARY          MAXIMUM PERFORMANCE  MAXIMUM PERFORMANCE

SQL> 


# 查看备库状态
SQL> alter database recover managed standby database disconnect from session;

Database altered.

SQL> alter database open;


Database altered.

SQL> SQL> 
SQL> alter database recover managed standby database using current logfile disconnect from session;

Database altered.

SQL> select    open_mode,          
  2            database_role,      
  3            protection_mode,     
  4            protection_level     
  5  from  v$database;

OPEN_MODE            DATABASE_ROLE    PROTECTION_MODE      PROTECTION_LEVEL
-------------------- ---------------- -------------------- --------------------
READ ONLY WITH APPLY PHYSICAL STANDBY MAXIMUM PERFORMANCE  MAXIMUM PERFORMANCE

SQL> 

可以看到主备数据库状态正常、现在我们去主库创建一个 django 账号、然后在这个账号下面创建 几张数据表:

image-20210818204038099

image-20210818204051755

从上图可以看到、数据很快就同步到备库了。

7、补充操作

查看日志应用情况:

[oracle@oracle11gdg01 ~]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.4.0 Production on Wed Aug 18 14:33:20 2021

Copyright (c) 1982, 2013, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> set pagesize 100
SQL> select sequence#,applied from v$archived_log order by 1;

 SEQUENCE# APPLIED
---------- ---------
         6 NO
         7 NO
         8 NO
         8 YES
         9 NO
         9 YES
        10 YES
---------- ---------
        16 NO
        16 YES
        17 YES
        17 NO
        18 NO
        18 NO

23 rows selected.

SQL> 

注:如上,如果发现有 NO 也是正常的,说明该日志在主库上还没有归档,可以在主库上运行 alter system switch logfile; 命令来进行日志切换,再到备库查看日志应用情况。

查看主备库归档序号是否一致:

# 查看主库归档日志序号
[oracle@oracle11gdg01 ~]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.4.0 Production on Wed Aug 18 14:33:20 2021

Copyright (c) 1982, 2013, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> archive log list;
Database log mode              Archive Mode
Automatic archival             Enabled
Archive destination            /u01/app/oracle/product/11.2.0/db_1/archivelog
Oldest online log sequence     17
Next log sequence to archive   19
Current log sequence           19
SQL> 


# 查看备库归档日志序号
[oracle@oracle11gdg02 ~]$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.4.0 Production on Wed Aug 18 20:34:32 2021

Copyright (c) 1982, 2013, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> archive log list;
Database log mode              Archive Mode
Automatic archival             Enabled
Archive destination            /u01/app/oracle/product/11.2.0/db_1/archivelog
Oldest online log sequence     18
Next log sequence to archive   0
Current log sequence           19
SQL> 

从上面可以看到主备库归档日志序号结果完全一致。

查看standby启动的DG进程:

SQL> select process,client_process,sequence#,status from v$managed_standby; 

PROCESS   CLIENT_P  SEQUENCE# STATUS
--------- -------- ---------- ------------
ARCH      ARCH             16 CLOSING
ARCH      ARCH             18 CLOSING
ARCH      ARCH              0 CONNECTED
ARCH      ARCH             17 CLOSING
RFS       ARCH              0 IDLE
RFS       UNKNOWN           0 IDLE
RFS       UNKNOWN           0 IDLE
RFS       LGWR             19 IDLE
MRP0      N/A              19 APPLYING_LOG

9 rows selected.

SQL> 

查看数据库的保护模式:

# 主库
SQL> select database_role,protection_mode,protection_level,open_mode from v$database; 

DATABASE_ROLE    PROTECTION_MODE      PROTECTION_LEVEL     OPEN_MODE
---------------- -------------------- -------------------- --------------------
PRIMARY          MAXIMUM PERFORMANCE  MAXIMUM PERFORMANCE  READ WRITE

SQL> 

# 备库
SQL> select database_role,protection_mode,protection_level,open_mode from v$database; 

DATABASE_ROLE    PROTECTION_MODE      PROTECTION_LEVEL     OPEN_MODE
---------------- -------------------- -------------------- --------------------
PHYSICAL STANDBY MAXIMUM PERFORMANCE  MAXIMUM PERFORMANCE  READ ONLY WITH APPLY

SQL> 

查看DG的日志信息:

SQL> select * from v$dataguard_status;

FACILITY                 SEVERITY         DEST_ID MESSAGE_NUM ERROR_CODE CAL
------------------------ ------------- ---------- ----------- ---------- ---
TIMESTAMP
---------
MESSAGE
--------------------------------------------------------------------------------
Log Transport Services   Informational          0           1          0 NO
17-AUG-21
ARC0: Archival started

Log Transport Services   Informational          0           2          0 NO
17-AUG-21
ARC1: Archival started

FACILITY                 SEVERITY         DEST_ID MESSAGE_NUM ERROR_CODE CAL
------------------------ ------------- ---------- ----------- ---------- ---

推荐文章

发表评论

邮箱地址不会被公开。 必填项已用*标注