Oracle数据库主从同步配置详解
1. 安装前准备
@H_502_4@1、数据库软件:Oracle 11g r1 (企业版)
2、操作系统:WINDOWS SERVER 2008 ENTERPRISE EDTION 64X
主库主机: dbprimary 192.168.1.57
从库主机: dbstandby 192.168.1.58
PS:在安装oracle时,主库安装数据库,从库只安装软件
2.主库配置
配置前准备
在/app/文件夹下创建interlib文件夹,再创建子文件夹log。
日志文件路径 logUrl=’/app/interlib/log/’。
控制文件路径:standbyUrl=’/app/interlib’;
在主库上启动数据库到mount模式,开启归档模式与force logging。
在DOS中依次执行下面语句:
#在DOS窗口执行下面代码:
sqlplus / as sysdba
sql>shutdown immediate;
sql>startup mount;
#修改为归档模式
sql>alter database archivelog;
sql>alter database open;
#设置强制归档模式
sql> alter database force logging;
PS:查看命令:select log_mode,force_logging from v$database ;
查看是否归档命令:Archive log list ;
@H_502_4@
#执行下面代码:
sql>alter database add standby logfile group 4 ('logUrl/ STAN04.LOG') size 50m;
sql>alter database add standby logfile group 5 ('logUrl/ STAN05.LOG') size 50m;
sql>alter database add standby logfile group 6 ('logUrl/ STAN06.LOG') size 50m;
PS:logUrl是日志文件的路径,路径需要加单引号,否则会报错ora-02236。
@H_502_4@#执行下面代码:
sql>alter database create standby controlfile as 'standbyUrl\standby.ctl';
@H_502_4@
#执行下面代码:
sql>create pfile='standbyUrl/initora.ora' from spfile;
standbyUrl是控制文件路径,只是为了保存initora.ora文件,方便查找。[^footnote].
#下面这部分是要添加的
*.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= logUrl VALID_FOR=(all_logfiles,all_roles) db_unique_name=primary'
*.log_archive_dest_2='service=standby arch async 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='D:\ oracle数据库数据文件路径','oracle数据库数据文件路径(例如D:\app\Administrator\oradata\orcl\)'
*.LOG_FILE_NAME_CONVERT=' logUrl ',' logUrl '
*.standby_file_management='auto'
效果图为:
PS:该效果图中的路径不是正确的,只是提供一个参考,具体的路径需根据实际情况配置。
按照顺序执行下面命令:
#停止服务
sql>shutdown immediate;
#使用新参数文件启动数据库
sql>startup pfile=' standbyUrl\initora.ora' nomount;
#创建新的spfile文件
sql>create spfile from pfile='standbyUrl\initora.ora';
#停止服务
sql>shutdown immediate;
#启动服务
sql>startup;
@H_502_4@
#该语句在DOS窗口执行,不需要登录sqlplus,路径不能加引号,否则汇报opw-00001错误
orapwd file=passwordUrl\PWDorcl.ora password=123 entries=10
PS:密码文件存放路径:passwordUrl=D:\app\Administrator\product\11.2.0\dbhome_1\database。
@H_502_4@修改listener.ora
监听服务文件路径:/app/product/11.2.0/dbhome_1/NETWORK/ADMIN/listener.ora 修改listener.ora文件,加粗的是要添加和修改的内容
#修改SID_LIST_LISTENER
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(SID_NAME = CLRExtProc)
(ORACLE_HOME = D:\app\Administrator\product\11.2.0\dbhome_1)
(PROGRAM = extproc)
(ENVS = "EXTPROC_DLLS=ONLY:D:\app\Administrator\product\11.2.0\dbhome_1\bin\oraclr11.dll")
)
#添加SID_DESC
(SID_DESC =
(GLOBAL_DBNAME = orcl)
(ORACLE_HOME = oracle主目录(例如D:\app\Administrator\product\11.2.0\dbhome_1))
(SID_NAME = orcl)
)
)
#修改LISTENER
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
#在原来的基础上添加SID_DESC
(ADDRESS = (PROTOCOL = TCP)(HOST = 主库主机IP)(PORT = 1521))
)
)
ADR_BASE_LISTENER = D:\app\Administrator
修改tnsname.ora文件
# tnsnames.ora Network Configuration File: D:\app\Administrator\product\11.2.0\dbhome_1\network\admin\tnsnames.ora
# Generated by Oracle configuration tools.
#增加主库配置
PRIMARY =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP) (HOST = 192.168.1.57) (PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = orcl)
)
)
#增加从库配置
STANDBY=
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP) (HOST = 192.168.1.58) (PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = orcl)
)
)
ORACLR_CONNECTION_DATA =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
)
(CONNECT_DATA =
(SID = CLRExtProc)
(PRESENTATION = RO)
)
)
ORCL =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = WIN-QEDP6N8PVGK)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl)
)
)
测试监听
#停止服务
sql>shutdown immediate;
sql>startup mount;
#alter database open是数据库处于mount状态,从mount状态打开数据库的命令。
sql>alter database open;
lsnrctl stop
lsnrctl start
#测试配置是否正确
sql>sqlplus sys/pwd@primary as sysdba
PS:若连接失败,检查防火墙设置或者ping下IP地址是否正常。
关闭主数据库
sql>shutdown immediate;
3.从库配置
3.1.主库复制文件到从库
@H_502_4@修改备库listener.ora文件的Ip为备库Ip(192.168.1.58 )。
3.2.备库新建实例
#在备库上注册oracle实例到服务中,具体命令如下:
oradim -new -sid orcl(实例名)
PS:如果已经装了数据库,执行的话也会提示这个实例已经存在,接着继续下面的就可以
3.3.备库启动监听
#DOS中执行
lsnrctl start
将刚才从主库拷贝过来的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=D:\app\interlib\log\ VALID_FOR=(all_logfiles,all_roles) db_unique_name=standby'
*.log_archive_dest_2='service=primary arch async 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='D:\app\Administrator\oradata\orcl\','D:\app\Administrator\oradata\orcl\' *.LOG_FILE_NAME_CONVERT='D:\app\interlib\log\','D:\app\interlib\log\' *.standby_file_management='auto'
使用新参数文件建立从库实例
sql>startup nomount pfile='C:\app\interlib\initora.ora';
sql>create spfile from pfile='C:\app\interlib\initora.ora';
sql>shutdown immediate;
3.5.配置从库为备用库模式
#启动备用数据库为挂载备库模式
sql>startup nomount;
sql>alter database mount standby database;
#在备用服务器上启动日志传送服务
sql>alter database recover managed standby database disconnect from session;
sql>shutdown immediate;
sql>startup;
3.6.检测配置是否成功
#查看切换状态:
sql>select switchover_status from v$database;
#手动切换归档,查看归档文件是否自动同步到备用库. 切换归档:
sql> alter system switch logfile;
#在主库和从库查看归档日志是否同步:
sql>SELECT SEQUENCE#,APPLIED FROM V$ARCHIVED_LOG ORDER BY SEQUENCE#;
具体操作步骤:
1. 查看oradata/archive文件是否传过去了
2. 在dbprimary上插入一些数据
#主库操作,查看当前sequence
(dbprimary)sql>select max(sequence#) from v$archived_log;
#从库操作,查看当前sequence
(dbstandby)sql>select max(sequence#) from v$archived_log;
#主库操作,查看当前sequence
(dbprimary)sql>alter system switch logfile;
#主库操作,查看当前sequence
(dbprimary)sql>select max(sequence#) from v$archived_log;
#从库操作,查看当前sequence
(dbstandby)sql>select max(sequence#) from v$archived_log;
如果dbstandby的sequence跟着改变,说明已经好了
如果查看从库可以看到归档日志,表示归档日志同步成功,如果APPLIED列的值为yes,表示重做应用成功.
主库配置完成.
4.常用命令
#检测Data Guard方式的命令
SELECT PROTECTION_MODE FROM V$DATABASE;
#修改Data Guard保护模式
ALTER DATABASE SET STANDBY DATABASE TO MAXIMIZE {AVAILABILITY | PERFORMANCE | PROTECTION};
#对单实例数据库或RAC中的当前实例执行日志切换;
ALTER SYSTEM SWITCH LOGFILE
#会对数据库中的所有实例执行日志切换。
而ALTER SYSTEM ARCHIVE LOG CURRENT
#设置自动归档时间间隔(单位是秒)
alter system set archive_lag_target=1800 scope=both;
#物理备库开启实时应用
alter database recover managed standby database using curren logfile dixconnect from session ;
#取消归档日志时间间隔-物理备库
alter database recover managed standby database nodelay ;
5.常见错误处理办法
错误号 | 错误描述 |
---|---|
ORA-10456 | cannot open standby database; media recovery session may be in progress |
#解决办法
alter database recover managed standby database cancel;
alter database open;
alter database recover managed standby database using current logfile disconnect ;
错误号 | 错误描述 |
---|---|
ORA-02232 | ??? MOUNT ?? |
#解决办法
主库生成的standby.ctl控制文件拷贝到initora.ora中控制文件指定的地方,再重新
使用initora.ora nomount启动数据库即可,对于从库原有的control01.ctl文件进行覆盖,必须保证主库和从库的control01文件一致。
错误号 | 错误描述 |
---|---|
ORA-01090 | shutdown in process-connection is not permitted |
#解决办法
再开一个会话执行
shutdown abort
startup
shutdown immediate
错误号 | 错误描述 |
---|---|
ora-16004,ora-01152,ora-01110 | 从库启动报错, |
解决办法:
sql> alter database recover managed standby database using current logfile disconnect from session; (需要在备库建立standby logfile,否则会出现ORA-38500错误)
在主库
sql> alter system switch logfile;
在备库
sql> alter database recover managed standby database cancel;
数据库已更改。
sql> alter database open;
本文引用部分网友文章:
https://www.2cto.com/database/201308/239244.html
http://blog.csdn.net/qq_23412263/article/details/70240485
http://tianzt.blog.51cto.com/459544/251239/
其他优秀文章参考:
http://www.jb51.cc/article/p-brtfrkse-bqq.html