【YashanDB 知识库】数据库一主一备部署及一主两备部署时,主备手动切换方法及自动切换配置
本文内容来自 YashanDB 官网,原文内容请见 https://www.yashandb.com/newsinfo/7686588.html?templateId=1718516
问题现象
数据库在正常或异常情况下,如何实现主备切换
问题的风险及影响
数据库主备切换若没有正确配置,在数据库发生主节点故障时,会影响业务的使用
问题影响的版本
23.2 整个大版本
问题发生的原因
1、若主节点所在主机因为其他原因导致资源紧张的情况下,想要切换备节点为主节点更好的提供服务,此时就需要在数据库正常的情况下执行手动主备切换
2、数据库主节点异常时,若配置了自动主备切换,则数据库在心跳时间内会自动切换,若没有配置自动切换就只能执行手动切换
解决方法及规避方法
以下主要示例数据库一主一备部署和一主两备部署的情况
一主一备部署
1、手动切换
YashanDB 支持在主备库同步正常的情况下进行主备库的手动 Switchover 切换,也支持在主库异常的情况下进行备库的手动 Failover 切换,但在开启自动选主时无法使用 Failover 切换。
(1)Switchover 切换
Switchover 切换方式适用于主备库同步正常的情况,可选择任意一个备库执行操作。
● Switchover 过程中,主库已连接的会话将全部断连,并且主库不可连接新的会话,直到切换完成或失败。
● Switchover 过程中,如果主备网络断连,切换将失败。
● 如果备库 transport_lag 或 apply_lag 不为 0,即备库有待接收和回放的 redo,Switchover 过程可能时间较久,通过查询 V$RECOVERY_PROGRESS 视图查看剩余回放时间。
● Switchover 完成后,主备库会重新进行连接,将出现短暂的网络断连。
示例:
检查主备库同步状态,确认主备库同步无异常,并确认主备库的角色
yasboot cluster status -c yashandb -d

备节点执行 switchover 切换主备节点
ALTER DATABASE SWITCHOVER;

可以看到主备切换完成
(2)failover 切换
Failover 切换方式适用于主库损坏,或者服务器宕机等不可用时,必须立即进行故障切换,尽快恢复业务。
前提是未开启自动切换
yasboot election config show -c yashandb

备节点为 readonly 状态
SELECT database_id,database_name,open_mode,database_role,protection_mode FROM V$DATABASE;

kill 主节点进程,模拟主节点故障

备节点执行 failover 主备切换
ALTER DATABASE FAILOVER;
SELECT database_id,database_name,open_mode,database_role,protection_mode FROM V$DATABASE;

升主成功

接下来进行旧主降备操作,将旧主库实例启动到 MOUNT 状态
yasboot node start -c yashandb -n 1-2 -m mount

执行降备命令,并 OPEN 数据库
ALTER DATABASE CONVERT TO PHYSICAL STANDBY;
ALTER DATABASE OPEN;

2、自动切换
启用基于 yasom 的仲裁选主开关
yasboot election enable on -c yashandb

kill 主机点数据库服务,备节点会自动切主
yasboot cluster status -c yashandb -d

一主两备部署

查看主备库的保护模式

当前为最大保护模式
查看自动选主开关,主备节点都查看
SHOW PARAMETER HA_ELECTION_ENABLED;

当前为开启,若是关闭状态,执行 ALTER SYSTEM SET HA_ELECTION_ENABLED=TRUE; 开启,主备节点都执行
kill 主节点数据库服务,模拟主库挂点

主节点切换完成
问题分析方法及处理过程
数据库部署后先检查是否配置自动切换
(1)一主一备部署
yasboot election config show -c yashandb
若 Automatic Failover 为 DISABLED,则自动切换为关闭状态
执行 yasboot election enable on -c yashandb 开启
(2)一主两备部署
SHOW PARAMETER HA_ELECTION_ENABLED 查看,若为 TRUE 则为开启状态
否则执行 ALTER SYSTEM SET HA_ELECTION_ENABLED=TRUE 开启
经验总结
目前,数据库一主一备部署,自动切换时默认关闭的,需要手动开启;一主两备部署,数据库默认是最大保护模式,主备自动切换是默认开启的
版权声明: 本文为 InfoQ 作者【YashanDB】的原创文章。
原文链接:【http://xie.infoq.cn/article/1c7d97e126ee55782ca66ecee】。文章转载请联系作者。
评论