前言:
DataGuard FarSync实例是Oracle12.1之后推出的DataGuard同步新特性,通过在主库距离较近的环境部署FarSync 实例(实例只需要参数文件以及控制文件,没有数据文件),作为主备之间的日志传输中转站,避免由于网络延迟问题带来的主备传输延时影响主库,特别是在最大可用以及最大性能模式下,网络延迟可能导致主库事务提交出现等待或者挂死的情况。
什么是Far Sync
Far Sync Instance功能的实现是通过在距离主库相对较近的地点部署far Sync实例,主库同步(sync)传输redo到Far Sync实例,然后Far Sync实例再将redo异步(async)传输到终端备库。这样既可以保证零数据丢失又可以降低主库压力。Far Sync实例只有密码文件,init参数文件和控制文件,没有数据文件。
如果redo 传输采用最大可用(Maximum Availability)模式,我们可以在距离生产中心主库相对较近的地点部署Far Sync实例,主库同步(sync)传输redo到Far Sync实例,这样保证零数据丢失,同时主库和Far Sync距离较近,网络延时很小,因此对主库性能影响很小。然后Far Sync实例再将redo异步(async)发送到远端备库。
如果redo 传输采用最大性能(Maximum Performance)模式,我们可以在距离生产中心主库相对较近的地点配置Far Sync实例,主库异步传输redo到Far Sync实例,然后Far Sync实例再负责传输redo到其他多个远端备库。这样可以减少主库向多个远端备库传输redo的压力,资源消耗。
Far Sync配置对于Data Guard 角色转换是透明的,即switchover/failover命令方式与12c之前相同。考虑到可能发生Data Guard 角色转换,即switchover/failover,可以在距离备库较近的地方也配置Far Sync实例,这个Far Sync实例只有在当前的备库切换为主库后才启用。而对于Far Sync实例的单点故障,可以在距离主库较近的地点配置2个Far Sync实例,起到高可用的作用。
配置Far Sync
架构:
角色 | 唯一名 | IP |
主库 | orcl | 192.168.2.241 |
备库 | orcldg | 192.168.2.243 |
FarSync | orclfar | 192.168.2.244 |
部署Far Sync之前,先按正常的模式部署一套ADG(一主一备),部署方法这里不展开。
接下来,开始配置Far Sync实例
配置FarSync实例的连接串tns
---FarSync实例的tns要添加到每个主备的tnsnames.ora
---主备的连接串也要添加到FarSync实例的tnsnames.ora
---主库的tns
orcl =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.2.241)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcl)
)
)
---备库的tns
orcldg =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.2.243)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orcldg)
)
)
---FarSync实例的tns
orclfar =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.2.244)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = orclfar)
)
)
配置FarSync实例init文件
--注意
--unique_name要跟主备区分
--log_archive_config,主备+farsync实例
--log_archive_dest_2参数,要指定到备库,并且类型为standby log
*.audit_file_dest='/u01/app/oracle/admin/orclfar/adump'
*.audit_trail='db'
*.compatible='19.0.0'
*.control_files='/u01/app/oracle/oradata/orclfar/control01.ctl'
*.db_block_size=8192
*.db_name='orcl'
*.db_unique_name='orclfar'
*.diagnostic_dest='/u01/app/oracle'
*.fal_client='orclfar'
*.fal_server='orcl'
*.log_archive_config='dg_config=(orcl,orcldg,orclfar)'
*.log_archive_dest_1='location=/u01/app/oracle/oradata/arch'
*.log_archive_dest_2='service=orcldg LGWR ASYNC NOAFFIRM delay=0 optional compression=disable max_failure=0 reopen=300 net_timeout=30 valid_for=(STANDBY_LOGFILES,STANDBY_ROLE) db_unique_name=orcldg'
*.nls_language='AMERICAN'
*.nls_territory='AMERICA'
*.open_cursors=300
*.pga_aggregate_target=368m
*.processes=300
*.remote_login_passwordfile='EXCLUSIVE'
*.sga_target=1102m
*.standby_file_management='AUTO'
*.db_create_file_dest='/u01/app/oracle/oradata'
启动实例到nomount
---创建所需的目录
mkdir -p /u01/app/oracle/admin/orclfar/adump
mkdir -p /u01/app/oracle/oradata/orclfar
mkdir -p /u01/app/oracle/oradata/arch
---启动到nomunt
export ORACLE_SID=orclfar
startup nomount
ORACLE instance started.
Total System Global Area 1157627160 bytes
Fixed Size 8895768 bytes
Variable Size 301989888 bytes
Database Buffers 838860800 bytes
Redo Buffers 7880704 bytes
创建FarSync实例的控制文件,并启动到mount状态
---在主库创建FarSync类型的控制文件
ALTER DATABASE CREATE FAR SYNC INSTANCE CONTROLFILE AS '/backup/control01.ctl';
---将控制文件和密码文件拷贝到FarSync实例
scp control01.ctl orapworclfar [email protected]:/backup
---恢复控制文件和密码文件到指定init路径
---启动FarSync实例到mount
alter database mount;
---实例的类型为FarSync
SQL> select database_role,controlfile_type
2 from v$database;
DATABASE_ROLE CONTROL
---------------- -------
FAR SYNC FARSYNC
---启动监听
lsnrctl start
FarSync实例添加standby log
--还是按thread#+1,大小和redo log保持一致
alter database add standby logfile thread 1 group 10 size 200M;
alter database add standby logfile thread 1 group 11 size 200M;
alter database add standby logfile thread 1 group 12 size 200M;
配置主库参数
--添加orclfar到主备配置
alter system set log_archive_config='dg_config=(orcl,orcldg,orclfar)';
--添加sync链路到FarSync实例,链路配置了备用链路三,指向备库,当orclfar出现问题时,可用自由切换
alter system set log_archive_dest_2='service=orclfar LGWR SYNC AFFIRM delay=0 optional ALTERNATE=LOG_ARCHIVE_DEST_3 compression=disable max_failure=1 reopen=300 net_timeout=30 valid_for=(ONLINE_LOGFILES,PRIMARY_ROLE) db_unique_name=orclfar'
--配置备用链路指向备库
alter system set LOG_ARCHIVE_DEST_STATE_3='ALTERNATE';
alter system set LOG_ARCHIVE_DEST_3='SERVICE=orcldg ASYNC ALTERNATE=LOG_ARCHIVE_DEST_2 VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=orcldg';
alter system set LOG_ARCHIVE_DEST_STATE_2='ENABLE';
--也可以配置备用链路指向第二个FarSync
#LOG_ARCHIVE_CONFIG='DG_CONFIG=(chicago,chicagoFS,chicagoFS1,boston)'
#LOG_ARCHIVE_DEST_STATE_2='ENABLE'
#LOG_ARCHIVE_DEST_2='SERVICE=chicagoFS SYNC AFFIRM MAX_FAILURE=1 ALTERNATE=LOG_ARCHIVE_DEST_3 VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=chicagoFS'
#LOG_ARCHIVE_DEST_STATE_3='ALTERNATE'
#LOG_ARCHIVE_DEST_3='SERVICE=chicagoFS1 SYNC AFFIRM ALTERNATE=LOG_ARCHIVE_DEST_2 VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) DB_UNIQUE_NAME=chicagoFS1'
配置备库参数
--添加orclfar到主备配置
alter system set log_archive_config='dg_config=(orcl,orcldg,orclfar)';
--配置fal_server
alter system set fal_server='orcl','orclfar';
配置完成,检查配置情况
--查看dataguard_config
select * from V$DATAGUARD_CONFIG;
DB_UNIQUE_ PARENT_DBUN DEST_ROLE CURRENT_SCN CON_ID
---------- ------------------------------------------------------------ ---------------------------------- ----------- ----------
orcl NONE PRIMARY DATABASE 2845624 0
orclfar orcl FAR SYNC INSTANCE 2844863 0
orcldg orclfar PHYSICAL STANDBY 2844863 0
---查看链路情况
THREAD# DEST_ID DEST_NAME TARGET DATABASE_MODE STATUS ERROR RECOVERY_M DB_UNIQUE_NAME DESTINATION CURRENT_SEQ# LAST_ARCHIVED APPLIED_SEQ#
---------- ---------- -------------------- ---------- --------------- ---------- ---------- ---------- --------------- --------------- ------------ ------------- ------------
1 1 LOG_ARCHIVE_DEST_1 LOCAL PRIM OPEN VALID IDLE NONE /u01/app/oracle 86 85
ARY /oradata/arch
1 2 LOG_ARCHIVE_DEST_2 FAR SYNC S MOUNTED VALID IDLE orclfar orclfar 86 85 83
TANDBY
3 LOG_ARCHIVE_DEST_3 UNKNOWN ST UNKNOWN ALTERNATE IDLE orcldg orcldg
ANDBY
---查看同步情况
select * FROM V$DATAGUARD_STATS;SQL> SQL>
SOURCE_DBID SOURCE_DB_UNIQUE_NAME NAME VALUE UNIT
----------- -------------------------------- -------------------------------- -------------------------------------------------- ------------------------------
TIME_COMPUTED DATUM_TIME CON_ID
------------------------------ ------------------------------ ----------
1637477139 orclfar transport lag +00 00:00:00 day(2) to second(0) interval
07/12/2022 00:14:17 07/12/2022 00:14:17 0
1637477139 orclfar apply lag +00 00:00:00 day(2) to second(0) interval
07/12/2022 00:14:17 07/12/2022 00:14:17 0
1637477139 orclfar apply finish time +00 00:00:00.000 day(2) to second(3) interval
07/12/2022 00:14:17 0
0 estimated startup time 8 second
07/12/2022 00:14:17 0
测试FarSync
1 从后台查看日志的同步情况
主库先通过log_archive_dest_2 SYNC同步给FarSync实例
FarSync实例再通过log_archive_dest_2 ASYNC同步给备库
备库最后再进行应用
2 数据同步测试
--主库创建test20220711
SQL> select database_role,db_unique_name from v$database;
DATABASE_ROLE
--------------------------------
DB_UNIQUE_NAME
------------------------------------------------------------
PRIMARY
orcl
create table test20220711 as select * from dba_objects;
SQL>
Table created.
--备库实时同步
SQL> select database_role,db_unique_name from v$database;
DATABASE_ROLE DB_UNIQUE_NAME
---------------- ------------------------------
PHYSICAL STANDBY orcldg
SQL> select count(*) from test20220711;
COUNT(*)
----------
72579
3 表空间创建同步测试
--主库创建表空间
select database_role,db_unique_name from v$database;
SQL>
DATABASE_ROLE DB_UNIQUE_NAME
-------------------------------- ------------------------------------------------------------
PRIMARY orcl
SQL>
SQL> create tablespace test20220711 datafile size 100M;
Tablespace created.
--备库实时同步
SQL> select tablespace_name,file_name from dba_data_files where tablespace_name='TEST20220711';
TABLESPACE_NAME
------------------------------
FILE_NAME
--------------------------------------------------------------------------------
TEST20220711
/u01/app/oracle/oradata/ORCLDG/datafile/o1_mf_test2022_kdrm0ln7_.dbf
4 测试备用链路切换
--断开FarSync实例的网卡
ifdow ens33
--可用看到主库日志链路自动切换到链路3,重新enable
Thread 1 cannot allocate new log, sequence 104
Checkpoint not complete
Current log# 1 seq# 103 mem# 0: /u01/app/oracle/oradata/ORCL/redo01.log
LGWR (PID:3148): Failed to archive LNO:1 T-1.S-103, error=16198
2022-07-12T00:41:08.658141+08:00
LGWR (PID:3148): ORA-16198: Received timed out error from KSR
LGWR (PID:3148): Error 16198 disconnecting from LAD:2 standby host 'orclfar'
2022-07-12T00:41:08.658530+08:00
LGWR (PID:3148): LAD:2 no longer supports SYNCHRONIZATION
2022-07-12T00:41:08.664655+08:00
Thread 1 advanced to log sequence 104 (LGWR switch), current SCN: 2852240
Current log# 2 seq# 104 mem# 0: /u01/app/oracle/oradata/ORCL/redo02.log
2022-07-12T00:41:08.684819+08:00
ARC1 (PID:3228): Archived Log entry 184 added for T-1.S-103 ID 0x61997c13 LAD:1
2022-07-12T00:41:08.761728+08:00
TT05 (PID:7783): SRL selected for T-1.S-104 for LAD:3
2022-07-12T00:41:08.837937+08:00
ALTER SYSTEM SET log_archive_dest_state_3='ENABLE' SCOPE=MEMORY SID='*';
--但这出现了一个问题,就是备库一直不应用日志,重启mrp进程才回继续应用,不知道是我测试环境资源太少的问题导致,还是Oracle bug
--网络恢复后,log_archive_dest_要想重新恢复到FarSync,需要把备用的log_archive_dest_3清空,重新设置log_archive_dest_2 才会自动切换回原来的链路
5 主备切换测试
--这里用的是12c之后新的切换命令,比较简单,命令自动完成
--验证切换的备库,是否具备条件,如果有错误的话,会显示warning,并在后台日志打印详细的信息
ALTER DATABASE SWITCHOVER TO orcldg VERIFY;
Database altered.
--验证没问题之后,执行切换
ALTER DATABASE SWITCHOVER TO orcldg;
Database altered.
--切换成功完成,orcl为备库,orcldg为主库
--在FarSync模式下,主备切换还是跟之前一样,切换之后,再重新指定配置FarSync即可
1* select database_role,db_unique_name from v$database
DATABASE_ROLE DB_UNIQUE_NAME
-------------------------------- ------------------------------------------------------------
PHYSICAL STANDBY orcl
SQL>
SQL> select database_role,db_unique_name from v$database;
DATABASE_ROLE DB_UNIQUE_NAME
---------------- ------------------------------
PRIMARY orcldg