本文主要分享Oracle一对多(一主多备)的DG环境的switchover切换,如何进行主从切换,切换后怎么恢复正常同步?
1、环境说明
本文的环境为一主两备,数据库版本为11.2.0.4,主要信息如下:
数据库IP | db_unique_name | 原角色(切换前) | 目标角色(切换后) |
---|---|---|---|
10.10.10.160 | ora11g | 主库 | 备库 |
10.10.10.41 | dgora11g | 备库 | 主库 |
10.10.10.47 | sbora11g | 备库 | 备库 |
说明:
(1)当前主库为10.10.10.160,10.10.10.41 和10.10.10.47是它的两个备库。
(2)switchover切换后,10.10.10.41变为主库,10.10.10.160和10.10.10.47成为它的两个备库。
2、主要参数配置
以下是相关数据库的主要参数配置信息(以db_unique_name进行区分):
ora11g:
set linesize 500 pages 0
col value for a100
col name for a50
select name, value
from v$parameter
where name in ('db_name','db_unique_name',
'log_archive_config',
'log_archive_dest_1','log_archive_dest_2',
'log_archive_dest_state_1',
'log_archive_dest_state_2',
'remote_login_passwordfile',
'log_archive_format',
'log_archive_max_processes',
'fal_server','db_file_name_convert',
'log_file_name_convert',
'standby_file_management')
/
db_file_name_convert /u01/oradata/dgora11g, /u01/oradata/ora11g, /u01/oradata/sbora11g, /u01/oradata/ora11g
log_file_name_convert /u01/oradata/dgora11g, /u01/oradata/ora11g, /u01/oradata/sbora11g, /u01/oradata/ora11g
log_archive_dest_1 location=/u01/oradata/archivelog VALID_FOR=(ALL_LOGFILES,ALL_ROLES) db_unique_name=ora11g
log_archive_dest_2 SERVICE=dgora11g LGWR ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) db_unique_name=dgora11g
log_archive_dest_state_1 ENABLE
log_archive_dest_state_2 ENABLE
fal_server dgora11g, sbora11g
log_archive_config DG_CONFIG=(ora11g,dgora11g,sbora11g)
log_archive_format %t_%s_%r.dbf
log_archive_max_processes 4
standby_file_management AUTO
remote_login_passwordfile EXCLUSIVE
db_name ora11g
db_unique_name ora11g
dgora11g:
db_file_name_convert /u01/oradata/ora11g, /u01/oradata/dgora11g
log_file_name_convert /u01/oradata/ora11g, /u01/oradata/dgora11g
log_archive_dest_1 location=/u01/oradata/archivelog VALID_FOR=(ALL_LOGFILES,ALL_ROLES) db_unique_name=dgora11g
log_archive_dest_2 SERVICE=ora11g LGWR ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) db_unique_name=ora11g
log_archive_dest_state_1 ENABLE
log_archive_dest_state_2 ENABLE
fal_server ora11g
log_archive_config DG_CONFIG=(ora11g,dgora11g)
log_archive_format %t_%s_%r.dbf
log_archive_max_processes 4
standby_file_management AUTO
remote_login_passwordfile EXCLUSIVE
db_name ora11g
db_unique_name dgora11g
sbora11g:
db_file_name_convert /u01/oradata/ora11g, /u01/oradata/sbora11g
log_file_name_convert /u01/oradata/ora11g, /u01/oradata/sbora11g
log_archive_dest_1 location=/u01/oradata/archivelog VALID_FOR=(ALL_LOGFILES,ALL_ROLES) db_unique_name=sbora11g
log_archive_dest_2 SERVICE=ora11g LGWR ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) db_unique_name=ora11g
log_archive_dest_state_1 ENABLE
log_archive_dest_state_2 ENABLE
fal_server ora11g
log_archive_config DG_CONFIG=(ora11g,sbora11g)
log_archive_format %t_%s_%r.dbf
log_archive_max_processes 4
standby_file_management AUTO
remote_login_passwordfile EXCLUSIVE
db_name ora11g
db_unique_name sbora11g
3、切换前检查
切换前,确认备库同步情况,有没有延迟
dgora11g:
sys@dgora11g> select value from v$dataguard_stats where name='transport lag';
VALUE
----------------------------------------------------------------
+00 00:00:00
sys@dgora11g> select value from v$dataguard_stats where name='apply lag';
VALUE
----------------------------------------------------------------
+00 00:00:00
--检查最新归档到的日志号及是否有gap
sys@dgora11g> SELECT UNIQUE THREAD# AS THREAD, MAX(SEQUENCE#) OVER (PARTITION BY thread#) AS LAST from V$ARCHIVED_LOG;
THREAD LAST
---------- ----------
1 289
sys@dgora11g> SELECT THREAD#, LOW_SEQUENCE#, HIGH_SEQUENCE# FROM V$ARCHIVE_GAP;
no rows selected
sbora11g:
sys@sbora11g > select value from v$dataguard_stats where name='transport lag';
VALUE
----------------------------------------------------------------
+00 00:00:00
sys@sbora11g > select value from v$dataguard_stats where name='apply lag';
VALUE
----------------------------------------------------------------
+00 00:00:00
sys@sbora11g > SELECT UNIQUE THREAD# AS THREAD, MAX(SEQUENCE#) OVER (PARTITION BY thread#) AS LAST from V$ARCHIVED_LOG;
THREAD LAST
---------- ----------
1 289
sys@sbora11g > SELECT THREAD#, LOW_SEQUENCE#, HIGH_SEQUENCE# FROM V$ARCHIVE_GAP;
no rows selected
检查确认两个备库实时同步,没有延时后,才能切换。
4、switchover切换
在切换时,在主库上是不可以选择我要切换到哪个备库的,这个选择是在备库上选择的,下面进行SWITCHOVER切换。
--主库状态
sys@ora11g> col name for a10;
sys@ora11g> set linesize 100;
sys@ora11g> select name,open_mode, database_role, switchover_status, force_logging, dataguard_broker, guard_status from v$database;
NAME OPEN_MODE DATABASE_ROLE SWITCHOVER_STATUS FOR DATAGUAR GUARD_S
---------- -------------------- ---------------- -------------------- --- -------- -------
ORA11G READ WRITE PRIMARY TO STANDBY YES DISABLED NONE
--备库状态
dgora11g:
sys@dgora11g> col name for a10;
sys@dgora11g> set linesize 100;
sys@dgora11g> select name,open_mode, database_role, switchover_status, force_logging, dataguard_broker, guard_status from v$database;
NAME OPEN_MODE DATABASE_ROLE SWITCHOVER_STATUS FOR DATAGUAR GUARD_S
---------- -------------------- ---------------- -------------------- --- -------- -------
ORA11G MOUNTED PHYSICAL STANDBY NOT ALLOWED YES DISABLED NONE
sbora11g:
sys@sbora11g > col name for a10;
sys@sbora11g > set linesize 100;
sys@sbora11g > select name,open_mode, database_role, switchover_status, force_logging, dataguard_broker, guard_status from v$database;
NAME OPEN_MODE DATABASE_ROLE SWITCHOVER_STATUS FOR DATAGUAR GUARD_S
---------- -------------------- ---------------- -------------------- --- -------- -------
ORA11G MOUNTED PHYSICAL STANDBY NOT ALLOWED YES DISABLED NONE
--将主库转为物理备库
ora11g:
alter database commit to switchover to physical standby with session shutdown;
切换后,这个数据库已经备关闭,启动数据库,这个数据库已经变为备库,启动MRP进程。
sys@ora11g> startup;
ORACLE instance started.
Total System Global Area 3691200512 bytes
Fixed Size 2258680 bytes
Variable Size 1392511240 bytes
Database Buffers 2281701376 bytes
Redo Buffers 14729216 bytes
Database mounted.
Database opened.
sys@ora11g> alter database recover managed standby database using current logfile disconnect from session;
Database altered.
或 alter database recover managed standby database using current logfile disconnect from session parallel 4;
--检查状态,可以看到已变为从库
sys@ora11g> select name,open_mode, database_role, switchover_status, force_logging, dataguard_broker, guard_status from v$database;
NAME OPEN_MODE DATABASE_ROLE SWITCHOVER_STATUS FOR DATAGUAR GUARD_S
---------- -------------------- ---------------- -------------------- --- -------- -------
ORA11G READ ONLY WITH APPLY PHYSICAL STANDBY TO PRIMARY YES DISABLED NONE
PS:主库切换后,两个备库均变为TO PRIMARY状态,这时就要选择切换哪个备库为主库了。
dgora11g:
sys@dgora11g> col name for a10;
sys@dgora11g> set linesize 100;
sys@dgora11g> select name,open_mode, database_role, switchover_status, force_logging, dataguard_broker, guard_status from v$database;
NAME OPEN_MODE DATABASE_ROLE SWITCHOVER_STATUS FOR DATAGUAR GUARD_S
---------- -------------------- ---------------- -------------------- --- -------- -------
ORA11G MOUNTED PHYSICAL STANDBY TO PRIMARY YES DISABLED NONE
sbora11g:
sys@sbora11g > col name for a10;
sys@sbora11g > set linesize 100;
sys@sbora11g > select name,open_mode, database_role, switchover_status, force_logging, dataguard_broker, guard_status from v$database;
NAME OPEN_MODE DATABASE_ROLE SWITCHOVER_STATUS FOR DATAGUAR GUARD_S
---------- -------------------- ---------------- -------------------- --- -------- -------
ORA11G MOUNTED PHYSICAL STANDBY TO PRIMARY YES DISABLED NONE
--备库切换成主库
这里选择sbora11g这个备库切换为主库:
sys@sbora11g > alter database commit to switchover to primary with session shutdown;
Database altered.
sys@sbora11g > alter database open;
Database altered.
sys@sbora11g > select name,open_mode, database_role, switchover_status, force_logging, dataguard_broker, guard_status from v$database;
NAME OPEN_MODE DATABASE_ROLE SWITCHOVER_STATUS FOR DATAGUAR GUARD_S
---------- -------------------- ---------------- -------------------- --- -------- -------
ORA11G READ WRITE PRIMARY TO STANDBY YES DISABLED NONE
5、新主库参数调整
log_archive_config、log_archive_dest_3还需加上从库dgora11g
sys@sbora11g > show parameter archive;
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
archive_lag_target integer 0
log_archive_config string DG_CONFIG=(ora11g,sbora11g)
log_archive_dest string
log_archive_dest_1 string location=/u01/oradata/archivel
og VALID_FOR=(ALL_LOGFILES,ALL
_ROLES) db_unique_name=sbora11
g
log_archive_dest_10 string
log_archive_dest_11 string
log_archive_dest_12 string
log_archive_dest_13 string
log_archive_dest_14 string
log_archive_dest_15 string
log_archive_dest_16 string
log_archive_dest_17 string
log_archive_dest_18 string
log_archive_dest_19 string
log_archive_dest_2 string SERVICE=ora11g LGWR ASYNC VALI
D_FOR=(ONLINE_LOGFILES,PRIMARY
_ROLE) db_unique_name=ora11g
............................................
log_archive_dest_3 string
--log_archive_config 需将三节点都加上
alter system set log_archive_config='DG_CONFIG=(sbora11g,ora11g,dgora11g)' scope=both sid='*';
alter system set log_archive_dest_3='SERVICE=dgora11g LGWR ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) db_unique_name=dgora11g' scope=both sid='*';
alter system set fal_server='ora11g','dgora11g' scope=both sid='*';
alter system set log_archive_dest_state_3=defer scope=both sid='*';
alter system set log_archive_dest_state_3=enable scope=both sid='*';
6、备库参数调整
--log_archive_config 若未设置,需将三节点都加上
--log_archive_config需将三节点都加上
--dgora11g 修改目标主库为sbora11g
sys@dgora11g> show parameter config;
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
dg_broker_config_file1 string /u01/app/oracle/product/11.2.0
/db/dbs/dr1dgora11g.dat
dg_broker_config_file2 string /u01/app/oracle/product/11.2.0
/db/dbs/dr2dgora11g.dat
log_archive_config string DG_CONFIG=(ora11g,dgora11g)
sys@dgora11g> alter system set log_archive_config='DG_CONFIG=(sbora11g,ora11g,dgora11g)' scope=both sid='*';
System altered.
--因为dgora11g这个数据库之前的主是ora11g(现在已经变成了备库),需要把dgora11g的主改成sbora11g。
alter system set log_archive_dest_2='SERVICE=sbora11g LGWR ASYNC VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE) db_unique_name=sbora11g' scope=both sid='*';
alter system set FAL_SERVER='ora11g','sbora11g';
若新的主库不在备库的DG_CONFIG配置里,主库会报错:
ARC3: Archivelog destination LOG_ARCHIVE_DEST_3 disabled: destination Data Guard configuration error
Archived Log entry 29 added for thread 1 sequence 300 ID 0x108ef4ac dest 1:
Fri Aug 09 20:16:12 2024
LNS: Standby redo logfile selected for thread 1 sequence 301 for destination LOG_ARCHIVE_DEST_2
--ora11g备库已经配置,不用修改
sys@ora11g> show parameter log_archive_config;
NAME TYPE VALUE
------------------------------------ ----------- ------------------------------
log_archive_config string DG_CONFIG=(ora11g,dgora11g,sbo
ra11g)
7、切换后验证
--主库插入测试数据
sys@sbora11g > create table tt0809 as select * from dba_objects;
Table created.
--两备库查询
sys@ora11g> select count(*) from tt0809;
COUNT(*)
----------
86266
sys@dgora11g> select count(*) from tt0809;
COUNT(*)
----------
86266
至此主从切换已完成,新主库应与两从库正常同步
关注我,学习更多的数据库知识