Create a Physical standby DB on Oracle 11gR2 step by step

搭建一个物理的standby详解

DataGurd除了了备份恢复,还可用于读写分离应用;

下面详细介绍物理standby的搭建;
0.应用环境:

   OS:RHEL5.2  DB:11gR2.0.2
  数据库  IP       DB_NAME   DB_UNIQUE_NAME    Oracle Net Service Name
  主      1.1.1.1    alex                alex                      alex
  备      1.1.1.2    alex                 alexstb                alexstb


1.主库配置

1)强制日志功能      --This statement can take a considerable amount of time to complete, because it waits for all unlogged direct write I/O to finish
alter database force logging;
select force_logging from v$database;   -- 验证是否为YES

2)创建口令文件

  1) 关闭强密码认证。
在主库和备库上都去执行:  ---11G在密码文件中启用了强密码认证方式,这特性要求主库和备库的密码要求完全一致
ALTER SYSTEM SET SEC_CASE_SENSITIVE_LOGON=FALSE SCOPE=BOTH;            --否则会报错,returning error ORA-16191.
在主库和备库上创建相同密码的密码文件:
orapwd file=<fname> entries=<users> force=<y/n> ignorecase=<y/n> nosysdba=<y/n>
2) 拷贝主库的密码文件到备库上,更改文件名。

orapwd file=$ORACLE_HOME/dbs/orapw$ORACLE_SID password=oracle entries=10
ls -l $ORACLE_HOME/dbs/orapw*    #检查密码文件
sqlplus sys/oracle@alex as sysdba

3)在主库上创建备用日志——切换时使用
ALTER DATABASE ADD STANDBY LOGFILE
group 4 ('/u01/app/oracle/oradata/alex/stbredo01.log') size 50M,
group 5 ('/u01/app/oracle/oradata/alex/stbredo02.log') size 50M,
group 6 ('/u01/app/oracle/oradata/alex/stbredo03.log') size 50M;

select * from v$log; -- 查看日志组的信息
select * from v$logfile; -- 查看日志组成员的信息。

4)编制主库初始化参数
create pfile='/home/oracle/pfile.ora' from spfile;
cp pfile.ora pfile.stb
cp pfile.ora pfile.org
添加如下行:
*.db_unique_name=alex
*.log_archive_config='dg_config=(alex,alexstb)'
*.log_archive_dest_1='location=/u01/app/oracle/oradata/alex/archive valid_for=(all_logfiles,all_roles) db_unique_name=alex'
*.log_archive_dest_2='service=alexstb async valid_for=(all_logfiles,primary_role)  db_unique_name=alexstb'
*.log_archive_dest_state_2=defer
*.log_archive_format=%t_%s_%r.arc
*.log_archive_max_processes=30
*.remote_login_passwordfile=exclusive

*.fal_client='alex'
*.fal_server='alexstb'
*.standby_file_management=auto
*.log_file_name_convert=("/u01/app/oracle/oradata/alexstb","/u01/app/oracle/oradata/alex")
*.db_file_name_convert=("/u01/app/oracle/oradata/alexstb","/u01/app/oracle/oradata/alex")
*.archive_lag_target=1800   #可选
建议使用spfile,这样可以动态的修改参数;
startup nomount pfile='/home/oracle/pfile.ora'     ---check:select * from V$DATAGUARD_CONFIG;
create spfile from pfile='/home/oracle/pfile.ora';
shutdown immediate

5)设置归档
shutdown immediate;
startup mount;
alter database archivelog;
alter database open;

6)创建备库所需的控制文件
startup nomount
alter database create standby controlfile as '/home/oracle/control01.ctl';
alter database open;


7)创建监听和服务器名文件

scp listener.ora tnsnames.ora  1.1.1.2:$ORACLE_HOME/network/admin/.   #copy to 备库

listener.ora:
  LISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = TCP)(HOST = study1)(PORT = 1521))
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
    )
  )

ADR_BASE_LISTENER = /u01/app/oracle

 tnsnames.ora:
alex =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = study1)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = alex)
    )
  )

 

2、备库设置
1)在备库创建口令文件
orapwd file=$ORACLE_HOME/dbs/orapw$ORACLE_SID password=oracle entries=10
ls -l $ORACLE_HOME/dbs/orapw*    #检查密码文件
sqlplus sys/oracle@alex as sysdba

2)创建相应的目录
mkdir -p /u01/app/oracle/admin/$ORACLE_SID/{a,b,c,u}dump
mkdir -p /u01/app/oracle/oradata/$ORACLE_SID/archive

2)创建Standby参数文件
*.audit_file_dest='/u01/app/oracle/admin/alexstb/adump'    #注意修改audit路径
*.db_unique_name=alexstb
*.log_archive_config='dg_config=(alex,alexstb)'
*.control_files='/u01/app/oracle/oradata/alexstb/control01.ctl', '/u01/app/oracle/oradata/alexstb/control02.ctl'
*.log_archive_dest_1='location=/u01/app/oracle/oradata/alexstb/archive valid_for=(all_logfiles,all_roles) db_unique_name=alexstb'
*.log_archive_dest_2='service=alex async valid_for=(all_logfiles,primary_role)  db_unique_name=alex'
*.log_archive_dest_state_2=defer
*.log_archive_format=%t_%s_%r.arc
*.log_archive_max_processes=30

*.remote_login_passwordfile=exclusive
*.fal_client='alexstb'
*.fal_server='alex'
*.standby_file_management=auto
*.log_file_name_convert=("/u01/app/oracle/oradata/alex","/u01/app/oracle/oradata/alexstb")
*.db_file_name_convert=("/u01/app/oracle/oradata/alex","/u01/app/oracle/oradata/alexstb")
*.archive_lag_target=1800   #可选

select * from V$DATAGUARD_CONFIG; --检查配置

3)备份主库文件到备库:参数文件、控制文件、数据文件
scp 1.1.1.1:/home/oracle/pfile.stb $ORACLE_HOME/dbs/init$ORACLE_SID.ora
scp 1.1.1.1:/home/oracle/control01.ctl $ORACLE_BASE/oradata/$ORACLE_SID/.
cp $ORACLE_BASE/oradata/$ORACLE_SID/control01.ctl  $ORACLE_BASE/oradata/$ORACLE_SID/control02.ctl 
  1)数据文件冷备份
 
  2)使用RMAN
startup nomount
rman auxiliary /
connect target sys/oracle@alex
duplicate target database for standby dorecover nofilenamecheck;

4)启动备库、主库

 

5)检查状态  
SQL> select database_role,protection_mode,protection_level from v$database;   --主库

DATABASE_ROLE    PROTECTION_MODE      PROTECTION_LEVEL
---------------- -------------------- --------------------
PRIMARY          MAXIMUM PERFORMANCE  MAXIMUM PERFORMANCE

SQL> select database_role,protection_mode,protection_level from v$database;    ---备库

DATABASE_ROLE    PROTECTION_MODE      PROTECTION_LEVEL
---------------- -------------------- --------------------
PHYSICAL STANDBY MAXIMUM PERFORMANCE  MAXIMUM PERFORMANCE

SQL> select sequence# ,applied from v$archived_log order by sequence#;    --检查日志的应用情况

 SEQUENCE# APPLIED
---------- ---------
        33 NO
        33 YES

6)备库应用和关闭应用日志
alter database recover managed standby database disconnect from session;    --开启应用重做

alter database recover managed standby database using current logfile;       --开启实时应用重做
alter database recover managed standby database using current logfile disconnect from session 
--实时应用是当日志传到standby redo log的时候就去应用,是real-time的,必须要有standby redo log.
alter database recover managed standby database cancel;                     --关闭应用重做

select switchover_status from v$database;                                  --查看切换状态

alter system switch logfile;                                             --手工切换日志
alter system archive log current;

1)多种模式切换
2)switch over  先要把主库拉下来,
3)fail over

关机:先关主库,再关备库
开机:先开备库,再开主库


 

你可能感兴趣的:(oracle,dataguard,Physical,11gr2)