手把手教你搭建Oracle 11G dataguard
一、前期准备
在准备搭建DATAGARD的时候,看过许多篇教程,按着教程去走,出现过问题,然后就不知道该如何进行下一步。
后来去看了官方文档的,整理一下思路后事情就简单多了。
以一主主库一备库为例,搭建DG的思路如下:
关闭防火墙。
配置ssh互信(非必须,减少SCP时输入密码的次数)
主库上的操作:
1)开启归档模式,开启forcedlogging;
2)添加standbylogfile,standbylogfile组比logfile组多一个;
3)修改参数文件
4)配置listener.ora和tnsnames.ora
5)备份数据库和控制文件
备库准备:
1)将主库上的参数文件、密码文件拷贝到备库上,修改参数文件;
2)将主库上的listener.ora和tnsnames.ora复制到备库上,修改listener.ora文件
3)将备份复制到备库上
4)在备库上创建对应的文件夹
5)启动备库到nomount模式
6)在备库上使用RMAN恢复数据库
7)备库上添加standbylogfile
8)应用归档
9)查看状态
二、基本配置准备
2.1primary服务器安装数据库软件并安装ORCL数据库,standby服务器上安装
2.2 关闭防火墙
primary服务器关闭防火墙:
standby服务器关闭防火墙
2.3 配置ssh互信
百度下“linuxscp免密码”,随便找篇文章看下。
[oracle@primarydbs]$ssh-keygen-trsa
[oracle@primarydbs]$scp~/.ssh/id_rsa.puboracle@192.168.62.202:/home/oracle/.ssh/authorized_keys
三、主库配置
1)enableforcedlogging:
SQL>ALTERDATABASEFORCELOGGING;
查看是否已经是归档模式,如果是未归档模式,则将其修改成归档模式。
SQL>SHUTDOWNIMMEDIATE;
SQL>STARTUPMOUNT;
SQL>ALTERDATABASEARCHIVELOG;
SQL>ALTERDATABASEOPEN;
3)配置standbylogfile文件,当主库变成备库时,可以接收来自备库的日志。
alterdatabaseaddstandbylogfilegroup4('/u01/app/oracle/oradata/orcl/redo_dg_021.log')size50M;
alterdatabaseaddstandbylogfilegroup5('/u01/app/oracle/oradata/orcl/redo_dg_022.log')size50M;
alterdatabaseaddstandbylogfilegroup6('/u01/app/oracle/oradata/orcl/redo_dg_023.log')size50M;
alterdatabaseaddstandbylogfilegroup7('/u01/app/oracle/oradata/orcl/redo_dg_024.log')size50M;
3)配置主库的参数:这里有两种方式,一种是使用alter system 来修改参数,另外一种是生成静态参数文件之后修改参数;我采用的是后一种配置方式。
参数的配置参考OracleDataGuardConceptsandAdministration11gRelease2(11.2)E41134-03中3-3和3-4页
initorcl.ora已经有的参数,可以不用配置。
SQL>createpfilefromspfile;
[oracle@primary~]$cd$ORACLE_HOME/dbs
[oracle@primarydbs]$viminitorcl.ora
#primary
#*.db_name='orcl'
*.db_unique_name=pri
*.LOG_ARCHIVE_CONFIG='DG_CONFIG=(pri,sty)'
#*.control_files='/u01/app/oracle/oradata/orcl/control01.ctl','/u01/app/oracle/oradata/orcl/control02.ctl'
*.LOG_ARCHIVE_DEST_1='LOCATION=/u01/app/oracle/archVALID_FOR=(ALL_LOGFILES,ALL_ROLES)DB_UNIQUE_NAME=pri'
*.LOG_ARCHIVE_DEST_2='SERVICE=styASYNCVALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE)DB_UNIQUE_NAME=sty'
*.LOG_ARCHIVE_DEST_STATE_1=ENABLE
*.LOG_ARCHIVE_DEST_STATE_2=ENABLE
*.remote_login_passwordfile='EXCLUSIVE'
*.LOG_ARCHIVE_FORMAT=%t_%s_%r.arc
#standbyrole
*.FAL_SERVER=sty
*.DB_FILE_NAME_CONVERT='/u01/app/oracle/oradata/orcl','/u01/app/oracle/oradata/orcl'
*.LOG_FILE_NAME_CONVERT='/u01/app/oracle/oradata/orcl','/u01/app/oracle/oradata/orcl'
*.STANDBY_FILE_MANAGEMENT=AUTO
SQL>shutdownimmediate
SQL>createspfilefrompfile;
4)主库上配置listener.ora和tnsnames.ora
主库上的listener.ora:
[oracle@primaryadmin]$catlistener.ora
#listener.oraNetworkConfigurationFile:/u01/app/oracle/product/11.2.0/dbhome_1/network/admin/listener.ora
#GeneratedbyOracleconfigurationtools.
SID_LIST_LISTENER=
(SID_LIST=
(SID_DESC=
(GLOBAL_DBNAME=ORCL)
(ORACLE_HOME=/u01/app/oracle/product/11.2.0/dbhome_1)
(SID_NAME=ORCL)
)
)
LISTENER=
(DESCRIPTION=
(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.62.201)(PORT=1521))
)
ADR_BASE_LISTENER=/u01/app/oracle
主库上的tnsnames.ora:
[oracle@primaryadmin]$cattnsnames.ora
#tnsnames.oraNetworkConfigurationFile:/u01/app/oracle/product/11.2.0/dbhome_1/network/admin/tnsnames.ora
#GeneratedbyOracleconfigurationtools.
PRI=
(DESCRIPTION=
(ADDRESS_LIST=
(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.62.201)(PORT=1521))
)
(CONNECT_DATA=
(SID=orcl)
)
)
STY=
(DESCRIPTION=
(ADDRESS_LIST=
(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.62.202)(PORT=1521))
)
(CONNECT_DATA=
(SID=orcl)
)
)
修改完成之后要重启监听。
5)备份数据库和控制文件
[oracle@primaryadmin]$rmantargetsys/asd@pri
RMAN>backupdatabaseformat'/u01/app/oracle/backup/full_db_%U';
SQL>ALTERDATABASECREATESTANDBYCONTROLFILEAS'/u01/app/oracle/backup/sty.ctl';
四、备库上的操作
1)将主库上的参数文件、密码文件拷贝到备库上,修改参数文件;
[oracle@primaryadmin]$cd$ORACLE_HOME/dbs
[oracle@primarydbs]$scpinitorcl.oraorapworcloracle@192.168.62.202:/u01/app/oracle/product/11.2.0/dbhome_1/dbs/
修改参数文件:
vim/u01/app/oracle/product/11.2.0/dbhome_1/dbs/initorcl.ora
*.db_name='orcl'
*.DB_UNIQUE_NAME=sty
*.control_files='/u01/app/oracle/oradata/orcl/control01.ctl','/u01/app/oracle/flash_recovery_area/orcl/control02.ctl'
*.DB_FILE_NAME_CONVERT='/u01/app/oracle/oradata/orcl','/u01/app/oracle/oradata/orcl'
*.LOG_FILE_NAME_CONVERT='/u01/app/oracle/oradata/orcl','/u01/app/oracle/oradata/orcl'
*.LOG_ARCHIVE_FORMAT=log%t_%s_%r.arc
*.LOG_ARCHIVE_DEST_1='LOCATION=/u01/app/oracle/archVALID_FOR=(ALL_LOGFILES,ALL_ROLES)DB_UNIQUE_NAME=sty'
*.LOG_ARCHIVE_DEST_2='SERVICE=priASYNCVALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE)DB_UNIQUE_NAME=pri'
*.LOG_ARCHIVE_DEST_STATE_1=ENABLE
*.LOG_ARCHIVE_DEST_STATE_2=ENABLE
*.REMOTE_LOGIN_PASSWORDFILE=EXCLUSIVE
*.STANDBY_FILE_MANAGEMENT=AUTO
*.FAL_SERVER=pri
标红的是修改的部分
密码文件说明:主库上有授权或者收回sysdba时,要更新备库上的密码文件。
2)将主库上的listener.ora和tnsnames.ora复制到备库上,修改listener.ora文件
主库上执行:
[oracle@primaryadmin]$scplistener.oratnsnames.oraoracle@192.168.62.202:/u01/app/oracle/product/11.2.0/dbhome_1/network/admin/
备库上执行:
[oracle@standby~]$cd/u01/app/oracle/product/11.2.0/dbhome_1/network/admin/
[oracle@standbyadmin]$vimlistener.ora
将host中的201 改成202,也就是将host改成备库机器上的IP,tnsnames.ora不用修改。
修改完成之后要重启监听
3)将备份复制到备库上
[oracle@primarybackup]$scp/u01/app/oracle/backup/*oracle@192.168.62.202:/u01/app/oracle/backup
4)在备库上创建对应的文件夹
[oracle@standbyoracle]$cd$ORACLE_BASE
[oracle@standbyoracle]$ls
archbackupcheckpointsdatabaseproduct
[oracle@standbyoracle]$mkdir -p oradata/orcl
[oracle@standbyoracle]$mkdir-padmin/orcl/adump
[oracle@standbyoracle]$mkdir-padmin/orcl/dpdump
[oracle@standbyoracle]$mkdir-padmin/orcl/pfile
5)启动备库到nomount模式
SQL>create spfile from pfile='/u01/app/oracle/product/11.2.0/dbhome_1/dbs/initorcl.ora';
SQL>startupnomount;
6)在备库上使用RMAN恢复数据库
[oracle@standby~]$rmantargetsys/asd@pri
RMAN>connectauxiliary/
run{
allocateauxiliarychannelc1devicetypedisk;
allocateauxiliarychannelc2devicetypedisk;
duplicatetargetdatabaseforstandbynofilenamecheckdorecover;
releasechannelc1;
releasechannelc2;
}
虽然最后出现了这个问题,但备库还是可以打开的。
RMAN-00571:===========================================================
RMAN-00569:===============ERRORMESSAGESTACKFOLLOWS===============
RMAN-00571:===========================================================
RMAN-03002:failureofDuplicateDbcommandat06/08/201617:38:36
RMAN-03015:erroroccurredinstoredscriptMemoryScript
RMAN-06053:unabletoperformmediarecoverybecauseofmissinglog
RMAN-06025:nobackupofarchivedlogforthread1withsequence16andstartingSCNof1010403foundtorestore
RMAN-06025:nobackupofarchivedlogforthread1withsequence15andstartingSCNof1009794foundtorestore
7)备库上添加standbylogfile
备库上添加standbylogfile,我做测试时,v$logfile视图里有standbylogfile group4 至group 7,而/u01/app/oracle/oradata/orcl并没有这些文件,所以要先把这几组日志删除再重建:
SQL>alterdatabaseopen;#未试过在mount状态创建standby日志
SQL>alterdatabasedroplogfilegroup4;
SQL>alterdatabasedroplogfilegroup5;
SQL>alterdatabasedroplogfilegroup6;
SQL>alterdatabasedroplogfilegroup7;
SQL>alterdatabaseaddstandbylogfilegroup4('/u01/app/oracle/oradata/orcl/redo_dg_021.log')size50M;
SQL>alterdatabaseaddstandbylogfilegroup5('/u01/app/oracle/oradata/orcl/redo_dg_022.log')size50M;
SQL>alterdatabaseaddstandbylogfilegroup6('/u01/app/oracle/oradata/orcl/redo_dg_023.log')size50M;
SQL>alterdatabaseaddstandbylogfilegroup7('/u01/app/oracle/oradata/orcl/redo_dg_024.log')size50M;
8)应用归档
--应用归档
SQL>shutdown immediate;
SQL>startup mount;
ALTERDATABASERECOVERMANAGEDSTANDBYDATABASEUSINGCURRENTLOGFILEDISCONNECTFROMSESSION;
--退出归档
alter database recover managed standby database cancel;
9)查看状态
SQL>SELECTSEQUENCE#,FIRST_TIME,NEXT_TIME,APPLIEDFROMV$ARCHIVED_LOGORDERBYSEQUENCE#;
SEQUENCE#FIRST_TIMENEXT_TIMEAPPLIED
-------------------------------------------------------
1508-JUN-1608-JUN-16YES
1608-JUN-1608-JUN-16YES
1708-JUN-1608-JUN-16YES
1808-JUN-1608-JUN-16YES
1908-JUN-1608-JUN-16YES
2008-JUN-1608-JUN-16YES
2108-JUN-1608-JUN-16IN-MEMORY
说明DG已经搭建成功
注:如果备库处于open状态,告警日志中会出现:ORA-16058:未装载备用数据库实例的告警信息。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。