一.
容灾配置
本文档中红色标注部分为需要在原文件中添加的文字,蓝色标注部分为需要依据个人实际配置数据库情况加以修改文字,绿色标注为注释
1. 安装数据库,建实例。本例中数据库
DB_NAME=ora INSTANCE_SID= ora
Primary IP 192.168.100.1
数据文件路径 /ORACLEapp/oracle/product/10.2.0/db_1
Standby IP 192.168.100.2 数据文件路径/ORACLEapp/oracle/product/10.2.0/db_1
操作系统为centos,ORACLE版本10.2.0
2. 在主库上执行
SQL>archive log list;
查看是否为归档模式,如果不是,需要执行如下
SQL
SQL>startup mount;
SQL>alter database archivelog;
SQL>alter database open
3. 若是归档模式,设置归档路径和归档格式
SQL>alter system set log_archive_dest_1=’location=/arch’ scope=spfile;
存放归档日志
SQL>alter system set log_archive_format=’arch_%t_%s_%r.arc’ scope=spfile;
4. 修改数据库为强制归档模式,这样某些不写日志的操作也会写日志归档,备库数据更安全,恢复数据更全面,不至于丢数据
SQL>alter database force logging;
SQL>shutdown immediate
5. 主库创建参数文件,
standby控制文件
SQL>startup mount
SQL>create pfile from spfile;
SQL>alter database create standby controlfile as ‘
/arch/111.ctl’;
6. rman备份
SQL>create tablespace rmantablespace datafile ‘/ORACLE/app/oracle/rmantable
Space.dat’ size 30m;
SQL> create user rman identified by rman123 default tablespace rmantablespace
Temporary temp;
SQL>grant resource,connect,recovery_catalog_owner to rman;
SQL>grant create table to rman;
$rman target /
RMAN>backup database plus archiveslog;
SQL>shutdown immediate
7. 修改主库的
pfile文件,添加以下各行参数
*.log_archive_dest_2='SERVICE=standby'
<指向第2个归档日志存放位置,至备机>
*.LOG_ARCHIVE_DEST_STATE_1=ENABLE
*.LOG_ARCHIVE_DEST_STATE_2=ENABLE
*.FAL_SERVER=standby
<作为备库时主库的网络名>
*.FAL_CLIENT=primary
<作为备库时备库的网络名>
*.STANDBY_FILE_MANAGEMENT=AUTO
*.STANDBY_ARCHIVE_DEST='/arch'
<作为备库时归档日志存放位置>
*.DB_FILE_NAME_CONVERT=('/ORACLE/app/oracle/oradata/ora/','/ORACLE/app/oracle/oradata/ora/'
)
<当主库和备库数据文件存放位置不一致时的路径转换,一致时可不加,前为主库数据文件路径,后为备库数据文件路径>
*.LOG_FILE_NAME_CONVERT=('/ORACLE/app/oracle/oradata/ora/','/ORACLE/app/oracle/oradata/ora/')
<当主库和备库在线日志文件(redo log)存放位置不一致时的路径转换,一致时可不加,前为主库在线日志文件路径,后为备库在线日志文件路径>
8. 修改备库的参数文件,添加以下各行参数
*.log_archive_dest_2='SERVICE=primary'
*.LOG_ARCHIVE_DEST_STATE_1=ENABLE
*.LOG_ARCHIVE_DEST_STATE_2=ENABLE
*.FAL_SERVER=primary
*.FAL_CLIENT=standby
*.STANDBY_ARCHIVE_DEST='/arch'
*.STANDBY_FILE_MANAGEMENT=AUTO
*.DB_FILE_NAME_CONVERT=('/ORACLE/app/oracle/oradata/ora/','/ORACLE/app/oracle/oradata/ora/')
*.LOG_FILE_NAME_CONVERT=('/ORACLE/app/oracle/oradata/ora/','/ORACLE/app/oracle/oradata/ora/')
同时修改参数文件中各个路径同备库实际情况一致
9. 备库将刚才新建的standby控制文件 111.ctl 复制到数据文件存放目录,并再复制3份,分别改名覆盖原控制文件control01.ctlcontrol02.ctlcontrol03.ctl
将rman备份文件也拷贝过去
10. 配置主备库的监听文件 (如果备库只安装了oracle软件,需要netca配置监听)
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(SID_NAME = PLSExtProc)
(ORACLE_HOME = /ORACLE/app/oracle/product/10.2.0/db_1/)
(PROGRAM = extproc)
)
(SID_DESC =
(GLOBAL_DBNAME= ora)
(ORACLE_HOME = /ORACLE/app/oracle/product/10.2.0/db_1/)
(SID_NAME = ora)
)
)
LISTENER =
(DESCRIPTION_LIST =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.100.1)(PORT = 1521))
(ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC0))
)
)
此为主库监听文件配置,备库同理,红色为需要添加部分,蓝色为本机IP地址
11. 配置主备库的tnsnames.ora,主备库皆在tnsnames.ora中添加以下内容
PRIMARY =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.100.1)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = ora)
)
)
STANDBY =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.100.2)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = or)
)
)
12. 主库备库分别重新打开监听,tnsping primary ,tnsping standby 看能否ping通,ping通即可进入下一步,不通的话可检查防火墙是否关闭,配置是否有问题。
13. 启动备库
SQL>create spfile from pfile=’ /ORACLE/app/oracle/product/10.2.0/db_1/dbs/
initora.ora’; <此处pfile文件为修改过的备库参数文件>
SQL>startup nomount (不可以直接startup,否则不能shutdown,解决方法:重启)
$$ORACLE_HOME/bin/orapwd file=$ORACLE_HOME/dbs/orapw$ORACLE_SID password=oracle force=y (建立密码文件,也可以拷贝过来)
$rman target /
RMAN>restore database;
SQl>alter database mount standby database;
SQL>alter database recover managed standby database disconnect from session;
14. 启动主库
SQL>shutdown immediate
SQL>create spfile from pfile=’
/ORACLE/app/oracle/product/10.2.0/db_1/dbs/
initora.ora’ <此处pfile文件为修改过的主库参数文件>
SQL>startup
15. 主库上做几次日志切换
SQL>alter system switch logfile;
SQL>archive log list;
16. 备库查询日志应用情况
SQL>archive log list;
(日志不同步主要有2大原因,一是监听,二是/arch文件夹权限,还有可能就是看看你启动是否应用的是修改后的pfile)
17. 此时查询到的日志情况应是除中间一项为0外,其他同主库一致
SQL> select sequence#,applied from v$archived_log order by sequence#;
查询备机日志应用情况,若都为YES且日志数字比主库当前日志号小1即为正常
在primary机查询
SQL>select switchover_status from v$database;
查询结果应为 TO STANDBY
在standby机查询
SQL>select switchover_status from v$database;
查询结果应为 NOT ALLOWED
<因为必须主库先切到备库,备库才能切到主库,所以主库正常运行的时候是不允许备库切到主库的,当主库关闭后,备库再查询此SQL会显示TO PRIMARY>
如果有错,请检查配置
至此,容灾配置正常完成.
二.容灾测试
主库上:
SQL>create user test identified by test;
SQL>grant connect,resource to test;
SQL>conn test/test@primary
SQL>create table test(name varchar2(20));
SQL>insert into test.test values('this is a test');
SQL>commit;
SQL>conn /as sysdba
SQL>alter system switch logfile;
将备库切换到只读查询模式,查看修改是否已经生效:
备库上:
SQL>conn /as sysdba
SQL>alter database recover managed standby database cancel;
SQL>alter database open read only;(我这步不能打开,只有切换到最大可用户之后才能打开,自己也是新手,具体原因还不明)
SQL>conn test/test
SQL>select * from test;
结果应该是:
NAME
--------------
This is a test
说明同步正常。
再次将备库切换至恢复状态:
SQL>alter database recover managed standby database disconnect from session;
强制主机归档,在备机查看归档日志应用情况
select sequence#,applied from v$archived_log order by sequence#;
应用情况为YES.
如果要强制主库每10分钟归档一次,如下设置:(任意时间都可以设置)
Alter system set archive_lag_target=600 scope=both;
三.容灾关闭
关闭数据库一定要先关闭主库再关闭备库。
主库:
SQL>shutdown immediate
备库:
SQL>alter database recover managed standby database cancel;
SQL>shutdown immediate
四.
主备机切换
主备机切换必须先主切备,在备切主
primary 切�Q到standby database
1.
SQL>alter system switch logfile; <主切备前最好做一次归档>
2.
SQL>alter database commit to switchover to standby with session shutdown;
3. SQL>shutdowm immediate
4. SQL>startup nomount
5. SQL>alter database mount standby database;
6. SQL>alter database recover managed standby database disconnect from session;
standby 切�Q到primary
1. SQL>alter database commit to switchover to primary with session shutdown;
2. SQL>shutdown immediate;
3. SQL>startup
四.切换到最大可用模式
1.首先应当在standby库创建standby redolog
SQL>alter database recover managed standby database cancel;
SQL>alter database add standby logfile
group 4 (‘/ORACLE/app/oracle/oradata/ora/redo04.log’) size
50m,
group 5 (‘/ORACLE/app/oracle/oradata/ora/redo05.log’) size
50m,
group 6 (‘/ORACLE/app/oracle/oradata/ora/redo06.log’) size
50m,
group 7 (‘/ORACLE/app/oracle/oradata/ora/redo07.log’) size
50m;
standby redolog的组数参考公式:(online redolog组数 + 1) * 数据库线程数;
单机线程数为1,RAC一般为2。
standby redolog的组成员数和大小也尽量和online redolog一样。
检查standby redolog是否已经添加:
SQL>select group#, thread#,sequence#,archived, status from v$standby_log;
GROUP# THREAD# SEQUENCE# ARC STATUS
---------- ---------- ---------- --- ----------
4 0 0 YES UNASSIGNED
5 0 0 YES UNASSIGNED
6 0 0 YES UNASSIGNED
7 0 0 YES UNASSIGNED
2.
standby数据库重新启动恢复管理模式
SQL>alter database recover managed standby database disconnect from session;
3.修改primary数据库参数,设置备库为最大性能模式(primary库执行)
SQL>ALTER SYSTEM SET LOG_ARCHIVE_DEST_2='SERVICE=STANDBY SYNC LGWR AFFIRM NET_TIMEOUT=10' SCOPE=SPFILE;
SQL>shutdown immediate
SQL>startup mount
SQL>alter database set standby database to maximize availability;
<maximize 后可跟{PROTECTION | AVAILABILITY | PERFORMANCE},分别对应最大保护,最高可用性及最高性能>
SQL>alter database open
4. 查看保护模式状态
SQL> select protection_mode,protection_level from v$database;
PROTECTION_MODE PROTECTION_LEVEL
-------------------- --------------------
MAXIMUM AVAILABILITY MAXIMUMAVAILABILITY
显示目前为最大可用模式
配置成功,顺便再测试一下停掉standby 数据库,再查看primary 数据库状态
SQL> select protection_mode,protection_level from v$database;
PROTECTION_MODE PROTECTION_LEVEL
-------------------- --------------------
MAXIMUM AVAILABILITY RESYNCHRONIZATION
Standby 数据库shutdown 后,primary 数据库保护级别切换为待同步。
5. 如果要使得主备库可以切换,则应如1-4步骤同理在主备库做相应修改
错误一:
ORA-03113: end-of-file on communication channel
错误
SQL> conn / as sysdba
Connected to an idle instance.
SQL> startup mount pfile=?/dbs/initjhcqcnc.ora
ORACLE instance started.
Total System Global Area 235999352 bytes
Fixed Size 450680 bytes
Variable Size 201326592 bytes
Database Buffers 33554432 bytes
Redo Buffers 667648 bytes
Database mounted.
SQL> select protection_mode from v$database;
PROTECTION_MODE
--------------------
MAXIMUM PROTECTION
SQL> alter database set standby database to maximize PERFORMANCE ;
Database altered.
SQL> alter database open;
Database altered.