1、序言为了学习DG,我们需要有学习环境来支持,所以搭建DG环境是学习DG第一步。在搭建GC文档里,有介绍虚拟机模板搭建,这里就直接用这个虚拟机模板来搭建DG,搭建之前先下载11201数据库软件,为了后面滚动升级试验。下面就来介绍怎么搭建DG学习环境!有什么问题能够联络我。作者:will QQ:第一章 制作DG虚拟机模板下载11201数据库软件点击接收,下载32bit或64bit软件本人下载是64bit软件把两个压缩包全部下载下来还原虚拟机模板把在搭建GC环境时虚拟机模板还原出来更名为prod装载虚拟机右键rename为prod开启虚拟机先登录进去cd /etc/sysconfig/network
2、-scriptslsvi ifcfg-eth0.bak把HWADDR删除掉,然后改一下IPADDR保留退出mv ifcfg-eth0.bak ifcfg-eth0lsservice network restartifconfigexit用secureCRT创建新连接这个提醒用一样方法处理设置好颜色字体,prod会话连接创建完成vi /etc/sysconfig/network把主机名改成prod.localdomain修改好以后保留退出hostname prod.localdomain退出后再登录修改hostsvi /etc/hosts127.0.0.1 localhost.localdoma
3、in localhost:1 localhost6.localdomain localhost6192.168.75.61 prod.localdomain prod192.168.75.62 standby.localdomain standby保留退出安装oracle软件先安装10201软件来搭建DG后面有滚动升级成11201方法把10201oracle软件传送到/u01目录下传输好以后修改软件属于者为oraclecd /u01ls -lrtchown -R oracle:oinstall *ls -lrt设置oracle环境变量su - oraclevi .bash_profile把下面
4、内容复制到.bash_profile最终export PATHexport TMP=/tmpexport TMPDIR=$TMPexport ORACLE_BASE=/u01export ORACLE_SID=prodexport ORACLE_HOME=/u01/oracle/db_1export ORACLE_TERM=xtermexport PATH=$PATH:$ORACLE_HOME/bin:$HOME/binexport LD_LIBRARY_PATH=$ORACLE_HOME/lib:/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin/expo
5、rt CLASSPATH=$ORACLE_HOME/JRE:$ORACLE_HOME/jlib:$ORACLE_HOME/rdbms/jlibexport DISPLAY=192.168.75.1:0.0stty erase halias sqlplus=rlwrap sqlplusalias rman=rlwrap rman保留后退出实施. .bash_profile让环境变量生效解压缩oracle软件cd /u01ls -lrtgzip -dc 10201_database_linux_x86_64.cpio.gz | cpio -icvdBumls -lrtcd database在win
6、dows端打开xmanagerpassive来接收远程图形双击它就能够了看到右下角有图标且是:0.0端口就行./runInstaller另开一个窗口用root用户实施上面脚本10201软件安装完成!把安装软件删除,节省空间cd .ls -lrtrm -f 10201_database_linux_x86_64.cpio.gzrm -fR databasels -lrtdf -h备份复制模板至此,完成了虚拟机模板制作,能够把虚拟机目录拷贝一份保留起来关机,然后复制一份作为备库使用第二章 搭建物理备库配置standby服务器把复制好虚拟机更名为standby装载虚拟机右键rename为standb
7、y打开虚拟机先登录进去cd /etc/sysconfig/network-scriptsls因为prod虚拟机没开,所以IP地址没有发生冲突,不过还是要改一下vi ifcfg-eth0改成192.168.75.62保留退出service network restartifconfigexit用secureCRT创建新连接这个提醒用一样方法处理设置好颜色字体,standby会话连接创建完成vi /etc/sysconfig/network把主机名改成standby.localdomain修改好以后保留退出hostname standby.localdomain退出后再登录查看hostscat /
8、etc/hosts一切正常!修改ORACLE_SIDsu - oraclevi .bash_profile把环境变量ORACLE_SID值修改成standby修改好以后保留退出. .bash_profile让环境变量生效创建主数据库prod开启prod虚拟机然后用netca命令配置监听su - oraclenetca这个就设置成默认监听,用1521端口看一下信息用dbca命令创建prod数据库要和.bash_profile里配置ORACLE_SID参数一样不安装DB Controlem设置sys用户密码不使用flash recovery area字符集选ZHS16GBK,当然你选AL32UTF
9、8也能够开始创建数据库解锁多个用户prod数据库创建完成!设置prod库登陆到sqlplus检验一下归档模式和force loggingsqlplus / as sysdbaselect log_mode,force_logging from v$database;假如是非归档就要关闭数据库修改shutdown immediatestartup mount洁净地关闭数据库再开启到mount状态改成归档模式和强制loggingalter database archivelog;alter database force logging;select log_mode,force_logging f
10、rom v$database;archive log list更改归档目录!mkdir /u01/oradata/prod/archivelogls -l /u01/oradata/prodexitalter system set log_archive_dest_1=location=/u01/oradata/prod/archivelog;archive log listalter database open;创建pfilecreate pfile from spfile;alter database create standby controlfile as /u01/oracle/st
11、andby.ctl;把pfile文件、口令文件和备控制文件传输到standbyexitcd /u01/oracle/db_1/dbsls -lrtscp orapwprod 192.168.75.62:/u01/oracle/db_1/dbsscp initprod.ora 192.168.75.62:/u01/oracle/db_1/dbsscp /u01/oracle/standby.ctl 192.168.75.62:/u01/oracle需要输入oracle用户密码备控制文件只要复制到和prod库路径相同就行,两边全部是/u01/oracle/standby.ctlprod设置先告一段
12、落设置standby数据库cd /u01/oracle/db_1/dbsls -lrt把口令文件和pfile名字改成standbymv orapwprod orapwstandbymv initprod.ora initstandby.oravi initstandby.ora把最上面几行去掉,然后把上面标注prod全部改成standby注意db_nameprod值不要修改在最终添加以下参数*.db_unique_name=standby*.db_file_name_convert=/u01/oradata/prod,/u01/oradata/standby*.log_file_name_co
13、nvert=/u01/oradata/prod,/u01/oradata/standby*.log_archive_config=dg_config=(prod,standby)*.log_archive_dest_1=location=/u01/oradata/standby/archivelog valid_for=(all_logfiles,all_roles) db_unique_name=standby*.log_archive_dest_2=service=prod lgwr async valid_for=(online_logfiles,primary_role) db_uni
14、que_name=prod*.log_archive_dest_state_1=enable*.log_archive_dest_state_2=enable*.log_archive_max_processes=10*.fal_server=prod*.fal_client=standby*.standby_file_management=auto保留退出创建部分目录mkdir -p /u01/admin/standby/adumpmkdir -p /u01/admin/standby/bdumpmkdir -p /u01/admin/standby/cdumpmkdir -p /u01/a
15、dmin/standby/udumpmkdir -p /u01/admin/standby/dpdumpls -l /u01/admin/standbymkdir -p /u01/oradata/standby/archivelogls -l /u01/oradata/standby配置standby库listener和静态服务名netmgr再添加静态服务名选择save network configuration,保留一下关闭lsnrctl start配置连接到prod网络服务名netcatnsping prod用pfile开启到nomount状态env | grep ORA登陆sqlplus
16、之前先检验一下环境变量sqlplus / as sysdbacreate spfile from pfile=?/dbs/initstandby.ora;startup nomountstandby库配置告一段落,重新回到prod库配置配置prod数据库再次回到prod库设置配置连接到standby网络服务名netca备库地址,备库监听用是1521端口tnsping standby配置prod库参数sqlplus / as sysdbaalter system set log_archive_config=dg_config=(prod,standby);alter system set lo
17、g_archive_dest_1=location=/u01/oradata/prod/archivelog valid_for=(all_logfiles,all_roles) db_unique_name=prod;alter system set log_archive_dest_2=service=standby lgwr async valid_for=(online_logfiles,primary_role) db_unique_name=standby;alter system set log_archive_dest_state_1=enable;alter system s
18、et log_archive_dest_state_2=enable;alter system set log_archive_max_processes=10;alter system set fal_server=standby;alter system set fal_client=prod;alter system set standby_file_management=auto;还有三需要重启才生效参数也设置一下alter system set db_unique_name=prodscope=spfile;alter system set db_file_name_convert=
19、/u01/oracle/standby,/u01/oracle/prod scope=spfile;alter system set log_file_name_convert=/u01/oracle/standby,/u01/oracle/prod scope=spfile;备份prod库exitmkdir -p /u01/backupsetls -l /u01创建了备份目录登陆到rman开始备份rman target /runallocate channel d1 type disk format /u01/backupset/%U;backup database;release chan
20、nel d1;把备份集目录传输到standby/u01目录下exitscp -r /u01/backupset 192.168.75.62:/u01能够在standby上检验一下ls -l /u01开始克隆standby库,在prod上运行rman target / auxiliary sys/oraclestandbyrunallocate auxiliary channel d1 type disk;duplicate target database for standby nofilenamecheck;release channel d1;还原了standby库!配置standby数据
21、库查询standby库状态select status from v$instance;现在数据库为mount状态创建备库standby日志alter database add standby logfile (/u01/oradata/standby/standby01.log) size 50m;alter database add standby logfile (/u01/oradata/standby/standby02.log) size 50m;alter database add standby logfile (/u01/oradata/standby/standby03.lo
22、g) size 50m;alter database add standby logfile (/u01/oradata/standby/standby04.log) size 50m;日志大小和主库一样全部是50M,数量比主库多一组在prod库切换多个日志exitsqlplus / as sysdbaalter system switch logfile;在standby库查看一下是否有归档日志了exitls -l /u01/oradata/standby/archivelog日志能够传输了就ok!备库实施日志应用同时语句,开始运行物理DGsqlplus / as sysdbaalter d
23、atabase recover managed standby database using current logfile disconnect from session;因为有备重做日志,所以能够加using current logfile语句,实现实时应用select sequence#, applied from v$archived_log where applied=YES order by sequence#;yes说明日志同时了在prod库上实施:conn scott/tigercreate table t(i int);insert into t values(1);comm
24、it;conn / as sysdbaalter system switch logfile;在standby库上实施:alter database recover managed standby database cancel;alter database open;conn scott/tigerselect * from t;数据已经同时,物理DG搭建成功!看一下数据库open状态conn / as sysdbaselect open_mode from v$database;注意:物理备库open是默认read only模式alter database recover managed
25、standby database using current logfile disconnect from session;select open_mode from v$database;开启数据同时语句后,数据库又变为mounted状态注意:在测试数据同时时候请不要用sys用户,不然可能同时不了,这里测试全部是用scott用户切换物理备库在prod库上实施:select switchover_status from v$database;alter database commit to switchover to physical standby;这个命令要实施蛮长时间,请耐心等候注意:
26、上面switchover_status值假如是TO STANDBY,能够直接switchover,假如是sessions active,则需要在switchover命令后面加上with session shutdown,比如alter database commit to switchover to physical standby with session shutdown;select status from v$instance;检验一下数据库状态shutdown immediatestartup nomountalter database mount standby database;
27、这条命令就是把主库切成备库在standby库上实施:select sequence#, applied from v$archived_log where applied=YES order by sequence#;已经和刚才主库同时完日志select switchover_status from v$database;alter database commit to switchover to primary;这条命令就是把备库切成主库select status from v$instance;查看一下数据库状态shutdown immediatestartup在prod库上实施:创建备日
28、志alter database add standby logfile (/u01/oradata/prod/standby01.log) size 50m;alter database add standby logfile (/u01/oradata/prod/standby02.log) size 50m;alter database add standby logfile (/u01/oradata/prod/standby03.log) size 50m;alter database add standby logfile (/u01/oradata/prod/standby04.l
29、og) size 50m;实施同时语句alter database recover managed standby database using current logfile disconnect from session;select sequence#, applied from v$archived_log where applied=YESorder by sequence#;在standby库上实施:conn scott/tigerinsert into t values(2);commit;conn / as sysdbaalter system switch logfile;在
30、prod库上实施:alter database recover managed standby database cancel;alter database open;conn scott/tigerselect * from t;物理DG切换成功!conn / as sysdbaselect open_mode from v$database;alter database recover managed standby database using current logfile disconnect from session;select open_mode from v$database
31、;恢复成同时模式再切换一次,切换来原来状态,步骤是一样这里只是为了验证是否ok在standby库上实施:select switchover_status from v$database;alter database commit to switchover to physical standby;shutdown immediatestartup nomountalter database mount standby database;在prod库上实施:select sequence#, applied from v$archived_log where applied=YES order
32、by sequence#;select switchover_status from v$database;alter database commit to switchover to primary;select status from v$instance;查看一下数据库状态shutdown immediatestartup在standby库上实施:alter database recover managed standby database using current logfile disconnect from session;在prod库上实施:conn scott/tigerin
33、sert into t values(3);commit;conn / as sysdbaalter system switch logfile;在standby库上实施:alter database recover managed standby database cancel;alter database open;conn scott/tigerselect * from t;conn / as sysdbaalter database recover managed standby database using current logfile disconnect from sessi
34、on;select open_mode from v$database;恢复到了原来状态,即prod是主库,standby是备库第三章 搭建逻辑备库配置逻辑备库 逻辑备库是在物理备库基础上配置,所以要搭建逻辑备库先要创建好物理备库才行在standby库上实施:停止备库applyalter database recover managed standby database cancel;注意:一定要先停止日志应用,不然下面build时候会有问题在prod库上实施:创建prod库作为逻辑备库时在线日志归档路径exitmkdir -p /u01/oradata/prod/lgarchivels -l
35、 /u01/oradata/prod修改log_archive_dest_3参数,当prod切换成备库时起作用,即在线日志归档路径sqlplus / as sysdbaalter system set log_archive_dest_3=location=/u01/oradata/prod/lgarchive valid_for=(standby_logfiles,standby_role) db_unique_name=prod;alter system set log_archive_dest_state_3=enable; 然后实施execute dbms_logstdby.build
36、;在standby库上实施:创建standby库作为逻辑备库时在线日志归档路径exitmkdir -p /u01/oradata/standby/lgarchivels -l /u01/oradata/standbysqlplus / as sysdbaalter system set log_archive_dest_3=location=/u01/oradata/standby/lgarchive valid_for=(standby_logfiles,standby_role) db_unique_name=standby;alter system set log_archive_des
37、t_state_3=enable; 检验standby库临时表空间数据文件select * from v$tempfile;有临时文件信息,假如没有,要添加临时文件alter tablespace temp add tempfile xxxxxx.select status from v$instance;查看一下数据库角色select database_role from v$database;startup mount forcealter database recover to logical standby standby;把备库转换成逻辑备库,后一个standby是备库名select
38、 status from v$instance;startup mount forceselect database_role from v$database;alter database open resetlogs;show parameter db_name此时备库db_name已经改成standby是一个新库了稍等片刻后在prod库上实施:alter system switch logfile;查看prod库alert信息查看standbyalert信息在standby库上实施:查看归档日志是否传输到备库exitls -l /u01/oradata/standby/archivelog
39、启用实时日志应用sqlplus / as sysdbaalter database start logical standby apply immediate;此时standby库alert日志里会产生大量信息prod库alert日志信息:配置完成,开始验证在prod库上实施:conn scott/tigercreate table a(a varchar2(10);insert into a values(a);commit;在standby库上实施:conn scott/tigerselect * from a;逻辑备库配置成功!切换逻辑备库 先把prod库和standby库undo_re
40、tention参数全部设置成3600conn / as sysdbaalter system set undo_retention=3600;开始切换逻辑备库在prod库上实施:select switchover_status from v$database;alter database prepare to switchover to logical standby;select switchover_status from v$database;在standby库上实施:select switchover_status from v$database;alter database prep
41、are to switchover to primary;select switchover_status from v$database;在prod库上实施:这个时候观察prod库switchover_status会发觉select switchover_status from v$database;假如上述查询返回结果不是TO LOGICAL STANDBY 话,你可能就需要取消此次转换,检验原因,然后再重新操作了取消转换命令为:alter database prepare to switchover cancel;prod库继续实施alter database commit to swi
42、tchover to logical standby;在standby库上实施:再检验standby库switchover_status会发觉select switchover_status from v$database;alter database commit to switchover to primary;在prod库上实施:alter database start logical standby apply immediate;在standby库上实施:conn scott/tigerinsert into a values(b);commit;conn / as sysdba在p
43、rod库上实施:conn scott/tigerselect * from a;conn / as sysdba逻辑备库切换成功!再切换回去在standby库上实施:select switchover_status from v$database;alter database prepare to switchover to logical standby;select switchover_status from v$database;在prod库上实施:select switchover_status from v$database;alter database prepare to sw
44、itchover to primary;select switchover_status from v$database;在standby库上实施:select switchover_status from v$database;alter database commit to switchover to logical standby;select switchover_status from v$database;在prod库上实施:select switchover_status from v$database;alter database commit to switchover to primary;select switchover_status from v$database;在standby库上