1.环境规划
集群1 | 集群2 | |
---|---|---|
主机 | rac1,rac2 | olrac1,olrac2,olrac3 |
ip | 192.168.5.111-112 | 192.168.5.131-133 |
实例名sid | orcldg1,orcldg2 | orcl1,orcl2,orcl3 |
scan-ip | 192.168.5.200 | 192.168.5.100 |
存储 | asm | asm |
1.集群安装
参考
oracle rac 环境搭建:https://www.yuque.com/docs/share/4196ef7e-8cbf-4268-87d5-ae4b775d9322?#
oracle rac 安装:https://www.yuque.com/docs/share/97fd8e3b-a025-4375-8afb-a1ef1eb2f406?#
rac db 安装:https://www.yuque.com/docs/share/5e50fb2f-8a16-4987-9e7d-9d47ec3939ba?#
rac 节点添加:https://www.yuque.com/docs/share/45aa6892-b580-42bb-a455-8c055243a259?#
2.rac 开启归档
1)修改cluster_database 修改为false
alter system set cluster_database=false scope=spfile sid='orcl1';
2)关闭所有实例数据库
srvctl stop database -db orcl
3)启动olracdb1本地实例挂载数据库
startup mount;
4)启动归档
alter database archivelog;
5)修改归档日志文件参数和路径
注意:log_archive_dest_1和log_archive_dest冲突两者只能设置一个
alter system set log_archive_format='%t_%s_%r.arc' scope=spfile sid='*';
alter system set log_archive_dest_1='location=+data/orcl/archivelog' scope=spfile sid='*';
6)修改cluster_databae 值为true
alter system set cluster_database=true scope=spfile sid='orcl1';
7)关闭实例启动数据库
shutdown immediate;
srvctl start database -db orcl
2.修改主库参数
1启用强制日志
select force_logging from v$database;
alter database force logging;
2.设置主库初始化参数
alter system set db_unique_name='orcl' scope=spfile;
alter system set db_unique_name=orcl scope=spfile; #不加单引号设置的db_unique_name为大写
alter system set log_archive_config='DG_CONFIG=(orcl,orcldg)' scope=both sid='*';
alter system set log_archive_dest_1='LOCATION=+data/orcl/archivelog VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=orcl' scope=both sid='*';
alter system set log_archive_dest_2='SERVICE=tnsorcldg LGWR SYNC AFFIRM VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=orcldg' scope=both sid='*';
3.设置转换文件路径
注意:如果不做主备切换这个值不用设置
pdb注意pdb_file_name_convert 参数
alter system set db_file_name_convert='+DATA/orcldg/datafile','+DATA/orcl/datafile','+DATA/orcldg/tempfile','+DATA/orcl/tempfile' scope=spfile sid='*';
alter system set log_file_name_convert='+DATA/orcldg/onlinelog','+DATA/orcl/onlinelog' scope=spfile sid='*';
4.设置主备数据文件添加自动同步
alter system set standby_file_management=auto scope=both sid='*';
5.设置切换client/server
注意:如果不做主备切换这个值不用设置
alter system set fal_client='tnsorcl' scope=both sid='*';
alter system set fal_server='tnsorcldg' scope=both sid='*';
3.设置/etc/hosts
注意:两个集群所有的主机都要添加里面
#Public
192.168.5.131 olrac1
192.168.5.132 olrac2
192.168.5.133 olrac3
#Virtual
192.168.5.141 olrac1-vip
192.168.5.142 olrac2-vip
192.168.5.143 olrac3-vip
#Private
10.10.10.21 olrac1-priv
11.11.11.21 olrac1-priv
10.10.10.22 olrac2-priv
11.11.11.22 olrac2-priv
10.10.10.23 olrac3-priv
11.11.11.23 olrac3-priv
#Scan-ip
192.168.5.100 olrac-scan
#Public
192.168.5.111 rac1
192.168.5.112 rac2
192.168.5.113 rac3
#Virtual
192.168.5.101 rac1-vip
192.168.5.102 rac2-vip
192.168.5.103 rac3-vip
#Private
10.10.10.11 rac1-priv
10.10.10.12 rac2-priv
10.10.10.13 rac3-priv
#Scan-ip
192.168.5.200 rac-scan
4.设置主备库监听和服务
先设置点对点 同步,同步完成之后,再修改到scan-ip
netca
netmgr
主备库oracle目录下所有节点:tnsnames.ora
TNSORCLDG =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = rac1-vip)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = orcl)
)
)
TNSORCL =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = olrac1-vip)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = orcl)
)
)
备库gird 目录下所有节点:listener.ora
netca
#注意:备库静态监听指向地址是oracle 账户下$ORACLE_HOME
VALID_NODE_CHECKING_REGISTRATION_LISTENER_DG = SUBNET
LISTENER_DG =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = IPC)(KEY = LISTENER_DG))
)
ENABLE_GLOBAL_DYNAMIC_ENDPOINT_LISTENER_DG = ON
SID_LIST_LISTENER_DG =
(SID_LIST =
(SID_DESC =
(GLOBAL_DBNAME = orcl)
(ORACLE_HOME = /u01/app/oracle/product/19.3.0.0/dbhome_1)
(SID_NAME = orcldg1)
)
)
ADR_BASE_LISTENER_DG = /u01/app/oracle
备库设置
1.拷贝密码文件
注意:拷贝需要执行首次同步的节点
主库grid账户
asmcmd
cd +data/orcl/password
pwcopy pwdorcl.287.1040139075 /u01/app/oracle/product/19.3.0.0/dbhome_1/dbs/orapworcl
scp orapworcl oracle@192.168.5.111:/u01/app/oracle/product/19.3.0.0/dbhome_1/dbs/orapworcldg
2.创建备库spfile文件
db_name='orcl'
db_unique_name='orcldg'
sga_target=3G
pga_aggregate_target=800M
audit_file_dest='/u01/app/oracle/admin/orcldg/adump'
compatible='19.0.0'
remote_login_passwordfile='EXCLUSIVE'
control_files='+data'
log_archive_config='DG_CONFIG=(orcl,orcldg)'
log_archive_dest_1='LOCATION=+data/orcldg/archivelog VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=orcldg'
log_archive_dest_2='SERVICE=tnsorcl LGWR SYNC AFFIRM VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=orcl'
db_file_name_convert='+DATA/orcl/datafile','+DATA/orcldg/datafile','+DATA/orcl/tempfile','+DATA/orcldg/tempfile'
log_file_name_convert='+DATA/orcl/onlinelog','+DATA/orcldg/onlinelog'
fal_client='tnsorcldg'
fal_server='tnsorcl'
standby_file_management='AUTO'
db_create_file_dest='+data'
db_recovery_file_dest='+data'
db_recovery_file_dest_size=20g
3.启动备库数据库到nomount状态
mkdir /u01/app/oracle/admin/orcldg/adump -p
startup nomount
4.duplicate 同步主库数据
rman target sys/Oracle123@192.168.5.131:1521/orcl auxiliary sys/Oracle123@192.168.5.111:1522/orcl
duplicate target database for standby from active database nofilenamecheck;
5.重建spfile
注意:修改pfile的时候不要关闭数据库,create 完成之后在重启。
备库节点创建pfile 追加参数
create pfile from spfile;
注意备库有多少个节点就添加多少个instance_number,thread,undo_tablespace
vi initorcldg1.ora
*.cluster_database=TRUE
instance_number=2
orcldg1.instance_number=1
orcldg2.instance_number=2
orcldg1.thread=1
orcldg2.thread=2
orcldg1.undo_tablespace='UNDOTBS1'
orcldg2.undo_tablespace='UNDOTBS2'
------------------------
create spfile='+data' from pfile='/u01/app/oracle/product/19.3.0.0/dbhome_1/dbs/initorcldg1.ora';
#修改pfile文件
cd /u01/app/oracle/product/19.3.0.0/dbhome_1/dbs/
vi initorcldg1.ora
spfile='+data/orcldg/parameterfile/spfile.290.1040221333'
6.创建物理standby
alter database add STANDBY LOGFILE THREAD 1 size 300m;
alter database add STANDBY LOGFILE THREAD 1 size 300m;
alter database add STANDBY LOGFILE THREAD 1 size 300m;
alter database add STANDBY LOGFILE THREAD 2 size 300m;
alter database add STANDBY LOGFILE THREAD 2 size 300m;
alter database add STANDBY LOGFILE THREAD 2 size 300m;
----------------------------------------------------
select decode(g.THREAD#, null, vs.THREAD#, g.THREAD#) THREAD#,
decode(g.GROUP#, null, vs.GROUP#, g.GROUP#) GROUP#,
decode(g.BYTES, null, vs.BYTES / 1024 / 1024, g.BYTES / 1024 / 1024) BYTES,
decode(g.STATUS, null, vs.STATUS, g.STATUS) STATUS,
decode(g.ARCHIVED, null, vs.ARCHIVED, g.ARCHIVED) ARCHIVED,
--decode(g.GROUP#,null,'STANDBY REDO','REDO') type,
gf.TYPE,
gf.MEMBER
from v$logfile gf
left join v$log g
on gf.GROUP# = g.GROUP#
left join v$standby_log vs
on gf.GROUP# = vs.GROUP#
order by 1, 2;
7.开启同步:
查看异常
select dest_name,status,error from v$archive_dest_status where dest_id <5;
select open_mode,database_role,protection_mode,protection_level from v$database;
开启同步:
alter database open;
开启实时同步:
alter database recover managed standby database using current logfile disconnect from session;
alter database recover managed standby database using current logfile disconnect;
开启同步(日志切换才会同步)
alter database recover managed standby database disconnect from session;
停止同步:
alter database recover managed standby database cancel;
v$database;
主备库查看:
select open_mode,database_role,protection_mode,protection_level from v$database;
最大性能->最高可用模式
alter database set standby database to maximize availability;
alter database set standby database to maximize performance;
8.添加备库到srvctl 管理
1.添加数据库(oracle用户下)
srvctl add database -h #查看命令说明
crsctl stat res -t
srvctl stop database -d fxdb
srvctl remove database -d fxdb
srvctl add database -db orcldg -o /u01/app/oracle/product/19.3.0.0/dbhome_1 -p +data/orcldg/parameterfile/spfile.290.1040221333 -c rac -r physical_standby
2.添加实例(oracle用户下)
srvctl add instance -h
srvctl add instance -d orcldg -i orcldg1 -n rac1
srvctl add instance -d orcldg -i orcldg2 -n rac2
crsctl stat res -t
srvctl stop database -d orcldg -o immediate #注意数据库重启之后需手工开起同步
srvctl start database -d orcldg
9.异常检查视图
select * from gv$managed_standby;
select * from gv$standby_log;
select dest_name,status,error from v$archive_dest_status where dest_id <5;
select open_mode,database_role,protection_mode,protection_level,db_unique_name from v$database;
【解决同步故障】
select * from v$managed_standby;
主库:
select * from v$archive_dest;
select * from v$archive_dest_status;
10.修改tns连接,备库添加remote_listener
主备库oracle目录下备库连接
tnsnames.ora
TNSORCLDG =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.5.200)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = orcl)
)
)
TNSORCL =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.5.100)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = orcl)
)
)
备库添加remote_listener
注意:添加完成这步之后,最好做下同步测试。
alter system set remote_listener='rac-scan:1521' scope=both;
11.asm重建密码文件
pwcopy --dbuniquename orcldg /u01/app/oracle/product/19.3.0.0/dbhome_1/dbs/orapworcldg2 +data/orcldg/pworcldg
su - oracle #注意不能在grid 账户下面执行会提示权限不足
srvctl modify database -db orcldg -pwfile +data/orcldg/password/pwdorcldg.356.1040410249
srvctl config database -db orcldg
开启同步
1.添加Standby日志组
Standby日志组=日志组+1
alter database add standby logfile group 4 size 200m;
alter database add standby logfile group 5 size 200m;
alter database add standby logfile group 6 size 200m;
alter database add standby logfile group 7 size 200m;
alter database drop standby logfile group 4;
alter database drop standby logfile group 5;
alter database drop standby logfile group 6;
alter database drop standby logfile group 7;
视图:
v$logfile
v$standby_log
--------------------------------------------------------------
select decode(g.THREAD#, null, vs.THREAD#, g.THREAD#) THREAD#,
decode(g.GROUP#, null, vs.GROUP#, g.GROUP#) GROUP#,
decode(g.BYTES, null, vs.BYTES / 1024 / 1024, g.BYTES / 1024 / 1024) BYTES,
decode(g.STATUS, null, vs.STATUS, g.STATUS) STATUS,
decode(g.ARCHIVED, null, vs.ARCHIVED, g.ARCHIVED) ARCHIVED,
--decode(g.GROUP#,null,'STANDBY REDO','REDO') type,
gf.TYPE,
gf.MEMBER
from v$logfile gf
left join v$log g
on gf.GROUP# = g.GROUP#
left join v$standby_log vs
on gf.GROUP# = vs.GROUP#
order by 1, 2;
2.开起实时同步
开启redo应用(实时同步)commit
自Oracle Database 12.1起,USING CURRENT LOGFILE子句已弃用
alter database recover managed standby database using current logfile disconnect from session;弃用
alter database recover managed standby database using current logfile disconnect;弃用
alter database recover managed standby database disconnect;
停止redo应用
alter database recover managed standby database cancel;
开启redo应用(日志切换才会同步)
alter database recover managed standby database using archived logfile disconnect;
alter system switch logfile;
查看状态:
select name,open_mode,database_role,protection_mode,protection_level from v$database;
3.更改保护模式:
主库备库都需要执行 3 选 1
alter database set standby database to maximize performance;最大性能
alter database set standby database to maximize availability;最大可用
alter database set standby database to maximize protection;最大保护 #只能在mount下
4.【DG启停顺序】
启动:
先备库,后主库
关闭:
先主库,后备库
前提:干净的关闭
5.解决同步故障
1.查下主库2节点,备库的配置
2.检查tns配置
3.检查fal_client,fal_server
4.查下备库是否在实时应用模式
5.查询相关视图
v$managed_standby(备库查询)
RFS:传输进程
MRP0:日志应用进程
v$archive_dest(主库)
v$archive_dest_status(主库)
select dest_name,status,error from v$archive_dest_status where dest_id = 2;
select dest_name,status,error from v$archive_dest;
主备切换
1.确认角色
2个角色:primary和standby
select database_role from v$database;
2.主库添加转换参数和standby 日志组
pdb注意pdb_file_name_convert 参数
alter system set db_file_name_convert='+DATA/orcldg/datafile','+DATA/orcl/datafile','+DATA/orcldg/tempfile','+DATA/orcl/tempfile' scope=spfile sid='*';
alter system set log_file_name_convert='+DATA/orcldg/onlinelog','+DATA/orcl/onlinelog' scope=spfile sid='*';
alter system set db_file_name_convert='+DGDATA','+DATA' scope=spfile sid='*';
alter system set log_file_name_convert='+DGDATA','+DATA' scope=spfile sid='*';
alter database add STANDBY LOGFILE THREAD 1 size 300m;
alter database add STANDBY LOGFILE THREAD 1 size 300m;
alter database add STANDBY LOGFILE THREAD 1 size 300m;
alter database add STANDBY LOGFILE THREAD 2 size 300m;
alter database add STANDBY LOGFILE THREAD 2 size 300m;
alter database add STANDBY LOGFILE THREAD 2 size 300m;
alter database add STANDBY LOGFILE THREAD 3 size 300m;
alter database add STANDBY LOGFILE THREAD 3 size 300m;
alter database add STANDBY LOGFILE THREAD 3 size 300m;
3.验证是否能正常同步
4.停止业务刷新缓存
alter system switch logfile;
alter system flush buffer_cache;
alter system checkpoint;
shutdown immediate
5.切换
主备库启动单节点进行1→1切换
查询是否允许切换
select name,database_role,switchover_status from v$database;
主切备: 主库查询
SWITCHOVER_STATUS:
主库:to standby:
备库:NOT ALLOWED
备库切回主库:
SWITCHOVER_STATUS:
备库为:to primary
alter database commit to switchover to physical standby;
alter database commit to switchover to physical standby with session shutdown;(会话连接着)
备库到主库
alter database commit to switchover to primary;
alter database commit to switchover to primary with session shutdown;
强制切换
alter database switchover to orcldg verify;
alter database switchover to orcldg ;
alter database switchover to orcldg force; #强制切换
原主库状态为:shutdown
原备库状态为:mount
6.重新启动数据库
最好最备库都重新启动,
备库切换到主库时为mount 状态,需要手动open
主库切换为备库时会直接shutdown,需要手动startup 之后手动同步。
startup
查询状态:
select open_mode,database_role from v$database;
开启实时应用:
alter database recover managed standby database using current logfile disconnect;
7.查看保护模式
select open_mode,database_role,protection_mode,protection_level,db_unique_name from v$database;
保护模式查看:
select name, protection_mode,protection_level from v$database;