搭建ORACLE DataGuard(使用RMAN备份)
操作系统:windows 2008
数据库:11.1.0.6
一、信息收集
1.配置信息收集
说明 主机名 IP地址 db_name sid DB_UNIQUE_NAME Service_name
主库 dg1 192.168.0.11 orcl orcl orcldg1 orcldg1
备库 dg2 192.168.0.22 orcl orcl orcldg2 orcldg2
2.主库信息收集
2.1查看数据库版本信息
SQL> SELECT * FROM V$VERSION;
2.2查看数据库状态
SQL> SELECT NAME,OPEN_MODE,DATABASE_ROLE,FORCE_LOGGING,LOG_MODE,FLASHBACK_ON FROM V$DATABASE;
SQL> SHOW PARAMETER NAME;
二、准备工作
1.备库安装oracle数据库软件,不创建数据库,保证两个数据库的安装路径一致;
备库设置环境变量:
ORACLE_HOME 为D:\app\Administrator\product\11.1.0.6\db_1
ORACLE_SID 为orcl
2.备库使用ORACLE"配置和移植工具"中的Net Configuration Assistant创建监听;
3.主备库创建目录
CMD> MD D:\app\Administrator\product\11.1.0.6\archive\arch
CMD> MD D:\app\Administrator\database\dg
三、搭建DATAGUARD
1.配置主备库的listener.ora 和tnsnames.ora 文件
1.1修改listener.ora文件
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(SID_NAME = PLSExtProc)
(ORACLE_HOME = D:\app\Administrator\product\11.1.0.6\db_1)
(PROGRAM = extproc)
)
####主备库都新增内容
(SID_DESC =
(GLOBAL_DBNAME = orcl)
(ORACLE_HOME = D:\app\Administrator\product\11.1.0.6\db_1)
(SID_NAME = orcl)
)
#####
)
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1))
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.0.11)(PORT = 1521))
)
)
ADR_BASE_LISTENER = D:\app\Administrator
1.2修改tnsnames.ora 文件
主备库都新增如下内容
ORCLDG1 =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.0.11)(PORT = 1521))
)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcldg1)
)
)
ORCLDG2 =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.0.22)(PORT = 1521))
)
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcldg2)
)
)
1.3重启主备库的监听
CMD>lsnrctl stop
CMD>lsnrctl start
1.4检查SQL*Net配置
在主数据库和备用数据库中使用以下命令检查SQL*Net配置
CMD>tnsping ORCLDG1
CMD>tnsping ORCLDG2
1.5测试远程登录
CMD>sqlplus sys/pwd@ORCLDG1 as sysdba
CMD>sqlplus sys/pwd@ORCLDG2 as sysdba
2.修改主库参数
SQL> ALTER SYSTEM SET INSTANCE_NAME=‘orcl‘ SCOPE=SPFILE; --可不配,使用默认设置
SQL> ALTER SYSTEM SET DB_UNIQUE_NAME=‘orcldg1‘ SCOPE=SPFILE;
SQL> ALTER SYSTEM SET LOCAL_LISTENER=‘orcldg1‘ SCOPE=SPFILE; --配TNS Name,也可不配使用默认设置
SQL> ALTER SYSTEM SET LOG_ARCHIVE_CONFIG=‘DG_CONFIG=(orcldg1,orcldg2)‘ SCOPE=SPFILE; --这里是db_unique_name!如果db_unique_name一致的话,这个参数不需要配
SQL> ALTER SYSTEM SET LOG_ARCHIVE_DEST_1=‘LOCATION=D:\app\Administrator\product\11.1.0.6\archive\arch LGWR VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=orcldg1‘ SCOPE=SPFILE;
SQL> ALTER SYSTEM SET LOG_ARCHIVE_DEST_2=‘SERVICE=orcldg2 ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=orcldg2‘ SCOPE=SPFILE;
SQL> ALTER SYSTEM SET LOG_ARCHIVE_DEST_STATE_1=‘ENABLE‘ SCOPE=SPFILE;
SQL> ALTER SYSTEM SET LOG_ARCHIVE_DEST_STATE_2=‘DEFER‘ SCOPE=SPFILE;--延迟同步
SQL> ALTER SYSTEM SET STANDBY_FILE_MANAGEMENT=‘AUTO‘ SCOPE=SPFILE;
SQL> ALTER SYSTEM SET FAL_CLIENT=‘orcldg1‘ SCOPE=SPFILE; --配主库TNS Name
SQL> ALTER SYSTEM SET FAL_SERVER=orcldg2; SCOPE=SPFILE;--配备库TNS Name
SQL> ALTER SYSTEM SET DB_FILE_NAME_CONVERT=‘D:\app\Administrator\database\dg‘,‘D:\app\Administrator\database\dg\‘ SCOPE=SPFILE;
SQL> ALTER SYSTEM SET LOG_FILE_NAME_CONVERT=‘D:\app\Administrator\database\dg‘,‘D:\app\Administrator\database\dg\‘ SCOPE=SPFILE;
SQL> ALTER SYSTEM SET STANDBY_ARCHIVE_DEST=‘D:\app\Administrator\product\11.1.0.6\archive\arch‘ SCOPE=SPFILE; --配的路径跟LOG_ARCHIVE_DEST_1一致,也可不配,使用默认设置
SQL> ALTER SYSTEM SET LOG_ARCHIVE_FORMAT=‘ARC_%T_%S_%R.arc‘ SCOPE=SPFILE; --可不配,使用默认设置
SQL> ALTER SYSTEM SET LOG_ARCHIVE_MAX_PROCESSES=5 SCOPE=SPFILE; --可不配,使用默认设置
注:
1)在DataGuar备库,没有特殊情况一定要将standby_file_management参数设为AUTO,并且设置db_file_name_convert和log_file_name_convert的值。
若standby_file_management=MANUAL,即使主备库数据文件路径一致,主库添加数据文件,备库也不会自动添加,必须手动完成。
若standby_file_management=AUTO,主库添加数据文件,备库会根据db_file_name_convert参数自动将数据文件创建到正确路径。
2)db_file_name_convert和log_file_name_convert是用来在线转化数据和standby日志文件的位置。主要用在物理standby数据库和rman数据库复制和TSPITR的操作。
它们的作用是:主库备库路径不相同的情况,备库会根据这两个参数将数据文件和redo文件创建到正确位置。
3)db_file_name_convert和log_file_name_convert指定时都必须是目录,不能为文件,否则启动数据库时会有ORA-01678的错误。
3.主库启用FORCE LOGGING模式
SQL> ALTER DATABASE FORCE LOGGING;
4.开启归档模式
SQL> SHUTDOWN IMMEDIATE;
SQL> STARTUP MOUNT;
SQL> ALTER DATABASE ARCHIVELOG;
SQL> ALTER DATABASE OPEN;
5.主库做全备份,并传输备份集到备库的相同位置
CMD> RMAN TARGET /
RUN
{
ALLOCATE CHANNEL C0 DEVICE TYPE DISK;
ALLOCATE CHANNEL C1 DEVICE TYPE DISK;
ALLOCATE CHANNEL C2 DEVICE TYPE DISK;
ALLOCATE CHANNEL C3 DEVICE TYPE DISK;
CONFIGURE CONTROLFILE AUTOBACKUP ON;
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO ‘D:\backup\%F‘;
BACKUP DATABASE FORMAT ‘D:\backup\ora11g_full_db_%d_%T_%u.bak‘;
BACKUP ARCHIVELOG ALL FORMAT ‘D:\backup\ora11g_arc_%s_%p_%t.bak‘;
}
如果主库备份盘空间不足可以使用压缩备份,但耗时稍长;
压缩备份:
将上面脚本中BACKUP DATABASE FORMAT 修改为BACKUP AS COMPRESSED DATABASE FORMAT即可
备份时间较长,在备份过程中可以继续下面的配置.
6.拷贝主库的密码文件到备库相应位置
密码文件默认位置:D:\app\Administrator\product\11.1.0.6\db_1\database\PWDorcl.ora
如果不存在按如下方法可以生成该文件
CMD> ORAPWD FILE=‘$ORACLE_HOME/dbs/PWD%ORACLE_SID%‘ PASSWORD=xxxx ENTRIES=10 FORCE=Y
7.生成备库PFILE文件
7.1在主库生成备库PFILE文件:
SQL> CREATE PFILE=‘D:\app\Administrator\product\11.1.0.6\db_1\dbs\init2.ora‘ FROM SPFILE;
7.2将生成的init2.ora拷贝到备库相应位置
7.3修改init2.ora文件
注意:修改以下下参数(和配置主库时修改的参数项一样,值要具体而定)
#DG CONFIG
*.log_archive_config=‘dg_config=(orcldg1,orcldg2)‘
*.log_archive_dest_1=‘LOCATION=D:\app\Administrator\product\11.1.0.6\archive\arch LGWR VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=orcldg2‘
*.log_archive_dest_2=‘SERVICE=orcldg1 ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=orcldg1‘
*.standby_file_management=‘AUTO‘
*.log_archive_dest_state_1=‘ENABLE‘
*.log_archive_dest_state_2=‘ENABLE‘
*.fal_server=orcldg1
*.db_unique_name=orcldg2
8.启动备库监听
CMD> lsnrctl start
9.进行备库的恢复
注意:
1)这里虽然是恢复备库,但是需要在主库上执行RMAN命令;
2)恢复时请将备分生成后主库上生成的归档日志文件拷贝到备库相应位置,这样在备库恢复时会将这些差异的数据一同恢复到备库。
CMD> RMAN TARGET /
RMAN> CONNECT AUXILIARY sys/pwd@ORCLDG2
RMAN> DUPLICATE TARGET DATABASE FOR STANDBY NOFILENAMECHECK;
10.恢复备库完毕后将主库的LOG_ARCHIVE_DEST_STATE_2设置为ENABLE
SQL> ALTER SYSTEM SET LOG_ARCHIVE_DEST_STATE_2=‘ENABLE‘;
11.主备库添加standby redo log
11.1查看重做日志信息
查看日志组成员
SQL> SELECT MEMBER FROM V$LOGFILE;
查看日志大小
SQL> SELECT GROUP,BYTES/1024*1024*1024,MEMBERS FROM V$LOG;
查看standby redo log
SQL> SELECT GROUP#, BYTES FROM V$STANDBY_LOG;
11.2新增standby redo log
SQL> ALTER DATABASE ADD STANDBY LOGFILE GROUP 4 ‘D:\app\Administrator\database\dg\stdredo04.log‘ SIZE 50M;
SQL> ALTER DATABASE ADD STANDBY LOGFILE GROUP 5 ‘D:\app\Administrator\database\dg\stdredo05.log‘ SIZE 50M;
SQL> ALTER DATABASE ADD STANDBY LOGFILE GROUP 6 ‘D:\app\Administrator\database\dg\stdredo06.log‘ SIZE 50M;
SQL> ALTER DATABASE ADD STANDBY LOGFILE GROUP 7 ‘D:\app\Administrator\database\dg\stdredo07.log‘ SIZE 50M;
注:
Standby redo log的组数参考公式:(online redo log组数 + 1) * 数据库线程数;单机线程数为1,RAC一般为2。
Standby redo log的组成员数和大小也尽量和online redo log一样。
12.备库启用
启动备库到(备用状态的)mount模式
SQL> ALTER DATABASE MOUNT STANDBY DATABASE;
设置备库为应用日志状态
SQL> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE DISCONNECT FROM SESSION;
取消备库自动恢复
SQL> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE CANCEL;
启动备库到readonly模式
SQL> ALTER DATABASE OPEN;
设置备库为时时应用日志状态
SQL> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE USING CURRENT LOGFILE DISCONNECT FROM SESSION;
参考:
blog.chinaunix.net/uid-23284114-id-4142519.html
blog.itpub.net/25105315/viewspace-710330/
blog.csdn.net/hj402555749/article/details/17654769
blog.itpub.net/12679300/viewspace-1127775/
blog.csdn.net/yuzhenhuan01/article/details/6450565
blog.chinaunix.net/uid-15108676-id-4134935.html
本文出自 “4699096” 博客,请务必保留此出处http://4709096.blog.51cto.com/4699096/1761537
原文地址:http://4709096.blog.51cto.com/4699096/1761537