一、概述
本文将介绍如何给rac搭建单节点的dg,以及如何对其进行角色转换。预先具备的知识(rac搭建,单实例-单实例dg搭建)
二、实验环境介绍
主库rac(已安装rac,并已有数据库orcl)
rac1:192.168.56.11,sid1:orcl1,version:11.2.0.4
rac2:192.168.56.12,sid2:orcl2,version:11.2.0.4
从库(已安装单实例数据库软件,无数据库实例)
oradg:192.168.56.102,sid:orcldg,version:11.2.0.4
三、搭建dg
** 以下所有主库操作都在节点1上做,如果需要在节点2上做的,我会标明
1. 主库打开归档模式,并强制写日志
先查看数据库的归档状态以及是否开启强制写日志,从下图可以看到目前数据库并没有打开归档,也没有开启强制写日志
SQL> select log_mode, force_logging from v$database;
数据库在mount状态下打开归档
SQL> alter system set log_archive_dest_1=‘location=+data‘ sid=‘*‘ scope=spfile;
SQL> shutdown immediate; # 两节点都关闭
SQL> startup mount; # 只开启节点1
SQL> alter database archivelog;
SQL> alter database open;
强制日志写,数据库在open状态就能修改
SQL> alter database force logging;
SQL> startup; # 当节点1open完毕后,在节点2上打开数据库
再来查看数据库的归档状态以及是否开启强制写日志
SQL> select log_mode, force_logging from v$database;
2. 主库打开dataguard开关
SQL> alter system set log_archive_config=‘dg_config=(orcl,orcldg)‘ sid=‘*‘; # orcl是主库的db_unique_name,orcldg是从库的db_unique_name
3. 主库设置远程归档
SQL> alter system set log_archive_dest_3=‘service=orcldg valid_for=(online_logfiles,primary_role) db_unique_name=orcldg‘ sid=‘*‘;
4. 将主库的口令文件传送给从库
[[email protected] ~]$ scp $ORACLE_HOME/dbs/orapw$ORACLE_SID 192.168.56.102:$ORACLE_HOME/dbs/orapworcldg
5. 从库准备参数文件
这里参数文件跟给单实例搭建单实例dg没什么区别,所以不做具体介绍
[[email protected] ~]# vi $ORACLE_HOME/dbs/initorcldg.ora
*.audit_file_dest=‘/u01/app/oracle/admin/orcldg/adump‘ *.audit_trail=‘db‘ *.compatible=‘11.2.0.4.0‘ *.control_files=‘/u01/app/oracle/oradata/orcldg/controlfile/control01.ctl‘ *.db_block_size=8192 *.db_domain=‘‘ *.db_name=‘orcl‘ *.diagnostic_dest=‘/u01/app/oracle‘ *.log_archive_config=‘dg_config=(orcl,orcldg)‘ *.log_archive_dest_2=‘location=/u01/app/oracle/oradata/orcldg/archstdlog/ valid_for=(standby_logfiles,standby_role) db_unique_name=orcldg‘ *.memory_target=838860800 *.open_cursors=300 *.processes=150 *.remote_login_passwordfile=‘exclusive‘ *.undo_tablespace=‘UNDOTBS1‘ standby_file_management=auto # 该参数默认值是manual,需要将其改为auto,表示主库的数据文件发生修改(如新建,重命名等),相应地从库也做相应修改 db_unique_name=‘orcldg‘ db_file_name_convert=‘+DATA/orcl/datafile/‘,‘/u01/app/oracle/oradata/orcldg/datafile/‘,‘+DATA/orcl/tempfile/‘,‘/u01/app/oracle/oradata/orcldg/tempfile/‘ log_file_name_convert=‘+DATA/orcl/onlinelog/‘,‘/u01/app/oracle/oradata/orcldg/onlinelog/‘
6. 从库中准备相关目录
[[email protected] ~]$ mkdir -p /u01/app/oracle/admin/orcldg/adump
[[email protected] ~]$ mkdir -p /u01/app/oracle/oradata/orcldg/controlfile
[[email protected] ~]$ mkdir -p /u01/app/oracle/oradata/orcldg/archstdlog
[[email protected] ~]$ mkdir -p /u01/app/oracle/oradata/orcldg/datafile
[[email protected] ~]$ mkdir -p /u01/app/oracle/oradata/orcldg/tempfile
[[email protected] ~]$ mkdir -p /u01/app/oracle/oradata/orcldg/onlinelog
7. 启动从库实例
[[email protected] ~]$ export ORACLE_SID=orcldg
[[email protected] ~]$ sqlplus / as sysdba
SQL> create spfile from pfile;
SQL> startup nomount
8. 从库配置并启动监听程序
因为采用duplicate方式复制主库数据,所以需要将从库配置为静态注册的形式
[[email protected] ~]$ vi $ORACLE_HOME/network/admin/listener.ora
SID_LIST_LISTENER = (SID_LIST = (SID_DESC = (GLOBAL_DBNAME = orcldg) (ORACLE_HOME = /u01/app/oracle/product/11.2.0/db_1) (SID_NAME = orcldg) ) ) LISTENER = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.56.102)(PORT = 1521)) )
[[email protected] ~]$ lsnrctl start
9. 主库配置服务命名
# 两个节点都要设置
[[email protected] ~]$ vi $ORACLE_HOME/network/admin/tnsnames.ora
orcldg = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.56.102)(PORT = 1521)) ) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = orcldg) ) )
[[email protected] ~]$ vi $ORACLE_HOME/network/admin/tnsnames.ora
内容跟rac1一致,略
10. 主库使用网络连接从库(测试连通性)
# 这里我的主库的sys密码是123456,从库与主库一致
[[email protected] ~]$ sqlplus sys/[email protected] as sysdba
11. 在主库启动rman复制从库
rman连接主库和从库
[[email protected] ~]$ rman target / auxiliary sys/[email protected]
RMAN> duplicate target database for standby from active database;
12. 从库添加standbylog
添加的日志大小跟主库的onlinelog保持一致,数量多两组(主库的onlinelog信息查看v$log)
group的编号不与当前的onlinelog重复即可
[[email protected] ~]$ mkdir -p /u01/app/oracle/oradata/orcldg/standbylog/
SQL> alter database add standby logfile group 21 ‘/u01/app/oracle/oradata/orcldg/standbylog/std01.log‘ size 50M;
SQL> alter database add standby logfile group 22 ‘/u01/app/oracle/oradata/orcldg/standbylog/std02.log‘ size 50M;
SQL> alter database add standby logfile group 23 ‘/u01/app/oracle/oradata/orcldg/standbylog/std03.log‘ size 50M;
SQL> alter database add standby logfile group 24 ‘/u01/app/oracle/oradata/orcldg/standbylog/std04.log‘ size 50M;
SQL> alter database add standby logfile group 25 ‘/u01/app/oracle/oradata/orcldg/standbylog/std05.log‘ size 50M;
SQL> alter database add standby logfile group 26 ‘/u01/app/oracle/oradata/orcldg/standbylog/std06.log‘ size 50M;
13. 从库打开应用日志
SQL> alter database recover managed standby database disconnect from session;
14. 主库切换归档
SQL> alter system switch logfile;
15. 打开从库
当从库应用归档一段时间后,就关闭应用归档日志,打开从库。
SQL> alter database recover managed standby database cancel;
SQL> alter database open;
SQL> alter database recover managed standby database using current logfile disconnect from session;
16. 验证同步
主库做修改
SQL> update scott.emp set sal=2000;
SQL> commit;
从库查询
SQL> select * from scott.emp;
至此,给rac搭建一个单实例的dg就已经做完了,跟单实例搭建单实例的dg没什么区别。接下来介绍如何切换。
四、主从切换准备
17. 主库添加standby logfile归档路径
使用grid账号,创建归档路径
[[email protected] ~]$ asmcmd
ASMCMD> lsdg # 查看磁盘组名字
ASMCMD> cd data
ASMCMD> cd orcl
ASMCMD> mkdir ARCHSTDLOG
使用sys数据库账号,修改数据库参数
SQL> alter system set log_archive_dest_2=‘location=+DATA/ORCL/ARCHSTDLOG/ valid_for=(standby_logfiles,standby_role) db_unique_name=orcl‘ sid=‘*‘;
18. 主库添加standby logfile
添加的日志的大小跟主库的onlinelog保持一致,数量多两组(主库的onlinelog信息查看v$log)
SQL> alter database add standby logfile thread 1 group 21 (‘+data‘) size 50M;
SQL> alter database add standby logfile thread 1 group 22 (‘+data‘) size 50M;
SQL> alter database add standby logfile thread 1 group 23 (‘+data‘) size 50M;
SQL> alter database add standby logfile thread 2 group 24 (‘+data‘) size 50M;
SQL> alter database add standby logfile thread 2 group 25 (‘+data‘) size 50M;
SQL> alter database add standby logfile thread 2 group 26 (‘+data‘) size 50M;
19. 主库修改参数文件
SQL> alter system set standby_file_management=auto sid=‘*‘;
SQL> alter system set db_file_name_convert=‘/u01/app/oracle/oradata/orcldg/datafile/‘,‘+DATA/orcl/datafile/‘,‘/u01/app/oracle/oradata/orcldg/tempfile/‘,‘+DATA/orcl/tempfile/‘ sid=‘*‘ scope=spfile;
SQL> alter system set log_file_name_convert=‘/u01/app/oracle/oradata/orcldg/onlinelog/‘,‘+DATA/orcl/onlinelog/‘ sid=‘*‘ scope=spfile;
到这里,主库转换为备库的准备工作已完成!
20. 从库配置tnsnames.ora
这里的配置tnsnams.ora的目的是,当从库转变为主库之后,原主库变为从库,需要给原主库发送归档日志,所以这里的tnsnames要指向原主库,又由于dg库应用归档只能在一个节点上, 所以toorcl只指向节点1。
[[email protected] ~]$ vi $ORACLE_HOME/network/admin/tnsnames.ora
toorcl = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.56.11)(PORT = 1521)) ) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = orcl) ) )
21. 从库为onlinelog设置归档路径
[[email protected] ~]$ mkdir -p /u01/app/oracle/oradata/orcldg/archivelog
SQL> alter system set log_archive_dest_1=‘location=/u01/app/oracle/oradata/orcldg/archivelog/ valid_for=(online_logfiles,primary_role) db_unique_name=orcldg‘;
22. 从库设置远程归档参数
SQL> alter system set log_archive_dest_3=‘service=toorcl valid_for=(online_logfiles,primary_role) db_unique_name=orcl‘;
到这里,从库转为主库的准备工作已完成!
四、主从切换
23. 查看主库的角色转换状态
SQL> select database_role,switchover_status from v$database;
24. 在节点1上主转从
集群在做角色转换时,只能有一个实例是打开状态,其余都要关闭,所以将节点2的实例关闭。
SQL> shutdown immediate; # 只在节点2上做
在节点1上执行以下命令,将主库转为从库,并且关闭实例
SQL> alter database commit to switchover to physical standby with session shutdown;
25. 在节点3上从转主
SQL> select database_role,switchover_status from v$database;
如果是"NOT ALLOWED"表示归档还没有应用完成,可以等待一段时间
如果日志全部应用了再查看角色转换状态
SQL> select database_role,switchover_status from v$database;
如果角色转换状态是TO PRIMARY,那么表示可以进行角色转换
执行从转主的命令,命令执行成功后,数据库的状态会变为mount
SQL> alter database commit to switchover to primary;
SQL> alter database open;
26. 打开新从库
在节点1和节点2上
SQL> startup
节点1上应用归档
SQL> alter database recover managed standby database using current logfile disconnect from session;
节点3上切换归档
SQL> alter system switch logfile;
27. 验证同步
主库做修改
SQL> update scott.emp set sal=3000;
SQL> commit;
从库查询
SQL> select * from scott.emp;
28. 错误处理
如果发现日志传不到备库的话可以在主库通过以下命令查看错误原因
SQL> select dest_id,dest_name,log_sequence,status,error from v$archive_dest;
如果报上面的错误的话,可以把归档开关重启一下即可
SQL> alter system set log_archive_dest_state_3=‘defer‘ sid=‘*‘;
SQL> alter system set log_archive_dest_state_3=‘enable‘ sid=‘*‘;
五、总结
1. dg的切换需要停掉所有的应用,并把数据库的所有连接全部kill掉,账号全锁,切换完成后再解锁
2. rac在切换时,只留一个活的实例,其它全部关闭
3. 命令每执行一条,就留意返回的结果,并实时查看alert.log
原文地址:https://www.cnblogs.com/ddzj01/p/12044310.html