PolarDB开源数据库进阶课11 激活容灾(Standby)节点

本文涉及的产品
云原生数据库 PolarDB MySQL 版,通用型 2核8GB 50GB
云原生数据库 PolarDB PostgreSQL 版,标准版 2核4GB 50GB
简介: 本文介绍了如何激活PolarDB容灾(Standby)节点,实验环境依赖于Docker容器中用loop设备模拟共享存储。通过`pg_ctl promote`命令可以将Standby节点提升为主节点,使其能够接收读写请求。激活后,原Standby节点不能再成为PolarDB集群的Standby节点。建议删除对应的复制槽位以避免WAL文件堆积。相关操作和配置请参考系列文章及视频教程。

背景

穷鬼玩PolarDB RAC一写多读集群系列已经写了几篇:

本篇文章介绍一下如何激活容灾(Standby)节点? 实验环境依赖 《在Docker容器中用loop设备模拟共享存储》 , 如果没有环境, 请自行参考以上文章搭建环境.

本文还依赖如下文档:

DEMO

b站视频链接

Youtube视频链接

假设你的PolarDB Standby已按前面的文档在容器pb3中部署完成, Standby 节点正常运行中.

1、pb3, 查看一下Standby节点本地数据目录

$ cd ~/standby/  
  
$ ll  
total 344  
drwx------ 22 postgres postgres   4096 Dec 24 14:07 ./  
drwxr-x---  1 postgres postgres   4096 Dec 24 11:06 ../  
-rw-------  1 postgres postgres    227 Dec 24 09:52 backup_label.old  
-rw-------  1 postgres postgres 183650 Dec 24 09:52 backup_manifest  
drwx------  6 postgres postgres   4096 Dec 24 09:52 base/  
-rw-------  1 postgres postgres    163 Dec 24 14:07 current_logfiles  
drwx------  2 postgres postgres   4096 Dec 24 14:07 global/  
drwx------  2 postgres postgres   4096 Dec 24 14:07 pg_commit_ts/  
drwx------  2 postgres postgres   4096 Dec 24 09:52 pg_dynshmem/  
-rw-------  1 postgres postgres   4870 Dec 24 09:52 pg_hba.conf  
-rw-------  1 postgres postgres   1636 Dec 24 09:52 pg_ident.conf  
drwx------  2 postgres postgres   4096 Dec 24 14:07 pg_log/  
drwx------  4 postgres postgres   4096 Dec 24 14:22 pg_logical/  
drwx------  2 postgres postgres   4096 Dec 24 14:07 pg_logindex/  
drwx------  4 postgres postgres   4096 Dec 24 14:07 pg_multixact/  
drwx------  2 postgres postgres   4096 Dec 24 09:52 pg_notify/  
drwx------  2 postgres postgres   4096 Dec 24 09:52 pg_replslot/  
drwx------  2 postgres postgres   4096 Dec 24 09:52 pg_serial/  
drwx------  2 postgres postgres   4096 Dec 24 09:52 pg_snapshots/  
drwx------  2 postgres postgres   4096 Dec 24 09:52 pg_stat/  
drwx------  2 postgres postgres   4096 Dec 24 09:52 pg_stat_tmp/  
drwx------  2 postgres postgres   4096 Dec 24 09:58 pg_subtrans/  
drwx------  2 postgres postgres   4096 Dec 24 09:52 pg_tblspc/  
-rw-------  1 postgres postgres      3 Dec 24 09:52 PG_VERSION  
drwx------  2 postgres postgres   4096 Dec 24 14:07 pg_xact/  
drwx------  2 postgres postgres   4096 Dec 24 14:12 polar_cache_trash/  
drwx------  2 postgres postgres   4096 Dec 24 14:07 polar_fullpage/  
drwx------  2 postgres postgres   4096 Dec 24 09:52 polar_rel_size_cache/  
-rw-------  1 postgres postgres    100 Dec 24 14:07 polar_settings.conf  
-rw-------  1 postgres postgres      9 Dec 24 14:07 polar_shmem_stat_file  
-rw-------  1 postgres postgres    505 Dec 24 09:53 postgresql.auto.conf  
-rw-------  1 postgres postgres  31063 Dec 24 09:53 postgresql.conf  
-rw-------  1 postgres postgres     81 Dec 24 14:07 postmaster.opts  
-rw-------  1 postgres postgres     84 Dec 24 14:07 postmaster.pid  
-rw-------  1 postgres postgres      0 Dec 24 09:52 standby.signal

共享盘nvme2n1中的数据目录

$ sudo pfs -C disk ls /nvme2n1/shared_data/  
   Dir  1     256               Tue Dec 24 09:52:52 2024  pg_multixact  
   Dir  1     256               Tue Dec 24 09:52:52 2024  pg_wal  
   Dir  1     0                 Tue Dec 24 09:52:54 2024  pg_commit_ts  
  File  1     0                 Tue Dec 24 09:52:54 2024  standby.signal  
   Dir  1     512               Tue Dec 24 09:52:57 2024  polar_fullpage  
   Dir  1     0                 Tue Dec 24 09:52:57 2024  pg_replslot  
   Dir  1     0                 Tue Dec 24 09:52:57 2024  pg_tblspc  
  File  1     227               Tue Dec 24 09:52:57 2024  polar_non_exclusive_backup_label  
  File  1     257               Tue Dec 24 09:52:57 2024  postgresql.auto.conf  
   Dir  1     0                 Tue Dec 24 09:52:57 2024  pg_twophase  
   Dir  1     128               Tue Dec 24 09:52:57 2024  pg_xact  
   Dir  1     128               Tue Dec 24 09:53:37 2024  pg_logindex  
   Dir  1     7808              Tue Dec 24 09:52:57 2024  global  
   Dir  1     512               Tue Dec 24 09:52:58 2024  base  
total 16384 (unit: 512Bytes)

2、pb3, 激活容灾(Standby)节点

PS: 激活之前, 可以先观察standby和主集群或实时流式归档(如有)的WAL差异, 有差异的话可以通过文件拷贝到standby节点, 通过restore_command配置进行恢复. 尽量减少standby与上游的数据差异.

$ pg_ctl promote -D ~/standby  
  
日志如下, 有点多.  
[PFSD_SDK INF Dec 24 14:27:57.278019][94]pfsd_umount_force 247: pbdname nvme2n1  
[PFSD_SDK INF Dec 24 14:27:57.279200][94]chnl_connection_release_shm 1164: client umount return : deleted /var/run/pfsd//nvme2n1/94.pid  
[PFSD_SDK INF Dec 24 14:27:57.279212][94]pfsd_umount_force 261: umount success for nvme2n1  
waiting for server to promote...[PFSD_SDK INF Dec 24 14:27:57.380783][94]pfsd_umount_force 247: pbdname nvme2n1  
[PFSD_SDK INF Dec 24 14:27:57.382082][94]chnl_connection_release_shm 1164: client umount return : deleted /var/run/pfsd//nvme2n1/94.pid  
[PFSD_SDK INF Dec 24 14:27:57.382091][94]pfsd_umount_force 261: umount success for nvme2n1  
.[PFSD_SDK INF Dec 24 14:27:57.583592][94]pfsd_umount_force 247: pbdname nvme2n1  
  
...  
[PFSD_SDK INF Dec 24 14:28:52.847152][94]chnl_connection_release_shm 1164: client umount return : deleted /var/run/pfsd//nvme2n1/94.pid  
[PFSD_SDK INF Dec 24 14:28:52.847161][94]pfsd_umount_force 261: umount success for nvme2n1  
 done  
server promoted

激活后是一个正常实例, 可以接收读写请求了

$ psql  
psql (PostgreSQL 15.10 (PolarDB 15.10.2.0 build d4f5477d debug) on aarch64-linux-gnu)  
Type "help" for help.  
  
postgres=# select pg_is_in_recovery();  
 pg_is_in_recovery   
-------------------  
 f  
(1 row)  
  
postgres=# create table t_new (id int);  
CREATE TABLE  
postgres=# insert into t_new values (1),(2),(3);  
INSERT 0 3  
postgres=# select * from t_new;  
 id   
----  
  1  
  2  
  3  
(3 rows)  
  
postgres=# \dt  
         List of relations  
 Schema | Name  | Type  |  Owner     
--------+-------+-------+----------  
 public | t     | table | postgres  
 public | t1    | table | postgres  
 public | t2    | table | postgres  
 public | t_new | table | postgres  
 public | tbl   | table | postgres  
(5 rows)

注意: PolarDB standby激活之后, 不能再成为原来PolarDB集群的Standby. 如果是PostgreSQL社区版本的话激活之后通过pg_rewind可以回退到过去, PolarDB standby如果部署在ZFS上, 可以通过zfs快照克隆来进行回退, 省去重建Standby的动作.

观察本地数据文件目录和共享盘nvme2n1中的数据目录, 对比激活前后只有一处不同. 本地数据文件目录的standby.signal文件不存在了, 而共享盘中的还在, 说明signal文件起作用的是本地数据目录. 共享盘中的standby.signal可以删掉.

$ sudo pfs -C disk rm /nvme2n1/shared_data/standby.signal

因为这种标记文件决定的是计算节点的角色, 所以只和计算节点相关, 和共享存储无关. 不会放在共享存储里, 想想看如果放在共享存储里, 那有多个计算节点时, 到底谁时RO呢?

激活之后, 如果上游节点还活着, 建议到PolarDB primary节点删除对应standby的slot, 以免导致wal文件堆积.

postgres=# select pg_drop_replication_slot('standby1');
postgres=# select * from pg_replication_slots ;
    slot_name     | plugin | slot_type | datoid | database | temporary | active | active_pid | xmin | catalog_xmin | restart_lsn | confirmed_flush_lsn | wal_status | safe_wal_size | two_phase 
------------------+--------+-----------+--------+----------+-----------+--------+------------+------+--------------+-------------+---------------------+------------+---------------+-----------
 replica1         |        | physical  |        |          | f         | t      |        208 |      |              | 1/C7AFA000  |                     | reserved   |               | f
 online_archwal_1 |        | physical  |        |          | f         | t      |        226 |      |              | 1/C7AFA100  |                     | reserved   |               | f
(2 rows)

参考

《穷鬼玩PolarDB RAC一写多读集群系列 | 在Docker容器中用loop设备模拟共享存储》

《穷鬼玩PolarDB RAC一写多读集群系列 | 如何搭建PolarDB容灾(Standby)节点》

《穷鬼玩PolarDB RAC一写多读集群系列 | 共享存储在线扩容》

《穷鬼玩PolarDB RAC一写多读集群系列 | 计算节点 Switchover》

《穷鬼玩PolarDB RAC一写多读集群系列 | 在线备份》

《穷鬼玩PolarDB RAC一写多读集群系列 | 在线归档》

《穷鬼玩PolarDB RAC一写多读集群系列 | 实时归档》

《穷鬼玩PolarDB RAC一写多读集群系列 | 时间点恢复(PITR)》

《穷鬼玩PolarDB RAC一写多读集群系列 | 读写分离》

《穷鬼玩PolarDB RAC一写多读集群系列 | 主机全毁, 只剩共享存储的PolarDB还有救吗?》

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
打赏
0
1
1
0
20691
分享
相关文章
【YashanDB知识库】OM仲裁节点故障后手工切换方案和yasom仲裁重新部署后重新纳管数据库集群方案
本文介绍了主备数据库集群的部署、OM仲裁故障切换及重新纳管的全过程。首先通过解压软件包并调整安装参数完成数据库集群部署,接着说明了在OM仲裁故障时的手动切换方案,包括关闭自动切换开关、登录备节点执行切换命令。最后详细描述了搭建新的yasom仲裁节点以重新纳管数据库集群的步骤,如生成配置文件、初始化进程、执行托管命令等,确保新旧系统无缝衔接,保障数据服务稳定性。
喜报|PolarDB开源社区荣获“2024数据库国内活跃开源项目”奖
喜报|PolarDB开源社区荣获“2024数据库国内活跃开源项目”奖
首届全国大学生计算机系统能力大赛PolarDB数据库创新设计赛(天池杯)圆满收官
首届全国大学生计算机系统能力大赛PolarDB数据库创新设计赛(天池杯)圆满收官
世界第一!阿里云PolarDB登顶全球数据库性能及性价比排行榜!
2月26日,阿里云PolarDB在2025开发者大会上登顶全球数据库性能及性价比排行榜。此次突破标志着中国基础软件取得里程碑成就,PolarDB凭借创新的云原生架构,成功应对全球最大规模并发交易峰值,在性能、可扩展性等方面领先全球。
【YashanDB知识库】原生mysql驱动配置连接崖山数据库
【YashanDB知识库】原生mysql驱动配置连接崖山数据库
【YashanDB知识库】原生mysql驱动配置连接崖山数据库
docker拉取MySQL后数据库连接失败解决方案
通过以上方法,可以解决Docker中拉取MySQL镜像后数据库连接失败的常见问题。关键步骤包括确保容器正确启动、配置正确的环境变量、合理设置网络和权限,以及检查主机防火墙设置等。通过逐步排查,可以快速定位并解决连接问题,确保MySQL服务的正常使用。
124 82
缓存与数据库的一致性方案,Redis与Mysql一致性方案,大厂P8的终极方案(图解+秒懂+史上最全)
缓存与数据库的一致性方案,Redis与Mysql一致性方案,大厂P8的终极方案(图解+秒懂+史上最全)

相关产品

  • 云原生数据库 PolarDB