DBSRV1 192.168.229.201 SID data
DBSRV2 192.168.229.202 SID data
准备环境
首先在主库和备库系统上安装Oracle11gR2,其中备库仅安装数据库软件。在CentOS上安装Oracle见这篇博文。
1. 主库配置
a) 必须是强制日志和归档模式
SQL> startup mount;
SQL> alter database force logging;
SQL> alter database archivelog;
SQL> archive log list
Database log mode Archive Mode
Automatic archival Enabled
Archive destination /u01/app/oracle/oradata/data/archive/
Oldest online log sequence 87
Next log sequence to archive 89
Current log sequence 89
b) 创建备用重做日志组
Standby redo log是使用Real Time Apply必须的条件
--Oracle文档要求standby logfile的数量至少比online logfile多一组. 所以我们在此创建4组standby logfile.
SQL> alter database add standby logfile group 4 ('/u01/app/oracle/oradata/data/stan04.log') size 10m;
SQL> alter database add standby logfile group 5 ('/u01/app/oracle/oradata/data/stan05.log') size 10m;
SQL> alter database add standby logfile group 6 ('/u01/app/oracle/oradata/data/stan06.log') size 10m;
SQL> alter database add standby logfile group 7 ('/u01/app/oracle/oradata/data/stan07.log') size 10m;
c) 修改主库初始化参数文件 (这里把主库备库用的配置文件放到一起了,方便记录)
SQL> create pfile='/tmp/initora.ora' from spfile;
data.__db_cache_size=838860800
data.__java_pool_size=16777216
data.__large_pool_size=16777216
data.__oracle_base='/u01/app/oracle'#ORACLE_BASE set from environment
data.__pga_aggregate_target=805306368
data.__sga_target=1191182336
data.__shared_io_pool_size=0
data.__shared_pool_size=301989888
data.__streams_pool_size=0
*.audit_file_dest='/u01/app/oracle/admin/data/adump'
*.audit_trail='db'
*.compatible='11.2.0.0.0'
*.control_files='/u01/app/oracle/oradata/data/control01.ctl','/u01/app/oracle/flash_recovery_area/data/control02.ctl'
*.db_block_size=8192
*.db_domain=''
*.db_name='data'
*.db_recovery_file_dest='/u01/app/oracle/flash_recovery_area'
*.db_recovery_file_dest_size=4070572032
*.diagnostic_dest='/u01/app/oracle'
*.dispatchers='(PROTOCOL=TCP) (SERVICE=dataXDB)'
*.memory_target=1994391552
*.open_cursors=300
*.processes=100
*.remote_login_passwordfile='EXCLUSIVE'
*.undo_tablespace='UNDOTBS1'
# -------------------------------------------------------------------------------------------
# 下面是增加的部分,主库使用
*.db_unique_name='dbsrv1'
*.archive_lag_target=1800
*.fal_client='dbsrv1'
*.fal_server='dbsrv2'
*.log_archive_config='DG_CONFIG=(dbsrv1,dbsrv2)'
*.log_archive_dest_1='location=/u01/app/oracle/oradata/data/archive/ VALID_FOR=(all_logfiles,all_roles) db_unique_name=dbsrv1'
*.log_archive_dest_2='service=dbsrv2 lgwr async valid_for=(online_logfiles,primary_role) db_unique_name=dbsrv2'
*.log_archive_dest_state_1='enable'
*.log_archive_dest_state_2='enable'
*.log_archive_format='%t_%s_%r.dbf'
*.standby_file_management='auto'
*.db_file_name_convert='/u01/app/oracle/oradata/data/','/u01/app/oracle/oradata/data/'
*.log_file_name_convert='/u01/app/oracle/oradata/data/','/u01/app/oracle/oradata/data/'
# -------------------------------------------------------------------------------------------
# 下面是增加的部分,备库库使用
*.db_unique_name='dbsrv2'
*.archive_lag_target=1800
*.fal_client='dbsrv2'
*.fal_server='dbsrv1'
*.log_archive_config='DG_CONFIG=(dbsrv1,dbsrv2)'
*.log_archive_dest_1='location=/u01/app/oracle/oradata/data/archive/ VALID_FOR=(all_logfiles,all_roles) db_unique_name=dbsrv2'
*.log_archive_dest_2='service=dbsrv1 lgwr async valid_for=(online_logfiles,primary_role) db_unique_name=dbsrv1'
*.log_archive_dest_state_1='enable'
*.log_archive_dest_state_2='enable'
*.log_archive_format='%t_%s_%r.dbf'
*.standby_file_management='auto'
*.db_file_name_convert='/u01/app/oracle/oradata/data/','/u01/app/oracle/oradata/data/'
*.log_file_name_convert='/u01/app/oracle/oradata/data/','/u01/app/oracle/oradata/data/
d) 用编辑过的配置文件重新启动主库
SQL> startup pfile='/tmp/initora.ora' nomount;
SQL> create spfile from pfile='/tmp/initora.ora';
SQL> shutdown immediate;
SQL> startup;
e) 主库创建密码文件
密码文件路径是 $ORACLE_HOME/dbs/orapwSID,默认安装数据库的时候就已经建立。如果没有建立,可以手工建立一个
$ orapwd file=orapwdata password=888888 entries=3
f) 监听配置,TNS配置 见这篇博文。
2. 备库配置
a) 创建必要的目录,需要数据文件,控制文件,日志文件,审计文件的目录。这里的路径信息和主库一致。
$ mkdir -p /u01/app/oracle/admin/data/adump
$ mkdir -p /u01/app/oracle/flash_recovery_area/data/
$ mkdir -p /u01/app/oracle/oradata/data/archive/
b) 备库密码文件
最好是直接把主库的这个orapwdata文件直接scp过来。如果手工建立,需要保证密码和主库的一致。
c) 初始化参数文件
参见前面的主库参数文件,仅需要备库的配置部分。
d) 配置监听,TNS配置
直接把主库的文件scp过来,修改监听地址即可。
e) 用编辑过的配置文件重新启动
SQL> startup pfile='/tmp/initora.ora' nomount;
SQL> create spfile from pfile='/tmp/initora.ora';
SQL> shutdown immediate;
SQL> startup nomount;
3. 初始化备库
可以使用全手工拷贝文件,也可以使用RMAN完成。
*************************************************************************
RMAN初始化备库 (推荐),在主库启动RMAN,连接到备库
$ rman target / auxiliary sys/888888@dbsrv2
RMAN> duplicate target database for standby from active database dorecover nofilenamecheck;
*************************************************************************
全手工初始化备库
手工拷贝文件的情况下,还需要在主库生成一个控制文件
SQL> startup mount
SQL> alter database create standby controlfile as '/tmp/standby.ctl';
复制所有的数据文件,日志文件,控制文件$ cd $ORACLE_HOME
$ scp -r admin cfgtoollogs diag flash_recovery_area oradata [email protected]:/u01/app/oracle/
本测试案例中,控制文件需要拷贝到oradata/data和flash_recovery_area/data下面,并且分别重命名control01.ctl,control02.ctl
4. 启动备库应用主库的日志
SQL> alter database mount standby database; /* RMAN方式不需要执行这句*/
SQL> alter database open read only;
Redo Apply
SQL> alter database recover managed standby database disconnect from session;
Real Time Apply
SQL> alter database recover managed standby database using current logfile disconnect from session;
Stop Apply
SQL> alter database recover managed standby database cancel;
5. 主备切换
主库
$ lsnrctl stop
SQL> alter database commit to switchover to physical standby with session shutdown;
SQL> shutdown immediate;
SQL> startup mount;
SQL> alter database open read only;
SQL> alter database recover managed standby database disconnect using current logfile ;
$ lsnrctl start
备库
SQL> alter database commit to switchover to primary;
SQL> shutdown immediate;
SQL> startup
6. 故障切换
检查归档文件是否连续(主库如果可以启动)
SQL> select thread#, low_sequence#, high_sequence# from v$archive_gap;
如果返回的有记录,按照列出的记录号复制对应的归档文件到待转换的 standby 服务器。这一步非常重
要,必须确保所有已生成的归档文件均已存在于 standby 服务器,不然可能会数据不一致造成转换时报错。
文件复制之后,通过下列命令将其加入数据字典:
SQL> alter database register physical logfile 'filespec1';
启动 failover
SQL> alter database recover managed standby database finish force;
SQL> alter database commit to switchover to primary;
7. 常用查询语句
-- run on Primary check the apply gap
select s.db_unique_name,
s.type,
s.database_mode,
s.recovery_mode,
s.protection_mode,
s.archived_seq#,
s.applied_seq#,
s.gap_status
from v$archive_dest_status s
where s.status = upper('valid')
order by s.dest_id;