借鉴了 很多资料,结合自己实践操作形成了如下文档,做个记录:
一、准备工作
1、数据库软件:Oracle 11g r1 (企业版)主/从库:安装服务端、客户端、PLSQL
2、操作系统:WINDOWS SERVER 2008 ENTERPRISE EDTION 64X
3、环境
主库主机:
IP:192.168.10.20
数据库安装路径:E:\app\Administrator\oradata
创建文件夹:E:\app\interlib\log 根据此路径创建没有的文件夹
SID:obpm(安装Oracle服务端时顺便安装数据库)
从库主机:
IP:192.168.10.21
数据库安装路径:E:\app\Administrator\oradata
创建文件夹:E:\app\interlib\log 根据此路径创建没有的文件夹
SID:obpm(安装Oracle服务端请选择不要数据库,SID由cmd命令创建)
二、主库配置
1、用NCA(Net Configuration Assistant) 创建监听程序。(按照提示‘下一步’即可)也可以参照《Oracle系列之服务端安装》
2、命令操作:
进入dos界面开始命令操作:
转换到sqlplus.exe 所在的路径:
cd E:\app\Administrator\product\11.2.0\dbhome_1\BIN
进入sqlplus环境:
sqlplus / as sysdba
shutdown immediate;
startup mount;
修改为归档模式:
alter database archivelog;
alter database open;
设置强制归档模式:
alter database force logging;
为备库创建日志文件:
alter database add standby logfile group 4('E:\app\interlib\log\STAN04.LOG') SIZE 50m;
alter database add standby logfile group 5('E:\app\interlib\log\STAN05.LOG') SIZE 50m;
alter database add standby logfile group 6('E:\app\interlib\log\STAN06.LOG') SIZE 50m;
为备库创建控制文件:
alter database create standby controlfile as 'E:\app\interlib\standby.ctl';
导出当前数据参数文件并增加内容:
create pfile='E:\app\interlib\initora.ora' from spfile;
打开E:\app\interlib\initora.ora文件按照以下内容修改:
#下面这部分是添加的
*.db_unique_name='primary'
*.archive_lag_target=1800
*.fal_client='standby'
*.fal_server='primary'
*.log_archive_config='DG_CONFIG=(primary,standby)'
*.log_archive_dest_1='location=E:\app\interlib\log lgwr VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=primary'
*.log_archive_dest_2='SERVICE=standby async affirm VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=standby'
*.log_archive_dest_state_1='enable'
*.log_archive_dest_state_2='enable'
*.log_archive_format='%t_%s_%r.dbf'
*.DB_FILE_NAME_CONVERT='E:\app\Administrator\oradata\obpm','E:\app\Administrator\oradata\obpm'
*.LOG_FILE_NAME_CONVERT=' E:\app\interlib\log',' E:\app\interlib\log'
*.standby_file_management=AUTO
修改之后继续操作导入修改后的参数文件:
shutdown immediate;
startup pfile='E:\app\interlib\initora.ora' nomount;
create spfile from pfile='E:\app\interlib\initora.ora' nomount;
shutdown immediate;
startup;
如果startup 成功说明没有错误
3、配置监听和访问服务:(E:\app\Administrator\product\11.2.0\dbhome_1\NETWORK\ADMIN下)
listen.ora文件修改:
典型监听文件listen.ora分成两部分,LISTENER注册的是服务,SID_LIST_LISTENER注册实例
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(SID_NAME = CLRExtProc)
(ORACLE_HOME = E:\app\Administrator\product\11.2.0\dbhome_1)
(PROGRAM = extproc)
(ENVS = "EXTPROC_DLLS=ONLY:E:\app\Administrator\product\11.2.0\dbhome_1\bin\oraclr11.dll")
)
(SID_DESC =
(GLOBAL_DBNAME=obpm)
(SID_NAME = obpm)
(ORACLE_HOME = E:\app\Administrator\product\11.2.0\dbhome_1)
)
)
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.10.20)(PORT = 1521))
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
)
)
tnsname.ora修改(增加以下内容,放在最前面)
primary =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.10.20)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = obpm)
)
)
standby=
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.10.21)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = obpm)
)
)
验证修改是否成功:
退出sqlplus:
host
lsnrctl stop
lsnrctl start
tnsping primary
tnsping standby
sqlplus sys/password@primary as sysdba
sqlplus sys/standby@primary as sysdba
关闭主库:
shutdown immediate;
三、备库配置
1、用NCA(Net Configuration Assistant) 创建监听程序。(按照提示‘下一步’即可,和主库一样)
2、创建实例(cmd命令)
oradim -new -sid obpm(删除实例:oradim -delete -sid obpm)
3、从主库中复制文件到备库相应目录中:
a、将主库E:\app\Administrator目录下的admin,cfgtollogs,diag,flash_recover_area目录拷贝到备用库的相同路径,备库已经有的文件和文件夹直接覆盖掉。
b、将主库E:\app\Administrator目录下的oradata文件夹下内容复制到从库相同目录下 。
c、将主库生成的密码文件拷贝到从库相同路径下(E:\app\Administrator\product\11.2.0\dbhome_1\database\PWDobpm.ora)。
d、将E:\app\interlib拷贝到备库相同路径 。
e、将主库生成的standby.ctl文件拷贝到备库的数据文件夹内(E:\app\Administrator\oradata\obpm\)和E:\app\Administrator\flash_recovery_area\orcl\下。并分别重名为control01.ctl,control02.ctl。
f、将主库的listener.ora和tnsname.ora拷贝到备库相同路径 。
修改备库listener.ora文件的Ip为备库Ip(192.168.10.21 )。
4、lsnrctl start;
5、修改initora.ora文件:
打开E:\app\interlib\initora.ora文件按照以下内容修改:
#下面这部分是添加的
*.db_unique_name='standby'
*.archive_lag_target=1800
*.fal_client='primary'
*.fal_server='standby'
*.log_archive_config='DG_CONFIG=(primary,standby)'
*.log_archive_dest_1='location=E:\app\interlib\log lgwr VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=standby'
*.log_archive_dest_2='SERVICE=primary async affirm VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=primary'
*.log_archive_dest_state_1='enable'
*.log_archive_dest_state_2='enable'
*.log_archive_format='%t_%s_%r.dbf'
*.DB_FILE_NAME_CONVERT='E:\app\Administrator\oradata\obpm','E:\app\Administrator\oradata\obpm'
*.LOG_FILE_NAME_CONVERT=' E:\app\interlib\log',' E:\app\interlib\log'
*.standby_file_management=AUTO
6、命令操作:
进入dos界面开始命令操作:
转换到sqlplus.exe 所在的路径:
cd E:\app\Administrator\product\11.2.0\dbhome_1\BIN
进入sqlplus环境:
set oracle_sid=obpm
sqlplus / as sysdba
启动备库数据库:
startup nomount pfile='E:\app\interlib\initora.ora';
create spfile from pfile='E:\app\interlib\initora.ora';
shutdown immediate;
startup nomount;
alter database mount standby database;
#在备用服务器上启动日志传送服务
alter database recover managed standby database using current logfile disconnect from session;
shutdown immediate;
startup;
到此,主从配置完成。
四、检验配置
#查看切换状态(主、备:): DG环境中,主库默认状态是TO STANDBY,备库默认状态是 NOT ALLOWED是正常的。
select switchover_status from v$database;
#主库执行:
alter system switch logfile;
#主、备执行(查看条数是否是一样的,一样的说明是同步了)
select max(sequence#) from v$archived_log;
#看备库是否redo应用(若applied 为YES则成功)
select sequence#,applied from v$archived_log order by sequence#;
也可以通过PLSQL工具在主库中添加数据看从库是否同步。
五、错误解决
1、主备库状态不是默认状态(主库状态为UNRESOLVABLE GAP,备库RECOVERY NEEDED),但是max(sequence#)主备条数一样,说明主备库之间是通的,但是没有应用(即:applied为NO)
select switchover_status from v$database;
select max(sequence#) from v$archived_log;
----解决方法
备库:selecct * from v$archive_gap;
由此可见,备库接受最低的seq是1360,因为1359丢失导致后面的log没有备应用。在日志文件夹中(E:\app\interlib\log)找到1359日志注册以下即可(所以日志文件不能轻易的删除,移动)
主库注册:alter database register physical logfile '日志路径';