oracle 11g dataguard搭建
主库ip 12.18.10.41
备库ip 12.18.10.42
主库unique_name dg1
备库unique_name dg2
查看是否为归档模式
SQL> archive log list
Database log mode Archive Mode
Automatic archival Enabled
Archive destination /data/arch/
Oldest online log sequence 5
Next log sequence to archive 8
Current log sequence 8
如果不是则修改为归档模式,将数据库启动到MOUNT状态,将数据库修改为归档模式后建数据库启动到OPEN状态。
SQL> shutdown immediate
SQL> startup mount
SQL> alter database archivelog;
SQL> alter database open;
SQL> alter database force logging; (强制产生日志)
修改主备库的hosts文件
12.18.10.41 dg1
12.18.10.42 dg2
修改主库参数:
SQL>create pfile='/backup/pfile.ora' from spfile;
SQL>alter system set LOG_ARCHIVE_CONFIG='DG_CONFIG=(dg1,dg2)';
SQL>alter system set LOG_ARCHIVE_DEST_1='LOCATION=/data/arch/ VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=dg1';
SQL>alter system set LOG_ARCHIVE_DEST_2='SERVICE=dg2 LGWR ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=dg2';
SQL>alter system set LOG_ARCHIVE_DEST_STATE_1=ENABLE;
SQL>alter system set LOG_ARCHIVE_DEST_STATE_2=ENABLE;
SQL>alter system set FAL_SERVER=dg2;
SQL>alter system set FAL_CLIENT=dg1;
SQL>alter system set STANDBY_FILE_MANAGEMENT=auto scope=spfile;
备份主库
[oracle@dg1 ~]$ rman target /
RMAN>backup full database format '/backup/backup_%T_%s_%p.bak';
RMAN>sql "alter system archive log current";
RMAN>backup archivelog all format='/backup/arch_%T_%s_%p.bak';
在主库上建立备库控制文件(控制文件通常需要有多份,手工将文件复制几份)
$sqlplus / as sysdba
SQL>alter database create standby controlfile as '/backup/stdby_control01.ctl';
把主库的密码文件拷贝到下面的目录下
[oracle@dg1 backup]$ cd $ORACLE_HOME
[oracle@dg1 dbhome_1]$ cd dbs
[oracle@dg1 dbs]$ cp orapwdg1 /backup/
[oracle@dg1 backup]$ scp * oracle@dg2:/backup/
#/backup 下应该有参数文件,密码文件,控制文件
备库
[oracle@dg2 backup]$ cp orapwdg1 $ORACLE_HOME/dbs/orapwdg2密码文件传到备库中,必须和备库的实例名一样(orapw实例名)
修改主备库的tnsnames.ora,备库如果没有的话从主库复制过去。tnsnames.ora在[oracle@dg1]$ cd $ORACLE_HOME/network/admin 下。 将HOST改为IP地址,要同时拥有dg1,dg2。
修改pfile.ora 文件。删除dg1开头的,保留*开头的。
.audit_file_dest='/u01/app/oracle/admin/dg2/adump'---修改路径,创建路径
*.audit_trail='db'
*.compatible='11.2.0.4.0'
*.control_files='/oradata/DG2/controlfile/o1_mf_c8wd9f41_.ctl'---修改路径,创建路径
*.db_block_size=8192
*.db_create_file_dest=''----设为空!!(*主库的这个值也要设为空)
*.db_domain=''
*.db_name='dg1'
*.diagnostic_dest='/u01/app/oracle'
*.dispatchers='(PROTOCOL=TCP) (SERVICE=dg1XDB)'
*.log_archive_dest_1='LOCATION=/oradata/arch'---修改路径,创建路径
*.log_archive_format='%t_%s_%r.dbf'
*.open_cursors=300
*.pga_aggregate_target=209715200
*.processes=1500
*.remote_login_passwordfile='EXCLUSIVE'
*.sessions=1655
*.sga_target=1073741824
*.undo_tablespace='UNDOTBS1'
~
启动到nomount状态,修改参数
SQL>startup nomount pfile='/backup/pfile.ora';
SQL>create spfile from pfile='/backup/pfile.ora';
SQL>alter system set db_unique_name=dg2 scope=spfile;
SQL>alter system set LOG_ARCHIVE_CONFIG='DG_CONFIG=(dg1,dg2)' scope=spfile;
SQL>alter system set LOG_ARCHIVE_DEST_1='LOCATION=/oradata/arch/ VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=dg2'scope=spfile;
SQL>alter system set LOG_ARCHIVE_DEST_2='SERVICE=dg1 LGWR ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=dg1' scope=spfile;
SQL>alter system set LOG_ARCHIVE_DEST_STATE_1=ENABLE scope=spfile;
SQL>alter system set LOG_ARCHIVE_DEST_STATE_2=ENABLE scope=spfile;
SQL>alter system set FAL_SERVER=dg1 scope=spfile;
SQL>alter system set FAL_CLIENT=dg2 scope=spfile;
*************************************************************************************************************
alter system set DB_FILE_NAME_CONVERT='/data/DG1/datafile/','/oradata/DG2/datafile/' scope=spfile; ----(前为主库存放数据文件的路径,后为备库存放数据文件的路径,如果主备库目录结构完全一样,则无需设定。)
alter system set LOG_FILE_NAME_CONVERT='/data/DG1/onlinelog/','/oradata/DG2/onlinelog/' scope=spfile; ---- (前为主库在线日志的路径,后为备库在线日志的路径,如果主备库目录结构完全一样,则无需设定)
主备切换之后在当前备库上设置这两个参数时路径要反过来
*************************************************************************************************************
SQL>alter system set STANDBY_FILE_MANAGEMENT=auto scope=spfile;
恢复控制文件:
RMAN> restore controlfile from'/backup/stdby_control01.ctl';
RMAN>alter database mount;
RMAN>run {
set newname for datafile 1 to '/oradata/DG2/datafile/o1_mf_system_bwocwhpc_.dbf'; ----首先创建/oradata/DG2/datafile路径
set newname for datafile 2 to '/oradata/DG2/datafile/o1_mf_sysaux_bwocwhs2_.dbf';
set newname for datafile 3 to '/oradata/DG2/datafile/o1_mf_undotbs1_bwocwht5_.dbf';
set newname for datafile 4 to '/oradata/DG2/datafile/o1_mf_users_bwocwhvw_.dbf';
set newname for datafile 5 to '/oradata/DG2/datafile/o1_mf_example_byx1qkr2_.dbf';
restore database;
switch datafile all;
}
在备库增加standby redolog组数是主库+1。查询语句SQL> select group#,members,bytes/1024/1024,status from v$log;(select group#,type,member from v$logfile;)
SQL>alter database add standby logfile -----首先创建/oradata/DG2/onlinelog/路径
group 5 ('/oradata/DG2/onlinelog/stdby_redo5.log') size 50m,
group 6 ('/oradata/DG2/onlinelog/stdby_redo6.log') size 50m,-------###特别注意standby redolog的大小要和主库redolog的大小一致!!!
group 7 ('/oradata/DG2/onlinelog/stdby_redo7.log') size 50m,
group 8 ('/oradata/DG2/onlinelog/stdby_redo8.log') size 50m,
group 9 ('/oradata/DG2/onlinelog/stdby_redo9.log') size 50m;
如果以后主库要切换成备库 就要在现在的主库上加上standbyredolog
SQL>alter database add standby logfile -----首先创建/oradata/dg1/onlinelog/路径
group 5 ('/data/DG1/onlinelog/stdby_redo5.log') size 50m,
group 6 ('/data/DG1/onlinelog/stdby_redo6.log') size 50m,
group 7 ('/data/DG1/onlinelog/stdby_redo7.log') size 50m,
group 8 ('/data/DG1/onlinelog/stdby_redo8.log') size 50m,
group 9 ('/data/DG1/onlinelog/stdby_redo9.log') size 50m;
启动备库
SQL >shutdown immediate
SQL >startup nomount;
SQL >alter database mount standby database;
SQL >alter database recover managed standby database using current logfile disconnect from session;
正常主备切换:
主库
SQL >alter database commit to switchover to physical standby;
SQL >shutdown immediate
SQL >startup nomount;
SQL >alter database mount standby database;
SQL >alter database recover managed standby database disconnect from session;
备库
SQL >alter database commit to switchover to primary;
如果此处报ORA-16139: media recovery required错误,可能是由于未应用日志引起,可先执行alter database recover managed standby database disconnect from session;
SQL >shutdown immediate;
SQL >startup
非正常切换:(即主服务器当机的情况)启动failover
备服务器
SQL >alter database recover managed standby database finish;
SQL >alter database commit to switchover to primary;
SQL >shutdown immediate;
SQL >startup;