环境描述
操作系统:RHEL Linux 7.4(64位)
数据库版本:Oracle 11g R2 11.2.0.4 (64位)单实例
存储方式:文件系统
主数据库和备数据库目录一致(安装路径都在/u01/app/oracle下)、SID相同
主库IP地址:20.4.1.81
备库IP地址:20.4.1.79
一、环境配置
1、配置主备库tnsname.ora文件
在主库上编辑tnsname.ora文件,然后拷贝到备库上。
[oracle@pri ~]$ cat /u01/app/oracle/product/11.2.0/db/network/admin/tnsnames.ora
# tnsnames.ora Network Configuration File: /u01/app/oracle/product/11.2.0/db/network/admin/tnsnames.ora
# Generated by Oracle configuration tools.
ORCL =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 20.4.1.81)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = orcl)
)
)
#连接到主库的tns
ORCL_PRI =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 20.4.1.81)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = orcl)
)
)
#连接到备库的tns
ORCL_STD =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 20.4.1.79)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = orcl)
)
)
配置好之后,将该tnsname.ora拷贝的standby备库上。
验证一下tnsping,和使用sqlplus登陆一下,连接是通的
[oracle@pri ~]$ tnsping orcl_pri
[oracle@pri ~]$ tnsping orcl_std
2、配置主备库的listener.ora文件(如果不做switch over就不需要做)
在备库添加SID_LIST_LISTENER内容:
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(GLOBAL_DBNAME = orcl_pri)
(ORACLE_HOME = /u01/app/oracle/product/11.2.0/dbhome_1)
(SID_NAME = orcl)
)
)
添加这个的时候,需要注意的是SID_LIST_LISTENER必须定格写,不然会报错。
在备库的时候也需要添加如下内容:
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(GLOBAL_DBNAME = orcl_std)
(ORACLE_HOME = /u01/app/oracle/product/11.2.0/dbhome_1)
(SID_NAME = orcl)
)
)
3、主数据库改为强制日志模式并开启归档
将主数据库改为强制日志模式(此步骤只在主库上做)
SQL> alter database force logging;
创建归档日志目录并赋权限
mkdir /u01/app/arch
chown oracle:oinstall /u01/app/arch
SQL> alter system set log_archive_dest_1='location=/u01/app/arch/' scope=spfile;
修改主库为归档模式:
SQL> archive log list;
SQL> shutdown immediate
SQL> startup mount
SQL> alter database archivelog;
SQL> alter database open;
4、创建密码文件(此步骤只在主库上做)
注意:两端分别创建自己的密码文件好像有问题,备库的密码文件需要跟主库一致, 否则导致日志传输不到备库,有待验证。我最后是将主库的密码文件直接copy 到备库,重命名后使用。
[oracle@pri ~]$ cd $ORACLE_HOME/dbs
[oracle@pri dbs]$ orapwd file=orapwpri password=OraAdmin_HisDB force=y
#这条命令可以手动生成密码文件,force=y 的意思是强制覆盖当前已有的密码文件,给主库添加密码文件,如果主库的密码文件没有的话,需要这样添加。
将主库的密码文件copy 给备库,并重命名:
[oracle@pri dbs]$ scp orapworcl 10.6.1.220:$ORACLE_HOME/dbs/orapworcl
5、主备库参数文件修改
(1)在主库创建 pfile 文件并修改 pfile内容
SQL> create pfile from spfile;
修改主库参数文件
$ cd $ORACLE_HOME/dbs
$ vi initpri.ora
--------------------------------
#添加一下内容
*.db_unique_name=orcl_pri
*.log_archive_config='dg_config=(orcl_pri,orcl_std)'
*.log_archive_dest_1='location=/u01/app/arch valid_for=(all_logfiles,all_roles) db_unique_name=orcl_pri'
*.log_archive_dest_2='service=orcl_std ASYNC NOAFFIRM valid_for=(online_logfiles,primary_role) db_unique_name=orcl_std'
*.log_archive_dest_state_1=enable
*.log_archive_dest_state_2=enable
*.log_archive_max_processes=30
*.fal_server='orcl_std'
*.fal_client='orcl_pri'
*.log_file_name_convert='/u01/app/oracle/oradata/orcl','/u01/app/oracle/oradata/orcl'
*.db_file_name_convert='/u01/app/oracle/oradata/orcl','/u01/app/oracle/oradata/orcl'
*.log_archive_format='%t_%s_%r.dbf'
*.standby_file_management='auto'
(2)在备库端,修改pfile 参数文件(只在备库端做)
将主库导出的参数文件传到备库,然后在备库端进行修改:
$ scp initorcl.ora 10.6.1.220:$ORACLE_HOME/dbs/initstd.ora
$ cd $ORACLE_HOME/dbs
$ cd $ORACLE_HOME/dbs
$ vi initstd.ora
---------------------------------
#添加一下内容
*.db_unique_name=orcl_std
*.log_archive_config='dg_config=(orcl_pri,orcl_std)'
*.log_archive_dest_1='location=/u01/app/arch valid_for=(all_logfiles,all_roles) db_unique_name=orcl_std'
*.log_archive_dest_2='service=orcl_pri ASYNC NOAFFIRM valid_for=(online_logfiles,primary_role) db_unique_name=orcl_pri'
*.log_archive_dest_state_1=enable
*.log_archive_dest_state_2=enable
*.log_archive_max_processes=30
*.fal_server='orcl_pri'
*.fal_client='orcl_std'
*.log_file_name_convert='/u01/app/oracle/oradata/orcl','/u01/app/oracle/oradata/orcl'
*.db_file_name_convert='/u01/app/oracle/oradata/orcl','/u01/app/oracle/oradata/orcl'
*.log_archive_format='%t_%s_%r.dbf'
*.standby_file_management='auto'
6、在备库上补建参数文件中的相关文件夹
因为我们的备库没有创建实例,所以是没有相关的目录的,我们需要参考主库的位置来创建。
将主库oracle目录下的admin,cfgtollogs,diag,flash_recover_area目录拷贝到备用库的相同路径,备库已经有的文件和文件夹直接覆盖掉吧。
归档日志文件存放目录:/u01/app/arch
审计文件目录:/u01/app/oracle/admin/orcl/adump
数据文件目录:/u01/app/oracle/oradata
--FRA目录
mkdir -p /u01/app/oracle/fast_recovery_area/orcl
--DATAFILE
mkdir -p /u01/app/oracle/oradata/orcl
--adump
mkdir -p /u01/app/oracle/admin/orcl/adump
--归档日志
mkdir -p /u01/app/arch/
7、使用新参数文件建立主备库实例
SQL> startup nomount pfile='$ORACLE_HOME/dbs/initorcl.ora';
SQL> create spfile from pfile='$ORACLE_HOME/dbs/initorcl.ora';
或者: SQL> create spfile from pfile;
SQL> shutdown immediate;
主库启动
SQL> startup;
备库启动到nomount
SQL> startup nomount;
二、数据同步
1、在主库备份数据文件
RMAN> backup database format='/backup/data_%d_%s.dbf';
并将备份文件拷贝到备库相同的目录/backup下
scp /backup/data* oracle@10.6.1.220:/backup
2、在主库备份控制文件
RMAN> backup current controlfile for standby format '/backup/control_%T_%s.bak';
拷贝备份文件在备库恢复控制文件
scp /backup/control_* oracle@10.6.1.220:/backup
3、在备库恢复控制文件,启动到mount
备库启动到nomount状态,开始恢复控制文件
SQL> startup nomount;
RMAN> restore standby controlfile from '/backup/control_%T_%s.bak';
#将备库启动到mount状态下
SQL> alter database mount standby database;
4、在备库利用rman恢复出备用库
rman target /
RMAN> catalog start with '/backup';
RMAN> restore database;
RMAN> recover database;
5、由于从Primary数据库复制文件时并没有复制Online Redologs,因此需将主库的重做日志文件redo.log联机状态拷至备库
SYS@pri> select group#,member from v$logfile;
GROUP# MEMBER
---------- --------------------------------------------------
3 /u01/app/oracle/oradata/orcl/redo03.log
2 /u01/app/oracle/oradata/orcl/redo02.log
1 /u01/app/oracle/oradata/orcl/redo01.log
6、在备库需手动创建备库重做日志
SQL> select group#,sequence#,status, bytes/1024/1024 from v$standby_log;
创建备库重做日志:
alter database add standby logfile group 11 ('/u01/app/oracle/oradata/orcl/redo11_std.log') size 500M;
alter database add standby logfile group 12 ('/u01/app/oracle/oradata/orcl/redo12_std.log') size 500M;
alter database add standby logfile group 13 ('/u01/app/oracle/oradata/orcl/redo13_std.log') size 500M;
alter database add standby logfile group 14 ('/u01/app/oracle/oradata/orcl/redo14_std.log') size 500M;
如果已经存在,则先删除在重建,否则报错:ORA-19527: physical standby redo log must be renamed。
SQL> alter database drop logfile group 11;
SQL> alter database drop logfile group 12;
SQL> alter database drop logfile group 13;
SQL> alter database drop logfile group 14;
7、将备库启动至日志应用模式下
SQL> alter database recover managed standby database disconnect from session;
# 查看应用状态,并在主库切换日志,查看序列变化
select max(sequence#),applied,archived from v$archived_log group by applied,archived;
开启物理备库的实时redo应用:
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 recover managed standby database cancel;
SQL> alter database recover managed standby database disconnect from session using current logfile;
8、主备库配置归档日志删除策略
#主库
CONFIGURE ARCHIVELOG DELETION POLICY TO APPLIED ON STANDBY;
这里有个警告, 解决方法,执行如下命令:
SQL> alter system set "_log_deletion_policy"=ALL scope=spfile sid='*';
#备库
RMAN> CONFIGURE ARCHIVELOG DELETION POLICY TO NONE;