Oracle Dataguard是自9I后引进的新特性,为防止任何可能引起数据丢失的元素,无论是因为人为差错,操作系统崩溃,还是自然和恐怖灾害
(哈哈,夸张了些)。说白了它是在主节点与备用节点间通过日志同步来保证数据的同步,可以实现快速切换与灾难性恢复。Data guard只是
在软件上对数据库进行设置,并不需要额外购买任何组件能在对主数据库影响很小的情况下,实现主备数据库的同步,而主备机的数据差异只
在在线日志部分,所以被不少企业作为了数据容灾方案。
Primary 主数据库
前面提到,Data Guard包含一个primary数据库即被大部分应用访问的生产数据库,该库即可以是单实例数据库,也可以是RAC。
Standby 备用数据库
同一个Data Guard中你可以最多创建9个standby数据库。一旦创建完成,Data Guard通过应用primary数据库的redo自动维护每一个standby数
据库。Standby数据库同样即可以是单实例数据库,也可以是RAC结构。关于standby数据库,通常分两类:逻辑standby和物理standby
DG保护模式
Dataguard提供了三种保护模式
1、 最大保护模式(maximum protection)
要求所有事务提交前不仅些redo不仅被写到本地的online redo log中,同时还要提交到standby的redo log中,并确认其中一个可用,最后才
会在primary上提交。
2、 最高性能(maximum performance)
事务可以随时提交,当前primary的redo也要至少写入一个standby数据库,但它可以是不同步的。
3、 最高可用性(maximum availability)
要求所有事务在提交前必须保障redo数据至少在一个standby数据库可用,不过与之不同的是,如果出现故障导入无法同时写入standby数据库
redo log,primary数据库并不会shutdown,而是自动转为最高性能模式,等standby数据库恢复正常之后,它又会再自动转换成最高可用性模
式
配置要求
1、 硬件及操作系统需求
同一个Data Gurid配置中的所有oracle数据库必须运行于相同的平台。比如inter架构下的32位linux系统可以与inter架构下的32位linux系统
组成一组Data Guard。另外,如果服务器都运行于32位的话,64位HP-UX也可以与32位HP-UX组成一组Data Guard。
不同服务器的硬件配置可以不同,比如cpu啦,内存啦,存储设备啦,但是必须确保standby数据库服务器有足够的磁盘空间用来接收及应用
redo数据。
primary 数据库和standby数据库的操作系统必须一致,不过操作系统版本可以略有差异,比如(linux as4&linux as5),primary数据库和
standby数据库的目录路径也可以不同。
2、 软件需求
Data Guard 是Oracle企业版的一个特性,明白了吧,标准版是不支持地。
通过Data Guard的SQL应用,可以实现滚动升级服务器数据库版本(要求升级前数据库版本不低于10.1.0.3)。
同一个Data Guard配置中所有数据库初始化参数:COMPATIBLE的值必须相同。
Primary 数据库必须运行于归档模式 ,并且务必确保在primary数据库上打开FORCE LOGGING,以避免用户通过nologging等方式避免写redo造
成对应的操作无法传输到standby数据库。
Primary 和standby数据库均可应用于单实例或RAC架构下 ,并且同一个data guard配置可以混合使用逻辑standby和物理standby 。
Primary 和standby数据库可以在同一台服务器,但需要注意各自的数据文件存放目录,避免重写或覆盖。
使用具有sysdba系统权限的用户管理primary和standby数据库。
建议数据库必须采用相同的存储架构。比如存储采用ASM/OMF的话,那不分primarty或是standby也都需要采用ASM/OMF。
另外还有很重要一点,注意各服务器的时间设置,不要因为时区/时间设置的不一置造成同步上的 问题
更详细的内容可以看看三思笔记《一步一步学Dataguard》下载地址
http://blog.oracle.com.cn/html/24/t-135124.html
安装环境(配置物理standby)
Guard1:
IP:192.168.1.79
OS ver: Centos 5 64bit
Oracle ver: Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - 64bit
SID:orcl79
Guard2:
IP:192.168.1.76
OS ver: Centos 5 64bit
Oracle ver: Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - 64bit(no database)
SID:orcl76
两台机器都已经安装好ORACLE10G,测试成功,且都工作在noarchivelog模式,采用文件系统存放数据文件。Standby只安装数据库软件
安装步骤
Primary和standby安装oracle 10G
略
Primary设置
1)修改主库属性
我这里选用192.168.1.79作为primary。
[oracle@centos79 ~]$ sqlplus /nolog
SQL*Plus: Release 10.2.0.1.0 - Production on Fri Aug 14 13:45:14 2009
Copyright (c) 1982, 2005, Oracle. All rights reserved.
SQL> conn / as sysdba
Connected.
SQL> alter database force logging;
alter database force logging
*
ERROR at line 1:
ORA-12920: database is already in force logging mode
之前改过了。
SQL>
2)修改数据库为归档模式
SQL> archive log list
Database log mode Archive Mode
Automatic archival Enabled
Archive destination USE_DB_RECOVERY_FILE_DEST
Oldest online log sequence 3
Next log sequence to archive 5
Current log sequence 5
SQL>
现在数据库已经运行在归档模式下。
3)创建密码文件
同一个Dataguard中所有数据库必须拥有独立的密码文件,并且保证sys用户拥有相同密码来保证redo的传输。
[oracle@centos79 dbs]$ orapwd file='/usr/local/ora10/product/10.2.0/db_1/dbs/orapworcl.ora' password=system entries=10
4)配置standby redo log
Su到root用户在/home下创建存放standby redo log目录
SQL> shutdown immediate
SQL> startup mount
Alter database add standby logfile group 4
('/home/oracle/oracle/dgredo/standby_redo4a.log','/home/oracle/oracle/dgredo/standby_redo4b.log') size 50M;
6)创建primary数据库初始化参数
通过修改创建的pfile,然后重新生成spfile,
SQL> create pfile from spfile;
File created.
SQL>
备份一份pfile.
cp /usr/local/ora10/product/10.2.0/db_1/dbs/initorcl.ora /home/oracle/oracle/
7)设置主库参数文件
*.db_name='orcl'
*.DB_UNIQUE_NAME='primary'
*.log_archive_config='DG_CONFIG=(primary,standby)'
*.log_archive_dest_1='location=/usr/local/ora10/flash_recovery_area/ VALID_FOR=(ALL_LOGFILES,ALL_ROLES)'
DB_UNIQUE_NAME='primary'
*.log_archive_dest_2='SERVICE=orcl76 arch ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=standby'
*.LOG_ARCHIVE_DEST_STATE_1=ENABLE
*.LOG_ARCHIVE_DEST_STATE_2=ENABLE
*.remote_login_passwordfile='EXCLUSIVE'
------------------------------------#standby角色参数
*.FAL_SERVER=orcl76
*.FAL_CLIENT=orcl79
DB_FILE_NAME_CONVERT=’oradata/orcl’,’oradata/orcl’
LOG_FILE_NAME_CONVERT=’oradata/orcl’,’oradata/orcl’
*.STANDBY_FILE_MANAGEMENT=AUTO
8)用pfile启动数据库
SQL> Shutdown immediate
SQL> startup pfile='/usr/local/ora10/product/10.2.0/db_1/dbs/initorcl.ora'
SQL> create spfile from pfile='/usr/local/ora10/product/10.2.0/db_1/dbs/initorcl.ora';
SQL> shutdown immediate
SQL> startup
9)修改listener.ora
[oracle@centos79 admin]$ cat listener.ora
# listener.ora Network Configuration File: /usr/local/ora10/product/10.2.0/db_1/network/admin/listener.ora
# Generated by Oracle configuration tools.
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(SID_NAME = PLSExtProc)
(ORACLE_HOME = /usr/local/ora10/product/10.2.0/db_1)
(PROGRAM = extproc)
)
)
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1))
(ADDRESS = (PROTOCOL = TCP)(HOST = centos79)(PORT = 1522))
)
)
LISTENER1 =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1))
(ADDRESS = (PROTOCOL = TCP)(HOST = centos79)(PORT = 1523))
)
)
10)修改tnsnames.ora文件
[oracle@centos79 admin]$ cat tnsnames.ora
# tnsnames.ora Network Configuration File: /usr/local/ora10/product/10.2.0/db_1/network/admin/tnsnames.ora
# Generated by Oracle configuration tools.
LISTENER_ORCL =
(ADDRESS = (PROTOCOL = TCP)(HOST = centos79)(PORT = 1522))
ORCL79 =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = centos79)(PORT = 1522))
(ADDRESS = (PROTOCOL = TCP)(HOST = centos76)(PORT = 1522))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl)
)
)
ORCL76 =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = centos76)(PORT = 1521))
(ADDRESS = (PROTOCOL = TCP)(HOST = centos79)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl)
)
)
ORCL254 =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.254)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl)
)
)
EXTPROC_CONNECTION_DATA =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1))
)
(CONNECT_DATA =
(SID = PLSExtProc)
(PRESENTATION = RO)
)
)
[oracle@centos79 admin]$
确保tnsping两台机器都能 ping通
10)备份数据库
RMAN> backup full database format '/home/oracle/oracle/backup_%T_%s_%p.bak';
11)创建standby数据控制文件
SQL> alter database create standby controlfile as '/home/oracle/oracle/dgredo/standby.ctl';
Database altered.
SQL>
11)拷贝密码文件,数据文件,standby controlfile到备机
略
[oracle@centos79 orcl]$ scp /home/oracle/oracle/dgredo/standby.ctl
[email protected]:/usr/local/ora10/oradata/orcl/
Standby配置
1)创建对应目录
$ORACLE_BASE下oradata,admin,flash_recovery_area目录
2)拷贝文件,包括数据文件,standby controlfile,全库备份文件,参数文件,监听和服务文件
[root@centos79 dbs]# scp initorcl.ora orapworcl.ora
[email protected]:/usr/local/ora10/product/10.2.0/db_1/dbs/
root@centos79 admin]# scp listener.ora sqlnet.ora tnsnames.ora
[email protected]:/usr/local/ora10/product/10.2.0/db_1/network/admin/
3)创建控制文件
[oracle@centos76 orcl]$ mv standby.ctl /usr/local/ora10/oradata/orcl/control01.ctl
[oracle@centos76 orcl]$ cp control01.ctl /usr/local/ora10/oradata/orcl/control02.ctl
[oracle@centos76 orcl]$ cp control01.ctl /usr/local/ora10/oradata/orcl/control03.ctl
[oracle@centos76 orcl]$
4)配置listener,tnsname
Lintener.ora文件如下。
[oracle@centos76 admin]$ cat listener.ora
# listener.ora Network Configuration File: /usr/local/ora10/product/10.2.0/db_1/network/admin/listener.ora
# Generated by Oracle configuration tools.
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(SID_NAME = PLSExtProc)
(ORACLE_HOME = /usr/local/ora10/product/10.2.0/db_1)
(PROGRAM = extproc)
)
)
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1))
(ADDRESS = (PROTOCOL = TCP)(HOST = centos76)(PORT = 1521))
)
)
LISTENER1 =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1))
(ADDRESS = (PROTOCOL = TCP)(HOST = centos76)(PORT = 1522))
)
)
TNSNAMES.ORA文件如下
[oracle@centos76 admin]$ cat tnsnames.ora
# tnsnames.ora Network Configuration File: /usr/local/ora10/product/10.2.0/db_1/network/admin/tnsnames.ora
# Generated by Oracle configuration tools.
LISTENER_ORCL =
(ADDRESS = (PROTOCOL = TCP)(HOST = centos79)(PORT = 1522))
ORCL79 =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = centos79)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl)
)
)
ORCL76 =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = centos76)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl)
)
)
ORCL254 =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.254)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl)
)
)
EXTPROC_CONNECTION_DATA =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1))
)
(CONNECT_DATA =
(SID = PLSExtProc)
(PRESENTATION = RO)
)
)
5)设置备库参数文件
*.db_name='orcl'
*.DB_UNIQUE_NAME='standby'
*.log_archive_config='DG_CONFIG=(primary,standby)'
*.log_archive_dest_1='location=/usr/local/ora10/flash_recovery_area/ VALID_FOR=(ALL_LOGFILES,ALL_ROLES)'
DB_UNIQUE_NAME='standby'
*.log_archive_dest_2='SERVICE=orcl79 arch ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=parimary'
*.LOG_ARCHIVE_DEST_STATE_1=ENABLE
*.LOG_ARCHIVE_DEST_STATE_2=ENABLE
*.remote_login_passwordfile='EXCLUSIVE'
------------------------------------#standby角色参数
*.FAL_SERVER=orcl79
*.FAL_CLIENT=orcl76
DB_FILE_NAME_CONVERT=’oradata/orcl’,’oradata/orcl’
LOG_FILE_NAME_CONVERT=’oradata/orcl’,’oradata/orcl’
*.STANDBY_FILE_MANAGEMENT=AUTO
6)创建密码文件
[oracle@centos76 ~]$ orapwd file='/usr/local/ora10/product/10.2.0/db_1/dbs/orapworcl76.ora' password=system entries=20
7)在备库上建立spfile
$ sqlplus '/as sysdba'
SQL> create spfile from pfile;
启动物理备库
SQL> startup nomount
SQL> alter database mount standby database;
备库做rman恢复
$ rman target / (要求主备库rman备份文件的存放路径和文件名一致)
RMAN> restore database;
Starting restore at 18-AUG-09
Starting implicit crosscheck backup at 18-AUG-09
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=153 devtype=DISK
Crosschecked 8 objects
Finished implicit crosscheck backup at 18-AUG-09
Starting implicit crosscheck copy at 18-AUG-09
using channel ORA_DISK_1
Crosschecked 4 objects
Finished implicit crosscheck copy at 18-AUG-09
searching for all files in the recovery area
cataloging files...
no files cataloged
using channel ORA_DISK_1
channel ORA_DISK_1: starting datafile backupset restore
channel ORA_DISK_1: specifying datafile(s) to restore from backup set
restoring datafile 00001 to /usr/local/ora10/oradata/orcl/system01.dbf
restoring datafile 00002 to /usr/local/ora10/oradata/orcl/undotbs01.dbf
restoring datafile 00003 to /usr/local/ora10/oradata/orcl/sysaux01.dbf
restoring datafile 00004 to /usr/local/ora10/oradata/orcl/users01.dbf
channel ORA_DISK_1: reading from backup piece /home/oracle/oracle/backup_20090818_11_1.bak
channel ORA_DISK_1: restored backup piece 1
piece handle=/home/oracle/oracle/backup_20090818_11_1.bak tag=TAG20090818T165957
channel ORA_DISK_1: restore complete, elapsed time: 00:00:36
Finished restore at 18-AUG-09
8)备库启动redo apply
SQL> alter database recover managed standby database disconnect from session;
Database altered.
SQL>
Data guard的优点:
1.支持所有的DDL和DML语句
2.不管是什么数据类型、表的类型,任何DDL和DML语句都可以应用在物理备用数据库上。
3.可以减轻主数据库的备份压力
4.standby的中的数据文件可以用来快速恢复主数据库的数据文件
5.逻辑standby可以减轻主数据库的工作压力
6.物理standby也可以用只读来打开,可以分担一部分非实时的查询的工作
7.逻辑standby数据是近实时更新的,而且也可以让用户进行查询操作
8.逻辑standby可以在standby中建立索引和物化视图以方便用户的查询