主备库内存不一致的Data Guard环境搭建
前言
前几天朋友问了我一个问题,一个单节点RAC ,要做一套 Data Guard ,但是备库能给数据库的内存只有主库的一半左右,能不能成功。不知道,没做过,试试呗。
一.概况
1. 涉及的技术点
1) RAC 作为 primary database , nonRAC 作为 standby database
2) 使用RMAN 作为数据库的备份方式
3) 使用Backup-based duplication方式 创建备库
4) 主库使用ASM 存储方式,备库使用 filesystem 作为存储
5) 使用standby logfile ,开启日志实时更新
2. 主备库信息表概要
Primary(RAC) | Standby(fs) | |
HOSTNAME | yukki | fuzhou |
ORACLE_SID | cs1 | stbcs1 |
DB_NAME | cs | cs |
DB_UNIQUE_NAME | cs | stby |
SERVICE_NAMES | cs_pri | cs_stb |
INSTANCE_NAME | cs1 | stbcs1 |
INSTANCE_NUMBER | 1 | 1 |
THREAD | 1 | 1 |
TEMPFILE_LOCATION | +DATA/cs/tempfile | /u01/db/oradata |
二. Primary 主库配置
1. 查看 Managed Standby 组件
SYS@ cs1>select * from v$option where lower(parameter)='managed standby';
PARAMETER VALUE
---------------------------------------------------------------- ---------------------------------------------------------------
Managed Standby TRUE
# 请确保该值为 true
2. 检查remote_login_passwordfile 的设置
SYS@ cs1>show parameter remote_login_passwordfile
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
remote_login_passwordfile string EXCLUSIVE
# 若该参数不为 exclusive ,则按照以下命令修改,并重启使其生效
SYS@ cs1>alter system set remote_login=exclusive scope=spfile;
3. 检查数据库是否为归档模式
SYS@ cs1>archive log list
Database log mode Archive Mode
Automatic archival Enabled
Archive destination +DATA
Oldest online log sequence 87
Next log sequence to archive 89
Current log sequence 89
# 若为非归档模式,则需要干净的关闭数据库后,启动到 mount 模式,修改为归档模式后再开库
SYS@ cs1>shutdown immediate
SYS@ cs1>startup mount
SYS@ cs1>alter database archivelog;
SYS@ cs1>alter database open;
SYS@ cs1>select log_mode from v$database;
4. 检查数据库是否开启force logging
SYS@ cs1>select name,force_logging from v$database;
NAME FOR
--------- ---
CS YES
# 若数据库未开启 force logging ,则
SYS@ cs1>alter database force logging;
SYS@ cs1>select name,force_logging from v$database;
SYS@ cs1>alter system archive log current;
5. 检查主库口令文件的MD5 值
[oracle@ yukki dbs]$ openssl md5 orapwcs1
MD5(orapwcs1)= 7836520c978614723e57330e12ccbe90
# 要确保主备库口令文件的 MD5 值相同,即使 sys 的密钥相同也不行
6. 主库参数修改
SYS@ cs1>alter system set db_unique_name=cs scope=spfile;
SYS@ cs1>alter system set log_archive_config='dg_config=(cs,stby)';
SYS@ cs1>alter system set log_archive_dest_1='location=+DATA valid_for=(all_logfiles,all_roles) db_unique_name=cs';
SYS@ cs1>alter system set log_archive_dest_2='service=dbstandby async valid_for= (online_logfiles,primary_roles ) db_unique_name=stby';
SYS@ cs1>alter system set log_archive_dest_state_1=enable;
SYS@ cs1>alter system set log_archive_dest_state_ 2 =enable;
SYS@ cs1>alter system set log_archive_max_processes=30;
SYS@ cs1>alter system set fal_server=dbstandby;
SYS@ cs1>alter system set standby_file_management= auto ;
SYS@ cs1>alter system set db_file_name_convert=' +DATA /cs/datafile, /u01/db/oradata' scope=spfile;
SYS@ cs1>alter system set log_file_name_convert=' +DATA /cs/onlinelog, /u01/db/oradata'scope=spfile;
SYS@ cs1>alter system set service_names=cs_pri;
三. Standby 备库配置
1. 准备standby 的口令文件
# 拷贝主库的口令文件传至备库的 $ORACLE_HOME/dbs 目录下,并重命名为 orapwstbcs1
[oracle@ yukki dbs]$ scp orapwcs1 oracle@ fuzhou :$ORACLE_HOME/dbs
[oracle@fuzhou dbs]$ mv orapwcs1 orapwstbcs1
# 检查备库口令文件的 MD5 值,确保和主库相同
[oracle@fuzhou dbs]$ openssl md5 orapwstbcs1
MD5(orapwstbcs1)= 7836520c978614723e57330e12ccbe90
2. 准备standby 的参数文件
在主库生成pfile ,并将其传至备库修改
SYS@ cs1>create pfile='/tmp/pfile2019110 1 ' from spfile;
[oracle@ yukki tmp]$ scp pfile2019110 1 oracle@fuzhou:/tmp /initstbcs1.ora
[oracle@fuzhou dbs]$ vi initstbcs1.ora
stbcs1._...
...
*.audit_file_dest='/u01/db/admin/cs/adump'
*.audit_trail='db'
*.compatible='11.2.0.4.0'
*.control_files='/u01/db/oradata/control01.ctl','/u01/db/oradata/control02.ctl'#Restore Controlfile
*.db_block_size=8192
*.db_create_file_dest='/u01/db/oradata'
*.db_domain=''
*.db_file_name_convert='+DATA/cs/datafile','/u01/db/oradata'
*.db_name='cs'
*.db_recovery_file_dest='/u01/db/fast_recovery_area'
*.db_recovery_file_dest_size=4385144832
*.db_unique_name='STBY'
*.diagnostic_dest='/u01/db'
*.dispatchers='(PROTOCOL=TCP) (SERVICE=stbcsXDB)'
*.enable_goldengate_replication=TRUE
*.fal_server='DBPRIMARY'
*.log_archive_config='DG_CONFIG=(STBY,CS)'
*.log_archive_dest_1='location=/u01/db/arch valid_for=(all_logfiles,all_roles) db_unique_name=stby'
*.log_archive_dest_2='service=dbprimary async valid_for= (online_logfiles,primary_roles ) db_unique_name=cs'
*.log_archive_dest_state_1='ENABLE'
*.log_archive_dest_state_2='ENABLE'
*.log_archive_format='%t_%s_%r.dbf'
*.log_archive_max_processes=30
*.log_file_name_convert='+DATA/cs/onlinelog','/u01/db/oradata'
*.open_cursors=300
*.pga_aggregate_target=109715200
*.processes=150
*.remote_login_passwordfile='exclusive'
*.service_names='CS_STB'
*.sga_target=329145600
*.standby_file_management='AUTO'
*.undo_tablespace='UNDOTBS1'
# 此处需要注意的是由于实验需求,备库参数文件里的 sga_target 和 pga_aggregate_target 需修改为主库的一半
# 在 11g 中取消的参数:
*.standby_archive_dest
*.fal_client
3. 创建必要的目录结构
[oracle@ fuzhou ~]$ mkdir -p /u01/db/admin/cs/adump
[oracle@ fuzhou ~]$ mkdir -p /u01/db/oradata
[oracle@ fuzhou ~]$ mkdir -p /u01/db/arch
[oracle@ fuzhou ~]$ mkdir -p /u01/db/fast_recovery_area
4. 创建spfile ,并启动 instance
[oracle@ fuzhou ~]$ export ORACLE_SID=stbcs1
[oracle@ fuzhou ~]$ sqlplus / as sysdba
SYS@ stbcs1>create spfile from pfile;
SYS@ stbcs1>startup nomount
SYS@ stbcs1>show parameter spfile
NAME TYPE VALUE
--------- -------- -------- -- ---------------------------------------------------- ------------------------
spfile string /u01/db/product/11204/dbhome_1/dbs/spfilestbcs1.ora
四. Backup-based duplication 复制 physical standby
1. listener.ora 配置
# 由于 standby 端只有 oracle 软件,实例无法启动到 mount 状态,此时 PMON 进程无法完成自动注册,故采用静态监听。
主库:
[grid@ yukki ~]$ cat /u01/11.2.0/grid/network/admin/listener.ora
LISTENER=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=IPC)(KEY=LISTENER)))) # line added by Agent
LISTENER_SCAN1=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=IPC)(KEY=LISTENER_SCAN1)))) # line added by Agent
ENABLE_GLOBAL_DYNAMIC_ENDPOINT_LISTENER_SCAN1=ON # line added by Agent
ENABLE_GLOBAL_DYNAMIC_ENDPOINT_LISTENER=ON # line added by Agent
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(GLOBAL_DBNAME= cs_pri)
(ORACLE_HOME = /u01/db/product/11204/dbhome_1)
(SID_NAME =cs1)
)
)
备库:
[oracle@ fuzhou ~]$ cat /u01/db/product/11204/dbhome_1/network/admin/listener.ora
# listener.ora Network Configuration File: /u01/db/product/11204/dbhome_1/network/admin/listener.ora
# Generated by Oracle configuration tools.
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.3.66)(PORT = 1521))
)
)
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(GLOBAL_DBNAME= cs_stb)
(ORACLE_HOME = /u01/db/product/11204/dbhome_1)
(SID_NAME =stbcs1)
)
)
2. tnsnames.ora 配置
往主备库的$ORACLE_HOME/network/admin/tnsnames.ora 中添加:
dbprimary =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.3.88)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = cs_pri)
)
)
dbstandby =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.3.66)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = cs_stb)
)
)
3. 备份primary 数据库
1) 查看数据库物理结构
[oracle@ yukki ~]$ rman target /
Recovery Manager: Release 11.2.0.4.0 - Production on Mon Nov 4 17:40:28 2019
Copyright (c) 1982, 2011, Oracle and/or its affiliates. All rights reserved.
connected to target database: CS (DBID=1434125244)
RMAN> report schema;
using target database control file instead of recovery catalog
Report of database schema for database with db_unique_name CS
List of Permanent Datafiles
===========================
File Size(MB) Tablespace RB segs Datafile Name
---- -------- ------------ ------- ------------------------ -------------------------------------------------------------
1 750 SYSTEM *** +DATA/cs/datafile/system.256.1018198953
2 580 SYSAUX *** +DATA/cs/datafile/sysaux.257.1018198953
3 75 UNDOTBS1 *** +DATA/cs/datafile/undotbs1.258.1018198953
4 5 USERS *** +DATA/cs/datafile/users.259.1018198953
5 50 TEST *** +DATA/cs/datafile/test.dbf
List of Temporary Files
=======================
File Size(MB) Tablespace Maxsize(MB) Tempfile Name
---- -------- ------------ ----------- -------------------- ---------------------------------------------------------------
1 29 TEMP 32767 +DATA/cs/tempfile/temp.268.1018199043
2) 备份数据库和控制文件
run{
sql 'alter system archive log current';
allocate channel c1 device type disk;
allocate channel c2 device type disk;
allocate channel c3 device type disk;
backup database filesperset 1 format '/backup/whole_%d_%U_%t.bus';
backup current controlfile for standby format '/backup/ctl_%d_%U_%t.bus';
release channel c1;
release channel c2;
release channel c3;
}
3) 备份归档日志
run{
sql 'alter system archive log current';
allocate channel c1 device type disk;
allocate channel c2 device type disk;
backup archivelog all format '/backup/ arch _%d_%U_%t.bus';
release channel c1;
release channel c2;
}
4) 将备份传至备库机
[oracle@ yukki ~]$ scp /backup/* oracle@fuzhou:/backup
4. 使用duplicate 进行数据库恢复
1) 创建脚本
[oracle@ yukki ~]$ vi duplicate.sh
connect target sys/oracle@dbprimary
connect auxiliary sys/oracle@dbstandby
run{
allocate channel c1 device type disk;
allocate channel c2 device type disk;
allocate channel c3 device type disk;
allocate auxiliary channel aux1 device type disk;
allocate auxiliary channel aux2 device type disk;
allocate auxiliary channel aux3 device type disk;
set until sequence=87 thread=1;
set newname for tempfile 1 to '/u01/db/oradata/temp01.dbf';
duplicate target database for standby nofilenamecheck dorecover;
release channel aux1;
release channel aux2;
release channel aux3;
release channel c1;
release channel c2;
release channel c3;
}
# 由于没有 temp_file_name_convert 这个参数,故在 duplicate 前需要给 tempfile set newname 操作
# 手动分配复制通道时,必须要加上 allocate auxiliary channel ,否则会提示 :
RMAN-05503: at least one auxiliary channel must be allocated to execute this command
#如果duplicate的时候使用关键词from active database(通过网络直传不落地的active database duplication方式,不需要主库的备份,节省了磁盘空间和传输备份的时间,但在复制的过程中对主库有一定压力,需要一定的网络带宽),则必须为主库分配通道,否则会提示:
RMAN-06034: at least 1 channel must be allocated to execute this command
2) 使用nohup 调用脚本,使其在后台运行
[oracle@ yukki ~]$ nohup rman cmdfile=duplicate.sh >duplicate.log &
5. 启动physical standby
SYS@ stbcs1>shutdown immediate;
SYS@ stbcs1>startup;
SYS@ stbcs1>recover managed standby database disconnect from session;
SYS@ stbcs1>select name,open_mode,database_role,protection_mode,switchover_status,controlfile_type from v$database;
NAME OPEN_MODE DATABASE_ROLE PROTECTION_MODE SWITCHOVER_STATUS CONTROL
------ -- ---------------- -- ------------- - ------ ------------ - ------- - - -------- -------------------------------------- ------------------------------- -------------
CS READ ONLY WITH APPLY PHYSICAL STANDBY MAXIMUM PERFORMANCE NOT ALLOWED STANDBY
五. DATAGUARD 使用 standby logfile
1. standby logfile 创建要求
# 确保主备库的日志文件大小相同,建议备库的 standby logfile 要比主库的 redo logfile 多一组,目的是确保备库随时都有一组空闲日志可使用。
# 当使用 rman 生成 controlfile for standby 的备份时, alert 日志中会有相关的提示信息,如下:
Clearing standby activation ID 1434109882 (0x557ac7ba)
The primary database controlfile was created using the
'MAXLOGFILES 192' clause.
There is space for up to 189 standby redo logfiles
Use the following SQL commands on the standby database to create
standby redo logfiles that match the primary database:
ALTER DATABASE ADD STANDBY LOGFILE 'srl1.f' SIZE 52428800;
ALTER DATABASE ADD STANDBY LOGFILE 'srl2.f' SIZE 52428800;
ALTER DATABASE ADD STANDBY LOGFILE 'srl3.f' SIZE 52428800;
ALTER DATABASE ADD STANDBY LOGFILE 'srl4.f' SIZE 52428800;
WARNING: OMF is enabled on this database. Creating a physical
standby controlfile, when OMF is enabled on the primary
database, requires manual RMAN intervention to resolve OMF
datafile pathnames.
NOTE: Please refer to the RMAN documentation for procedures
describing how to manually resolve OMF datafile pathnames.
2. 备库添加standby logfile
# 首先查看主库 online redo logfiles 的信息
SYS@ cs1>select group#,thread#,bytes from v$log;
GROUP# THREAD# BYTES
---------- ---------- ----------
1 1 52428800
2 1 52428800
3 1 52428800
# 确保主库 ORLs 日志组大小相同,再配置 SRLs ,且在备库添加 standby logfile 时,要先停掉 MRP 进程:
SYS@ stbcs1> recover managed standby database cancel;
SYS@ stbcs1>alter database add standby logfile thread 1 group 11 '/u01/db/oradata/stb_redo01.log' size 52428800;
SYS@ stbcs1>alter database add standby logfile thread 1 group 12 '/u01/db/oradata/stb_redo0 2 .log' size 52428800;
SYS@ stbcs1>alter database add standby logfile thread 1 group 13 '/u01/db/oradata/stb_redo0 3 .log' size 52428800;
SYS@ stbcs1>alter database add standby logfile thread 1 group 14 '/u01/db/oradata/stb_redo0 4 .log' size 52428800;
# 由于主库有三组 ORLs ,在创建 SRLs 的时候若不指定组数,默认会是 4-7 ,那么后续在主库添加日志组的话就会产生混乱,故从第 11 组开始配置 standby redo logfiles 。
# 还有就是当主库多实例的时候,备库也要配置上多个 thread ,目的是为了能开启 real time apply ,但是如果备库只创建了 thread 1 ,并不会影响 archive log 的传输和应用,但是备库并不会采用 real time apply ,主库 online redo 无法做到实时传输应用,只在归档切换后备库才会应用。
3. 主库添加standby logfile
SYS@ cs1>alter database add standby logfile thread 1 group 1 1 '+DATA/cs/onlinelog/stb y_ redo01.log' size 52428800;
SYS@ cs1>alter database add standby logfile thread 1 group 1 2 '+DATA/cs/onlinelog/stb y_ redo0 2 .log' size 52428800;
SYS@ cs1>alter database add standby logfile thread 1 group 1 3 '+DATA/cs/onlinelog/stb y_ redo0 3 .log' size 52428800;
SYS@ cs1>alter database add standby logfile thread 1 group 1 4 '+DATA/cs/onlinelog/stb y_ redo0 4 .log' size 52428800;
# 在配置备库的 standby logfile 的时候,也需要在主库上预配置,目的是用于未来切换使用。
六. 部分参数说明
1. db_name
数据库名称,一套Data Guard 环境中,需要保持主备库的 db_name 相同。
2. db_unique_name
DG 环境中用于区分主备库的唯一名字,即使主备库角色互换, db_unique_name 也不会更改。
3. log_archive_config
该参数通过dg_configs 设置同一个 Data Guard 环境中的所有 db_unique_name ,以逗号分隔,定义该参数能确保主备数据库能够发送或接收日志。
4. log_archive_dest_1
通过location 设置日志归档的本地路径,主备库需要定义各自的 Online Redo Log 的归档地址。本例 log_archive_dest_1='location=+DATA valid_for=(all_logfiles,all_roles) db_unique_name=cs' ,可以理解为对主库( cs )而言,不管她是主库还是备库( all_roles ),她都会自己完成归档动作,并将日志归档于本地路径 +DATA 下。
5. log_archive_dest_2
该参数仅当数据库角色为primary 时生效,指定 primary 传输 redo log 到该参数定义的 standby database 上,其中 service 的设置为 tnsnames.ora 中定义的 Oracle Net 名称。 log_archive_dest_2 可以说是 dataguard 上最重要的参数之一,它定义了 redo log 的传输方式 (sync or async) 以及传输目标 ( 即 standby apply node) ,直接决定了 dataguard 的数据保护级别。
6. fal_server
fal 即 fatch archive log ,其值为 tnsnames.ora 中远端数据库服务的 Oracle Net 名称, fal_server 为备库中设置的参数,一旦备库产生 gap ,会通过 fal_server 参数向主库请求传输缺失的日志,当然为了 switchover ,主库上也要预配置该参数。
7. db_file_name_convert
定义主备库的数据文件路径转换,远端在前,本地端在后。若有多个,逐一指明对映关系。
8. log_file_name_convert
定义主备库在线日志文件路径转换,远端在前,本地端在后。若有多个,逐一指明对映关系。
9. standby_file_management
备库参数,用来控制是否主动将主库增加表空间或数据文件的改动,传播到物理备库。
auto :主库执行的表空间创建操作会被传播到物理备库上执行。
manual : default ,需要手工复制新创建的数据文件到物理备库服务器。
10. service_name ( tnsnames.ora 中的参数)
service_name 是在多实例出现后,为了方便应用连接数据库提出的参数,该参数直接对应数据库而不是某个实例,故该参数与 sid 没有直接关系,不必与 sid 一样。当服务器端 listener.ora 中配置了静态监听后,客户端 tnsnames.ora 中 service_name 与服务器端静态监听中的 GLOBAL_DBNAME 相对应,且可不必与服务器端数据库中 service_names 参数对应。但若没有配置静态监听,客户端 tnsnames.ora 里的 service_name 需要从服务器端数据库中的 service_names 中取值。
以上,主备库内存不一致,可以搭建Data Guard 环境。