(一)基础环境
主库 | 备库 | |
操作系统 | RedHat6.7 | RedHat6.7 |
服务器名称 | primarydb1 primarydb2 |
standbydb1 standbydb2 |
IP地址规划 |
192.168.10.31 primarydb1 10.10.10.31 primarydb1-priv |
192.168.10.41 standbydb1 10.10.10.41 standbydb1-priv |
------------------------ | ------------------------------ | --------------------------------------- |
数据库版本 | 11.2.0.4 | 11.2.0.4 |
db_name | testdb | testdb |
db_unique_name | testdb | testdbdg |
instance_name | testdb1 testdb2 |
testdbdg1 testdbdg2 |
service_name | testdb | testdbdg |
数据库安装情况 | 安装GI+数据库软件+创建数据库 | 安装GI+数据库软件 (不用创建数据库) |
在配置dataguard之前,主库已经安装了GI+数据库软件+创建了数据库,备库也已经安装了GI+数据库软件,备库不需要使用dbca建库。
(二)主库配置
(2.1)配置归档,主库需要运行在归档模式下
--查看是否开启归档 archive log list
如果没有开启归档,需要开启,方法如下:
(2.2)主库启用强制记录日志
(2.3)主库网络配置
(2.3.1)静态监听配置
主库可以使用动态监听,也可以使用静态监听,这里新创建一个监听LISTENER_ADG,监听新的端口1522。以下操作使用grid用户执行
step1:在节点1执行添加监听命令
srvctl add listener -l LISTENER_ADG -o $ORACLE_HOME -p "TCP:1522"
step2:启动监听
srvctl start listener -l LISTENER_ADG
step3:修改监听配置文件,加入静态注册信息,2个节点都需要修改,这里以节点1为例
[grid@primarydb1 ~]$ vim /u01/app/11.2.0/grid/network/admin/listener.ora
LISTENER_ADG=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=IPC)(KEY=LISTENER_ADG)))) # line added by Agent 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 ENABLE_GLOBAL_DYNAMIC_ENDPOINT_LISTENER_ADG=ON # line added by Agent #添加以下信息 SID_LIST_LISTENER_ADG = (SID_LIST = (SID_DESC = (GLOBAL_DBNAME = testdb) (ORACLE_HOME = /u01/app/11.2.0/grid) #填写的是grid_home (SID_NAME=testdb1) #如果是节点2,需改为SID_NAME=testdb2 ) )
step4:重启监听,一个节点执行
srvctl stop listener -l LISTENER_ADG
srvctl start listener -l LISTENER_ADG
(2.3.2)tns配置,主库2个节点都添加
[oracle@primarydb1 ~]$ cd /u01/app/oracle/product/0/db_1/network/admin/ [oracle@primarydb1 admin]$ ls samples shrept.lst tnsnames.ora [oracle@primarydb1 admin]$ vim tnsnames.ora
tnstestdb = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.10.33)(PORT = 1522)) (ADDRESS = (PROTOCOL = TCP)(HOST = 10.34)(PORT = ))
(CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = testdb) ) ) tnstestdbdg = (DESCRIPTION =10.43)(PORT = ))
(ADDRESS = (PROTOCOL = TCP)(HOST = 10.44)(PORT = )) (CONNECT_DATA = testdbdg) ) )
(2.4)主库参数配置
因为有的参数重启才会生效(scope=spfile),因此改完参数需要重启
[grid@primarydb1 ~]$ srvctl stop database -d testdb
[grid@primarydb1 ~]$ srvctl start database -d testdb
(2.5)添加standby online redo log
standby redo log的大小与redo log大小相同。组数为比在线日志多一组。
查看在线日志组数信息
sql> select a."GROUP#",a."THREAD#",a."BYTES",a."MEMBERS" from v$ a,v$logfile b where a."GROUP#" = b."#" order by group#; # THREAD# BYTES MEMBERS -------- ---------- ---------- ---------- 1 1 52428800 1 2 3 2 4 1
可以看到,每个线程(可以理解为每个实例)有2组日志文件,每组有1个日志文件。因此我们在创建standby redo log时需要为每个thread创建3组日志文件。
(2.6)将主库的密码文件拷贝到备库
拷贝主库密码文件到备库,在节点1执行
[oracle@primarydb1 dbs]$ pwd /u01/app/oracle/product/0/db_1/dbs # 拷贝密码文件到备库节点1 [oracle@primarydb1 dbs]$ scp orapwtestdb1 oracle@10.41:`pwd` # 拷贝密码文件到备库节点2 [oracle@primarydb1 dbs]$ 10.42:`pwd`
(2.7)将主库的参数文件拷贝到备库
sql> create pfile='/home/oracle/pfile_20190818 from spfile; sql> exit Disconnected from Oracle Database 11g Enterprise Edition Release 0.4.0 - 64bit Production With the Partitioning,Real Application Clusters,Automatic Storage Management,OLAP,Data Mining and Real Application Testing options [oracle@primarydb1 ~]$ scp pfile_20190818 oracle@` oracle@10.41s password: pfile_20190818 100% 1859 1.8KB/s 00:00
(三)备库配置
linux环境中,密码文件命名格式为:orapwd{$sid},因此需要修改密码文件的名字
备库节点1:
[oracle@standbydb1 dbs]$ dbs [oracle@standbydb1 dbs]$ mv orapwtestdb1 orapwtestdbdg1
备库节点2:
[oracle@standbydb2 dbs]$ dbs [oracle@standbydb2 dbs]$ mv orapwtestdb1 orapwtestdbdg2
最终参数文件如下:
*.audit_file_dest=/u01/app/oracle/admin/testdbdg/adump' *.audit_trail=db' *.cluster_database=true *.compatible=11.2.0.4.0' *.control_files=+DATA/testdb/controlfile/current.266.1016639457' *.db_block_size=8192 *.db_create_file_dest=+DATA' *.db_domain='' *.db_file_name_convert=+DATA/testdb+DATA/testdbdg' *.db_name=testdb' *.db_unique_name=testdbdg' *.diagnostic_dest=/u01/app/oracle' *.fal_client=tnstestdbdg' *.fal_server=tnstestdb testdbdg1.instance_number=1 testdbdg2.instance_number=2 *.log_archive_config=' *.log_archive_dest_1=LOCATION=+ARCH' *.log_archive_dest_2=SERVICE=tnstestdb LGWR SYNC AFFIRM VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=testdb' *.log_archive_dest_state_1=enable' *.log_archive_dest_state_2=' *.log_file_name_convert=' *.remote_login_passwordfile=exclusive' *.standby_file_management=AUTO testdb1.thread= testdb2.thread=2 testdb1.undo_tablespace=UNDOTBS2 testdb2.undo_tablespace=UNDOTBS1'
里面涉及到的路径需要手动创建
(3.3)备库网络配置
(3.3.1)静态监听配置
这里新创建一个监听LISTENER_ADG,监听新的端口1522。以下操作使用grid用户执行
step1:在节点1执行添加监听命令
srvctl add listener -l LISTENER_ADG -o $ORACLE_HOME -p "TCP:1522"
step2:启动监听
srvctl start listener -l LISTENER_ADG
step3:修改监听配置文件,加入静态注册信息,2个节点都需要修改,这里以节点1为例
[grid@standbydb1 ~]$ vim /u01/app/0/grid/network/admin/listener.ora LISTENER_ADG=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=IPC)(KEY=ON # line added by Agent SID_LIST_LISTENER_ADG = testdbdg) (ORACLE_HOME = /u01/app/grid) (SID_NAME=testdbdg1) ) )
step4:重启监听,一个节点执行
(3.3.2)tns配置,备库2个节点都添加
# 在tnsnames.ora文件中添加如下内容
tnstestdb = testdbdg)
)
)
(3.4)启动备库到nomount状态
[oracle@standbydb1 ~]$ sqlplus / as sysdba sql*Plus: Release 0 Production on Mon Aug 19 04:14:04 2019 Copyright (c) 1982,1)">2013,Oracle. All rights reserved. Connected to an idle instance. sql> startup nomount pfile='; ORACLE instance started. Total System Global Area 313196544 bytes Fixed Size 2252744 bytes Variable Size 255852600 bytes Database Buffers 50331648 bytes Redo Buffers 4759552 bytes sql>
注意:再将备库启动到nomount状态后,理论上,可以从主库通过sqlplus连接到备库,同样也可以在备库通过sqlplus连接到主库,这里建议测试主库备库之间网络的连通性。在主库与备库上执行
确保所有命令都能执行成功。
在节点1的oracle用户下执行:
[oracle@standbydb1 db_1]$ srvctl add database -d testdbdg -o /u01/app/oracle/product/db_1 [oracle@standbydb1 db_1]$ srvctl add instance -d testdbdg -i testdbdg1 -n standbydb1 [oracle@standbydb1 db_1]$ srvctl add instance -d testdbdg -i testdbdg2 -n standbydb2
(四)主库备份
rman target / RMAN> run { allocate channel c1 type disk; allocate channel c2 type disk; sql alter system archive log current; backup database format /databaseBackup/full_db_%U; sql; backup archivelog all format /databaseBackup/archlog_%U; backup current controlfile format /databaseBackup/controlfile_%U; backup spfile format /databaseBackup/spfile_%U; release channel c1; release channel c2; }
将备份传到备库服务器:
[oracle@primarydb1 /]$ cd databaseBackup/ [oracle@primarydb1 databaseBackup]$ archlog_0au9imjq_1_1 archlog_0cu9imnk_1_1 full_db_06u9imbr_1_1 full_db_08u9imhs_1_1 spfile_0gu9imtt_1_1 archlog_0bu9imju_1_1 controlfile_0du9imrd_1_1 full_db_07u9imbr_1_1 full_db_09u9imhv_1_1 [oracle@primarydb1 databaseBackup]$ scp * oracle@10.41:/databaseBackup/
(五)备库还原
(5.1)restore控制文件
[oracle@standbydb1 ~]$ rman target / Recovery Manager: Release 0 - Production on Mon Aug 06:47:00 2011,Oracle and/or its affiliates. All rights reserved. connected to target database: TESTDB (not mounted) RMAN> restore standby controlfile from /databaseBackup/controlfile_0du9imrd_1_1'; Starting restore at 19-AUG-19 using target database control file instead of recovery catalog allocated channel: ORA_DISK_1 channel ORA_DISK_1: SID=292 instance=testdbdg1 device type=DISK channel ORA_DISK_1: restoring control channel ORA_DISK_1: restore complete,elapsed time: 08 output file name=+DATA/testdbdg/controlfile/current.268.1016664445 Finished restore at 19
解决方法:https://jingyan.baidu.com/album/9faa7231c13269473c28cb33.html?picindex=9
(5.2)修复数据库
在启动到mount状态时报了2个错误:
报错1:初始化参数文件找不到
解决方法:复制pfile到给定位置
[oracle@standbydb1 bin]$ cp /home/oracle/pfile_20190818 /u01/app/oracle/product/11.2.0/db_1/dbs/inittestdbdg1.ora
报错2:控制文件找不到
解决方法:
1.通过asmcmd找到控制文件的位置;
ASMCMD> pwd +data/testdbdg/CONTROLFILE ASMCMD> current.256.1016666437
[oracle@standbydb1 ~]$ vim /u01/app/oracle/product/0/db_1/dbs/inittestdbdg1.ora ... *.control_files='+data/testdbdg/CONTROLFILE/current.256.1016666437'
...
重新启动数据库到mount状态
[oracle@standbydb1 dbs]$ sqlplus /07:08:15 sql> startup mount; ORACLE instance started. Total System Global Area bytes Database mounted.
开始修复数据库
RMAN> restore database; Starting restore at 14 instance=testdbdg1 device type=DISK channel ORA_DISK_1: starting datafile backup set restore channel ORA_DISK_1: specifying datafile(s) to restore from backup set channel ORA_DISK_1: restoring datafile 00001 to +DATA/testdbdg/datafile/system.268.1016639305 channel ORA_DISK_1: restoring datafile 00004 to +DATA/testdbdg/datafile/users.267.1016639305 channel ORA_DISK_1: reading from backup piece /databaseBackup/full_db_06u9imbr_1_1 channel ORA_DISK_1: piece handle=/databaseBackup/full_db_06u9imbr_1_1 tag=TAG20190819T035434 channel ORA_DISK_1: restored backup piece channel ORA_DISK_1: starting datafile backup set restore channel ORA_DISK_1: specifying datafile(s) to restore from backup set channel ORA_DISK_1: restoring datafile 00002 to +DATA/testdbdg/datafile/sysaux.260.101663930500003 to +DATA/testdbdg/datafile/undotbs1.263.101663930500005 to +DATA/testdbdg/datafile/undotbs2.264.1016639859full_db_07u9imbr_1_1 channel ORA_DISK_1: piece handle=/databaseBackup/full_db_07u9imbr_1_1 tag=5619
(六)打开备库
打开备库
sql> alter database open ; Database altered.
查看数据库状态:
sql> select name,db_unique_name,open_mode from v$database; NAME DB_UNIQUE_NAME OPEN_MODE --------- ------------------------------ -------------------- TESTDB testdbdg READ ONLY
开始数据库实时日志应用,在开启之前,需要确认standby redo logfile是否已经创建,这里已经从主库恢复过来,故不需要创建
在执行该语句后,数据库会自动去同步主库新生成的日志,可以打开主库与备库的告警日志,查看数据库同步状态。
(七)启动另外一个节点
(7.1)首先使用pfile生成spfile
(7.2)启动节点
首先查看数据库状态:
[grid@standbydb1 ~]$ crsctl status res -t -------------------------------------------------------------------------------- ... ora.testdbdg.db OFFLINE OFFLINE Instance Shutdown OFFLINE OFFLINE ...
打开节点1:
打开节点2:
再次确认数据库的状态,已经正常启动:
主库:
备库:
(八)测试数据同步情况
(1)主库创建表,插入数据:
sql> select * from test02;
ID NAME
---------- --------------------
1 lijiaman
备库查看:
数据已同步。
(2)主库删除表,表进入了回收站,11gR2的dataguard不需要关闭回收站。
发现备库表也进入了回收站
同样,主库执行闪回删除,将sales表从回收站恢复回来,备库也会执行相同的操作。
数据同步测试无异常。
(九)存在的问题
存在的问题1:备库使用scan-ip无法连接:
配置信息:ip192.168.10.40 port:1521 service:testdbdg
需要设置初始化参数:remote_listener
【完】