Oracle Active Data Guard搭建
一:安装
1.基础环境配置
1.1.开启强制日志记录
- DG日志发送方式中ARCH进程和LGWR进程的ASYNC模式都是基于日志同步的,所以我们必须强制将数据库的所有操作记录到日志中
RAC2:关闭库
SQL> shutdown
Database closed.
Database dismounted.
ORACLE instance shut down.
RAC1:开启强制日志记录
SQL> alter database force logging;
Database altered.
ALL Node:开启rac2,并验证日志记录模式
RAC1
SQL> select log_mode,force_logging from v$database;
LOG_MODE FOR
------------ ---
ARCHIVELOG YES
RAC2
SQL> startup
ORACLE instance started.
Total System Global Area 2020970496 bytes
Fixed Size 2214776 bytes
Variable Size 1224737928 bytes
Database Buffers 788529152 bytes
Redo Buffers 5488640 bytes
Database mounted.
Database opened.
SQL> select log_mode,force_logging from v$database;
LOG_MODE FOR
------------ ---
ARCHIVELOG YES
1.2.主库备份
- 创建rman备份目录
[root@racnode1 u01]# mkdir rmanbak
[root@racnode1 u01]# ls
app rmanbak
[root@racnode1 u01]# chown oracle:oinstall rmanbak
- 使用rman对database和archive log进行备份
[oracle@racnode1 ~]$ rman target /
Recovery Manager: Release 11.2.0.1.0 - Production on Wed Aug 9 10:26:21 2017
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
connected to target database: ORCL (DBID=1479188731)
RMAN> run {
allocate channel c1 type disk;
allocate channel c2 type disk;
allocate channel c3 type disk;
allocate channel c4 type disk;
backup database format '/u01/rmanbak/FULL_%U.bak';
backup archivelog all format '/u01/rmanbak/ARC_%U.bak';
release channel c1;
release channel c2;
release channel c3;
release channel c4;
}
using target database control file instead of recovery catalog
allocated channel: c1
channel c1: SID=148 instance=orcl1 device type=DISK
allocated channel: c2
channel c2: SID=29 instance=orcl1 device type=DISK
allocated channel: c3
channel c3: SID=156 instance=orcl1 device type=DISK
allocated channel: c4
channel c4: SID=32 instance=orcl1 device type=DISK
Starting backup at 09-AUG-2017 10:26:44
channel c1: starting full datafile backup set
channel c1: specifying datafile(s) in backup set
input datafile file number=00001 name=+DATA/orcl/datafile/system.256.951407925
channel c1: starting piece 1 at 09-AUG-2017 10:26:44
channel c2: starting full datafile backup set
channel c2: specifying datafile(s) in backup set
input datafile file number=00002 name=+DATA/orcl/datafile/sysaux.257.951407925
input datafile file number=00004 name=+DATA/orcl/datafile/users.259.951407925
channel c2: starting piece 1 at 09-AUG-2017 10:26:44
channel c3: starting full datafile backup set
channel c3: specifying datafile(s) in backup set
input datafile file number=00003 name=+DATA/orcl/datafile/undotbs1.258.951407925
input datafile file number=00005 name=+DATA/orcl/datafile/undotbs2.264.951408055
channel c3: starting piece 1 at 09-AUG-2017 10:26:45
channel c4: starting full datafile backup set
channel c4: specifying datafile(s) in backup set
including current control file in backup set
channel c4: starting piece 1 at 09-AUG-2017 10:26:52
channel c3: finished piece 1 at 09-AUG-2017 10:26:56
piece handle=/u01/rmanbak/FULL_03sbfaj4_1_1.bak tag=TAG20170809T102644 comment=NONE
channel c3: backup set complete, elapsed time: 00:00:11
channel c3: starting full datafile backup set
channel c3: specifying datafile(s) in backup set
including current SPFILE in backup set
channel c3: starting piece 1 at 09-AUG-2017 10:26:57
channel c4: finished piece 1 at 09-AUG-2017 10:26:57
piece handle=/u01/rmanbak/FULL_04sbfaj5_1_1.bak tag=TAG20170809T102644 comment=NONE
channel c4: backup set complete, elapsed time: 00:00:05
channel c1: finished piece 1 at 09-AUG-2017 10:26:57
piece handle=/u01/rmanbak/FULL_01sbfaj4_1_1.bak tag=TAG20170809T102644 comment=NONE
channel c1: backup set complete, elapsed time: 00:00:13
channel c2: finished piece 1 at 09-AUG-2017 10:26:58
piece handle=/u01/rmanbak/FULL_02sbfaj4_1_1.bak tag=TAG20170809T102644 comment=NONE
channel c2: backup set complete, elapsed time: 00:00:14
channel c3: finished piece 1 at 09-AUG-2017 10:26:58
piece handle=/u01/rmanbak/FULL_05sbfajh_1_1.bak tag=TAG20170809T102644 comment=NONE
channel c3: backup set complete, elapsed time: 00:00:01
Finished backup at 09-AUG-2017 10:26:58
Starting backup at 09-AUG-2017 10:26:59
current log archived
channel c1: starting archived log backup set
channel c1: specifying archived log(s) in backup set
input archived log thread=1 sequence=5 RECID=1 STAMP=951408120
input archived log thread=2 sequence=1 RECID=2 STAMP=951408150
input archived log thread=1 sequence=6 RECID=5 STAMP=951447623
channel c1: starting piece 1 at 09-AUG-2017 10:26:59
channel c2: starting archived log backup set
channel c2: specifying archived log(s) in backup set
input archived log thread=2 sequence=2 RECID=3 STAMP=951408156
input archived log thread=2 sequence=3 RECID=4 STAMP=951447623
input archived log thread=2 sequence=4 RECID=8 STAMP=951555111
channel c2: starting piece 1 at 09-AUG-2017 10:26:59
channel c3: starting archived log backup set
channel c3: specifying archived log(s) in backup set
input archived log thread=1 sequence=7 RECID=6 STAMP=951516208
input archived log thread=1 sequence=8 RECID=7 STAMP=951555108
channel c3: starting piece 1 at 09-AUG-2017 10:26:59
channel c4: starting archived log backup set
channel c4: specifying archived log(s) in backup set
input archived log thread=1 sequence=9 RECID=11 STAMP=951560819
input archived log thread=2 sequence=5 RECID=9 STAMP=951558975
input archived log thread=2 sequence=6 RECID=10 STAMP=951560429
channel c4: starting piece 1 at 09-AUG-2017 10:26:59
channel c1: finished piece 1 at 09-AUG-2017 10:27:00
piece handle=/u01/rmanbak/ARC_06sbfajj_1_1.bak tag=TAG20170809T102659 comment=NONE
channel c1: backup set complete, elapsed time: 00:00:01
channel c2: finished piece 1 at 09-AUG-2017 10:27:00
piece handle=/u01/rmanbak/ARC_07sbfajj_1_1.bak tag=TAG20170809T102659 comment=NONE
channel c2: backup set complete, elapsed time: 00:00:01
channel c3: finished piece 1 at 09-AUG-2017 10:27:00
piece handle=/u01/rmanbak/ARC_08sbfajj_1_1.bak tag=TAG20170809T102659 comment=NONE
channel c3: backup set complete, elapsed time: 00:00:01
channel c4: finished piece 1 at 09-AUG-2017 10:27:00
piece handle=/u01/rmanbak/ARC_09sbfajj_1_1.bak tag=TAG20170809T102659 comment=NONE
channel c4: backup set complete, elapsed time: 00:00:01
Finished backup at 09-AUG-2017 10:27:00
released channel: c1
released channel: c2
released channel: c3
released channel: c4
- 验证备份
[oracle@racnode1 ~]$ ll -th /u01/rmanbak/
总用量 1.3G
-rw-r----- 1 oracle asmadmin 89M 8月 9 10:27 ARC_08sbfajj_1_1.bak
-rw-r----- 1 oracle asmadmin 81M 8月 9 10:27 ARC_07sbfajj_1_1.bak
-rw-r----- 1 oracle asmadmin 4.5M 8月 9 10:26 ARC_09sbfajj_1_1.bak
-rw-r----- 1 oracle asmadmin 30M 8月 9 10:26 ARC_06sbfajj_1_1.bak
-rw-r----- 1 oracle asmadmin 424M 8月 9 10:26 FULL_02sbfaj4_1_1.bak
-rw-r----- 1 oracle asmadmin 96K 8月 9 10:26 FULL_05sbfajh_1_1.bak
-rw-r----- 1 oracle asmadmin 588M 8月 9 10:26 FULL_01sbfaj4_1_1.bak
-rw-r----- 1 oracle asmadmin 18M 8月 9 10:26 FULL_04sbfaj5_1_1.bak
-rw-r----- 1 oracle asmadmin 3.6M 8月 9 10:26 FULL_03sbfaj4_1_1.bak
1.3.在Primary上创建Standby Database的控制文件
SQL> alter database create standby controlfile as'/u01/rmanbak/standby.ctl';
Database altered.
1.4.在Primary上创建Standby Database初始化参数文件
SQL> create pfile='/u01/rmanbak/initphyracdb.ora' from spfile;
File created.
1.5.拷贝密码文件
[oracle@racnode1 dbs]$ pwd
/u01/app/oracle/product/11.2.0/db_1/dbs
[oracle@racnode1 dbs]$ cp orapworcl1 /u01/rmanbak/
1.6.拷贝/rmanbak下的所有备份文件到备库
[oracle@racnode1 rmanbak]$ scp * [email protected]:/u01/rmanbak/
The authenticity of host '192.168.30.134 (192.168.30.134)' can't be established.
RSA key fingerprint is 43:e6:68:ce:37:01:1f:12:46:19:27:39:a5:ba:02:7d.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.30.134' (RSA) to the list of known hosts.
[email protected]'s password:
ARC_06sbfajj_1_1.bak 100% 29MB 29.4MB/s 00:00
ARC_07sbfajj_1_1.bak 100% 81MB 80.8MB/s 00:00
ARC_08sbfajj_1_1.bak 100% 88MB 88.0MB/s 00:01
ARC_09sbfajj_1_1.bak 100% 4598KB 4.5MB/s 00:00
FULL_01sbfaj4_1_1.bak 100% 587MB 117.4MB/s 00:05
FULL_02sbfaj4_1_1.bak 100% 424MB 84.8MB/s 00:05
FULL_03sbfaj4_1_1.bak 100% 3680KB 3.6MB/s 00:00
FULL_04sbfaj5_1_1.bak 100% 18MB 17.7MB/s 00:01
FULL_05sbfajh_1_1.bak 100% 96KB 96.0KB/s 00:00
initphyracdb.ora 100% 1390 1.4KB/s 00:00
orapworcl1 100% 1536 1.5KB/s 00:00
standby.ctl 100% 18MB 17.6MB/s 00:00
1.7.将拷贝到备库上的初始化文件,并修改
orcl.__db_cache_size=771751936
orcl.__java_pool_size=16777216
orcl.__large_pool_size=16777216
orcl.__oracle_base='/u01/app/oracle'#ORACLE_BASE set from environment
orcl.__pga_aggregate_target=822083584
orcl.__sga_target=1207959552
orcl.__shared_io_pool_size=0
orcl.__shared_pool_size=385875968
orcl.__streams_pool_size=0
*.audit_file_dest='/u01/app/oracle/admin/orcl/adump'
*.audit_trail='db'
*.cluster_database=false
*.compatible='11.2.0.0.0'
# 指定控制文件路径
*.control_files='/u01/app/oracle/oradata/orcl/control01.ctl','/u01/app/oracle/oradata/orcl/control02.ctl'
*.db_block_size=8192
*.db_create_file_dest='/u01/app/oracle/oradata/orcl'
*.db_domain=''
*.db_name='orcl'
*.db_recovery_file_dest='/u01/app/oracle/flash_recovery_area'
*.db_recovery_file_dest_size=10737418240
*.diagnostic_dest='/u01/app/oracle'
*.dispatchers='(PROTOCOL=TCP) (SERVICE=orclXDB)'
orcl.instance_number=1
# 归档配置
*.log_archive_config='dg_config=(orcl,rac)'
# 本地归档
*.log_archive_dest_1='LOCATION=/u01/archive_log VALID_FOR=(ALL_LOGFILES,ALL_ROLES)'
# 远程归档
*.log_archive_dest_2='SERVICE=rac LGWR ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=rac'
*.log_archive_dest_state_1='ENABLE'
*.log_archive_dest_state_2='ENABLE'
*.log_archive_format='%t_%s_%r.dbf'
*.log_archive_max_processes=5
*.memory_target=2030043136
*.open_cursors=300
*.processes=150
# 启用本地密码,默认即可
*.remote_login_passwordfile='exclusive'
orcl.thread=1
*.service_names='racdg'
*.standby_file_management='auto'
orcl.undo_tablespace='UNDOTBS1'
# dbf文件路径转换
*.db_file_name_convert='+DATA/ORCL/DATAFILE','/u01/app/oracle/oradata/orcl'
*.db_unique_name='orcl'
# 当前节点【对应监听文件】
*.fal_client='orcl'
# 主节点【对应监听文件】
*.fal_server='rac1','rac2'
# 日志文件路径转换
*.log_file_name_convert='+REDO/ORCL','/u01/archive_log','+DATA/orcl','/u01/group_log'
1.8.使用修改后的初始化文件启动备库到nomount状态
SQL> startup pfile='/u01/app/oracle/product/11.2.0/db_1/dbs/initphyracdb.ora' nomount;
ORACLE instance started.
Total System Global Area 2020970496 bytes
Fixed Size 2254664 bytes
Variable Size 1224739000 bytes
Database Buffers 788529152 bytes
Redo Buffers 5447680 bytes
1.9.备库恢复控制文件
restore controlfile from '/u01/rmanbak/standby.ctl';
1.10.切换备库到mount状态
SQL> alter database mount;
Database altered.
SQL> select status from v$instance;
STATUS
------------------------------------
MOUNTED
1.20.使用rman将主库数据恢复到备库
会自动识别当前目录下的备份文件
RMAN> run {
allocate channel c1 type disk;
allocate channel c2 type disk;
allocate channel c3 type disk;
allocate channel c4 type disk;
restore database ;
release channel c1;
release channel c2;
release channel c3;
release channel c4;
}
released channel: ORA_DISK_1
allocated channel: c1
channel c1: SID=129 device type=DISK
allocated channel: c2
channel c2: SID=193 device type=DISK
allocated channel: c3
channel c3: SID=7 device type=DISK
allocated channel: c4
channel c4: SID=69 device type=DISK
Starting restore at 09-AUG-17
Starting implicit crosscheck backup at 09-AUG-17
Crosschecked 9 objects
Finished implicit crosscheck backup at 09-AUG-17
Starting implicit crosscheck copy at 09-AUG-17
Finished implicit crosscheck copy at 09-AUG-17
searching for all files in the recovery area
cataloging files...
no files cataloged
channel c1: starting datafile backup set restore
channel c1: specifying datafile(s) to restore from backup set
channel c1: restoring datafile 00003 to /u01/app/oracle/oradata/orcl//undotbs1.258.951407925
channel c1: restoring datafile 00005 to /u01/app/oracle/oradata/orcl//undotbs2.264.951408055
channel c1: reading from backup piece /u01/rmanbak/FULL_03sbfaj4_1_1.bak
channel c2: starting datafile backup set restore
channel c2: specifying datafile(s) to restore from backup set
channel c2: restoring datafile 00001 to /u01/app/oracle/oradata/orcl//system.256.951407925
channel c2: reading from backup piece /u01/rmanbak/FULL_01sbfaj4_1_1.bak
channel c3: starting datafile backup set restore
channel c3: specifying datafile(s) to restore from backup set
channel c3: restoring datafile 00002 to /u01/app/oracle/oradata/orcl//sysaux.257.951407925
channel c3: restoring datafile 00004 to /u01/app/oracle/oradata/orcl//users.259.951407925
channel c3: reading from backup piece /u01/rmanbak/FULL_02sbfaj4_1_1.bak
channel c1: piece handle=/u01/rmanbak/FULL_03sbfaj4_1_1.bak tag=TAG20170809T102644
channel c1: restored backup piece 1
channel c1: restore complete, elapsed time: 00:00:01
channel c3: piece handle=/u01/rmanbak/FULL_02sbfaj4_1_1.bak tag=TAG20170809T102644
channel c3: restored backup piece 1
channel c3: restore complete, elapsed time: 00:00:07
channel c2: piece handle=/u01/rmanbak/FULL_01sbfaj4_1_1.bak tag=TAG20170809T102644
channel c2: restored backup piece 1
channel c2: restore complete, elapsed time: 00:00:15
Finished restore at 09-AUG-17
released channel: c1
released channel: c2
released channel: c3
released channel: c4
1.11.添加备库的日志组
SQL> alter database add standby logfile thread 1 group 5 size 50M,group 6 size 50M,group 7 size 50M;
Database altered.
SQL> alter database add standby logfile thread 2 group 8 size 50M,group 9 size 50M,group 10 size 50M;
Database altered.
1.12.主库参数调整
# 设置本地归档路径
SQL> alter system set log_archive_dest_1='LOCATION=+REDO VALID_FOR=(ALL_LOGFILES,ALL_ROLES)';
System altered.
# 设置远程归档路径
SQL> alter system set log_archive_dest_2='SERVICE=orcl LGWR ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=orcl';
System altered.
# 验证归档设置
SQL> show parameter log_archive
# 开启远程归档传输
SQL> alter system set log_archive_dest_state_2=enable;
System altered.
1.13.开启备库应用日志
SQL> alter database recover managed standby database using current logfile disconnect from session;
Database altered.
执行上条命令后,Standby database应为是延迟传输,只有在归档时,才会发生应用,所以在查询日志应用情况时,会发现在当前日志会显示IN MEMORY。
SQL> select thread#, sequence#, applied from v$archived_log;
THREAD# SEQUENCE# APPLIED
---------- ---------- ---------
1 132 YES
2 126 YES
1 133 YES
1 134 IN-MEMORY
1.14.开启ADG
ADG:11G的新特性,在备库上已open状态,打开日志应用进程,默认会 READ ONLY WITH APPLY状态模式,在应用日志的同时也可以读standby database进行查询。11G之间,应用日志时,只能以mount模式打开。
Standby Database
- 取消日志应用
SQL> alter database recover managed standby database cancel;
Database altered.
- 以open模式打开数据库
SQL> alter database open;
Database altered.
- 开启日志应用
SQL> alter database recover managed standby database using current logfile disconnect from session;
Database altered.
- 查询数据库模式是否正常
SQL> select dbid,name,open_mode,current_scn,protection_mode,database_role,force_logging,open_mode,switchover_status from v$database;
DBID NAME OPEN_MODE CURRENT_SCN PROTECTION_MODE
---------- --------- -------------------- ----------- --------------------
DATABASE_ROLE FOR OPEN_MODE SWITCHOVER_STATUS
---------------- --- -------------------- --------------------
1479188731 ORCL READ ONLY WITH APPLY 3638179 MAXIMUM PERFORMANCE
PHYSICAL STANDBY YES READ ONLY WITH APPLY NOT ALLOWED
2.验证DG状态是否正常
2.1.查看archive log序列是否一致
Primary Database
SQL> archive log list;
Database log mode Archive Mode
Automatic archival Enabled
Archive destination +REDO
Oldest online log sequence 21
Next log sequence to archive 22
Current log sequence 22
Standby Database
SQL> archive log list;
Database log mode Archive Mode
Automatic archival Enabled
Archive destination /u01/app/oracle/arch
Oldest online log sequence 21
Next log sequence to archive 0
Current log sequence 22
2.2.查看DG状态
Primary Database
SQL> select dbid,name,open_mode,current_scn,protection_mode,database_role,force_logging,open_mode,switchover_status from v$database;
DBID NAME OPEN_MODE CURRENT_SCN PROTECTION_MODE
---------- --------- -------------------- ----------- --------------------
DATABASE_ROLE FOR OPEN_MODE SWITCHOVER_STATUS
---------------- --- -------------------- --------------------
1479188731 ORCL READ WRITE 1407507 MAXIMUM PERFORMANCE
PRIMARY YES READ WRITE TO STANDBY
- PROTECTION_MODE:MAXIMUM PERFORMANCE 默认最大性能模式
- DATABASE_ROLE:PRIMARY 角色
- SWITCHOVER_STATUS:切换状态(可以查看出当前DG状态)
- NOT ALLOWED 当前库无备库
- TO STANDBY 当前库可以切换成备库
- RESOLVABLE GAP 主库和备库之间有GAP间隙
Standby Database
2.3.验证日志应用
select thread#, sequence#, applied from v$archived_log;
2.4.查看相应进程是否起来
select process from v$managed_standby;