Oracle 11g R2之Dataguard搭建物理standby
数据库版本:11.2.0.4
服务器版本:RedHat6.4
主库Primary:安装数据库软件及实例
备库Standby: 仅安装数据库软件
Database DB_UNIQUE_NAME Oracle Net Service Name Primary orcl orcl Physical standby dg dg
一、搭设前主备库环境:
主库Primary:
[oracle@oracle dbs]$ cat /home/oracle/.bash_profile # .bash_profile # Get the aliases and functions if [ -f ~/.bashrc ]; then . ~/.bashrc fi # User specific environment and startup programs PATH=$PATH:$HOME/bin export PATH export TMP=/tmp export TMPDIR=$TMP export ORACLE_HOSTNAME=orcl export ORACLE_UNQNAME=orcl export ORACLE_BASE=/u01/oracle export ORACLE_HOME=$ORACLE_BASE/product/11.2.0/db_1 export ORACLE_SID=orcl export PATH=$ORACLE_HOME/bin:$PATH export LD_LIBRARY_PATH=$ORACLE_HOME/lib:$LD_LIBRARY_PATH export CLASSPATH=$ORACLE_HOME/jlib:$ORACLE_HOME/rdbms/jlib:$CLASSPATH
备库Standby:
[oracle@dg ~]$ cat /home/oracle/.bash_profile # .bash_profile # Get the aliases and functions if [ -f ~/.bashrc ]; then . ~/.bashrc fi # User specific environment and startup programs PATH=$PATH:$HOME/bin export PATH export TMP=/tmp export TMPDIR=$TMP export ORACLE_HOSTNAME=dg export ORACLE_UNQNAME=dg export ORACLE_BASE=/u01/oracle export ORACLE_HOME=$ORACLE_BASE/product/11.2.0/db_1 export ORACLE_SID=dg export PATH=$ORACLE_HOME/bin:$PATH export LD_LIBRARY_PATH=$ORACLE_HOME/lib:$LD_LIBRARY_PATH export CLASSPATH=$ORACLE_HOME/jlib:$ORACLE_HOME/rdbms/jlib:$CLASSPATH
二、搭设Dataguard
主库Primary操作:
--检查数据库是否支持Dataguard [oracle@oracle dbs]$ sqlplus / as sysdba sql*Plus: Release 11.2.0.4.0 Production on Thu Jul 23 10:02:01 2015 Copyright (c) 1982,2013,Oracle. All rights reserved. Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production With the Partitioning,OLAP,Data Mining and Real Application Testing options sql> select * from v$option where parameter = 'Managed Standby'; PARAMETER ---------------------------------------------------------------- VALUE ---------------------------------------------------------------- Managed Standby TRUE
--检查主库是否开启归档模式 sql> archive log list Database log mode Archive Mode Automatic archival Enabled Archive destination /u01/archive/orcl Oldest online log sequence 23 Next log sequence to archive 25 Current log sequence 25 --如未开启执行下列操作 sql> shutdown immediate sql> startup mount sql> alter database archivelog; sql> alter database open; --将数据库设为强制日志模式 sql> alter database force logging; --创建数据库密码文件(如果没有) [oracle@oracle /]$ orapwd file=$ORACLE_HOME/dbs/orapw$ORACLE_SID password=oracle force=y entries=5 --创建主库归档目录 [oracle@oracle /]$ cd /u01 [oracle@oracle u01]$ mkdir archive [oracle@oracle u01]$ cd archive [oracle@oracle archive]$ mkdir orcl --备份数据库初始化参数文件 sql> create pfile='/home/oracle/pfile.ora' from spfile; --创建pfile文件,该文件方便修改初始化参数 sql> create pfile from spfile; --打开pfile文件,添加如下内容 DB_UNIQUE_NAME=orcl LOG_ARCHIVE_CONFIG='DG_CONFIG=(orcl,dg)' LOG_ARCHIVE_DEST_1='LOCATION=/u01/archive/orcl VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=orcl' LOG_ARCHIVE_DEST_2='SERVICE=dg ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=dg' LOG_ARCHIVE_DEST_STATE_1=ENABLE LOG_ARCHIVE_DEST_STATE_2=ENABLE REMOTE_LOGIN_PASSWORDFILE=EXCLUSIVE LOG_ARCHIVE_FORMAT=%t_%s_%r.arc FAL_SERVER=dg DB_FILE_NAME_CONVERT='/u01/oracle/oradata/dg/','/u01/oracle/oradata/orcl/' LOG_FILE_NAME_CONVERT='/u01/archive/dg','/u01/archive/orcl' STANDBY_FILE_MANAGEMENT=AUTO --关闭数据库 sql> shutdown immediate --通过pfile创建spfile sql> create spfile from pfile; --打开数据库 sql> startup --备份主库Primary数据库文件 --关闭数据库 sql> shutdown immediate --停止监听 [oracle@oracle /]$ lsnrctl stop --启动数据库到mount状态 sql> startup mount --创建备份目录 [oracle@oracle /]$ cd /u01 [oracle@oracle u01]$ mkdir backup --rman备份 [oracle@oracle /]$ rman target / RMAN> backup full database format '/u01/backup/backup_%t_%s_%p.bak'; --在主库上建立备库的控制文件(2份) sql> alter database create standby controlfile as '/u01/backup/stdby_control01.ctl'; [oracle@oracle /]$ cd /u01/backup/ [oracle@oracle backup]$ cp stdby_control01.ctl stdby_control02.ctl
备库Standby操作:
--创建必要的目录 cd /u01 mkdir archive cd archive mkdir dg cd /u01/oracle mkdir admin cd admin mkdir dg cd dg mkdir adump cd /u01/oracle/oradata mkdir dg cd /u01/oracle mkdir fast_recovery_area cd fast_recovery_area mkdir dg cd /u01 mkdir backup --通过scp将主库中的备份文件传到备库上 [oracle@oracle backup]$ cd /u01/backup [oracle@oracle backup]$ scp backup*.bak 192.168.8.223:/u01/backup [oracle@oracle backup]$ scp stdby_control*.ctl 192.168.8.223:/u01/oracle/oradata/ [oracle@oracle backup]$ cd $ORACLE_HOME/dbs [oracle@oracle dbs]$ scp initorcl.ora 192.168.8.223:$ORACLE_HOME/dbs/ 传输好后,将控制文件放到初始化参数中设置的目录即可 --为备库standby创建密码文件: [oracle@dg ~]$ orapwd file=$ORACLE_HOME/dbs/orapw$ORACLE_SID password=oracle force=y entries --修改备库初始化参数文件(主库初始化参数文件为initorcl.ora需修改成initdg.ora), 将初始化文件中添加如下内容(主库中类似的内容删除或进行修改): DB_UNIQUE_NAME=dg LOG_ARCHIVE_CONFIG='DG_CONFIG=(orcl,dg)' LOG_ARCHIVE_DEST_1='LOCATION=/u01/archive/dg VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=dg' LOG_ARCHIVE_DEST_2='SERVICE=orcl ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=orcl' LOG_ARCHIVE_DEST_STATE_1=ENABLE LOG_ARCHIVE_DEST_STATE_2=ENABLE REMOTE_LOGIN_PASSWORDFILE=EXCLUSIVE LOG_ARCHIVE_FORMAT=%t_%s_%r.arc FAL_SERVER=orcl DB_FILE_NAME_CONVERT='/u01/oracle/oradata/orcl/','/u01/oracle/oradata/dg/' LOG_FILE_NAME_CONVERT='/u01/archive/orcl','/u01/archive/dg' STANDBY_FILE_MANAGEMENT=AUTO --通过pfile创建spfile文件 [oracle@dg ~]$ sqlplus / as sysdba sql> create spfile from pfile; --启动物理备库standby sql> startup nomount sql> alter database mount standby database; --备库standby做rman恢复 [oracle@dg ~]$ rman target / RMAN> restore database;
配置主备库文件(备库没有可以从主库scp过来进行修改),下面仅贴出主备库相关文件内容
--主库Primary的listener.ora # listener.ora Network Configuration File: /u01/oracle/product/11.2.0/db_1/network/admin/listener.ora # Generated by Oracle configuration tools. LISTENER = (DESCRIPTION_LIST = (DESCRIPTION = (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521)) (ADDRESS = (PROTOCOL = TCP)(HOST = oracle)(PORT = 1521)) ) ) ADR_BASE_LISTENER = /u01/oracle --备库standby的listener.ora # listener.ora Network Configuration File: /u01/oracle/product/11.2.0/db_1/network/admin/listener.ora # Generated by Oracle configuration tools. LISTENER = (DESCRIPTION_LIST = (DESCRIPTION = (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521)) (ADDRESS = (PROTOCOL = TCP)(HOST = dg)(PORT = 1521)) ) ) ADR_BASE_LISTENER = /u01/oracle --主库Primary的tnsnames.ora # tnsnames.ora Network Configuration File: /u01/oracle/product/11.2.0/db_1/network/admin/tnsnames.ora # Generated by Oracle configuration tools. ORCL = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = oracle)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = orcl) ) ) DG = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.8.223)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = dg) ) ) --备库standby的tnsnames.ora # tnsnames.ora Network Configuration File: /u01/oracle/product/11.2.0/db_1/network/admin/tnsnames.ora # Generated by Oracle configuration tools. DG = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = dg)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = dg) ) ) ORCL = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.8.205)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = orcl) ) ) --完成后重启listener lsnrctl stop lsnrctl start --tnsping主备库 tnsping orcl tnsping dg --tnsping时可能会有linux防火墙限制,禁用防火墙命令 service iptables stop
配置standby redolog(如果数据库是最佳性能模式可以忽略,如果以后变成备库且要转为其它两种模式则要建立)
--主库Primary上建立: sql> alter database add standby logfile group 4 ('/u01/oracle/oradata/orcl/stdby_redo04.log') size 50m,group 5 ('/u01/oracle/oradata/orcl/stdby_redo05.log') size 50m,group 6 ('/u01/oracle/oradata/orcl/stdby_redo06.log') size 50m,group 7 ('/u01/oracle/oradata/orcl/stdby_redo07.log') size 50m; --备库standby上建立 sql> alter database add standby logfile group 4 ('/u01/oracle/oradata/dg/stdby_redo04.log') size 50m,group 5 ('/u01/oracle/oradata/dg/stdby_redo05.log') size 50m,group 6 ('/u01/oracle/oradata/dg/stdby_redo06.log') size 50m,group 7 ('/u01/oracle/oradata/dg/stdby_redo07.log') size 50m;
--在备库standby上启动redo apply sql> alter database recover managed standby database disconnect from session;
到此物理standby创建完毕!
一些常用命令(整理中)
--查询角色切换状态 select switchover_status from v$database; --备库启动顺序 startup nomount alter database mount standby database; alter database recover managed standby database disconnect from session; --从正在恢复状态只读打开 alter database recover managed standby database cancel; alter database open read only; --主备库实时同步 alter database recover managed standby database using current logfile disconnect from session;
<script type="text/javascript"> $(function () { $('pre.prettyprint code').each(function () { var lines = $(this).text().split('\n').length; var $numbering = $('<ul/>').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($('<li/>').text(i)); }; $numbering.fadeIn(1700); }); }); </script>