资讯专栏INFORMATION COLUMN

PostgreSQL9.5:pg_rewind 快速恢复备节点

hersion / 1726人阅读

摘要:上操作备注执行抛出以上错误,错误内容很明显。再次上操作备注成功。启动原主库,上操作数据验证上操作备注成功,原主库现在是以备库角色启动,而且数据表也同步过来了。三原理四参考的主备切换使用搭建流复制环境

了解 PG 的朋友应该知道 PG 的主备切换并不容易,步骤较严谨,在激活备节点前需主动关闭主节点,否则再想以备节点角色拉起主节点会比较困难,之前博客介绍过主备切换,PostgreSQL HOT-Standby 的主备切换 ,PG 9.5 版本已经将 pg_rewind 加入到源码,当主备发生切换时,可以将原来主库通过同步模式恢复,避免重做备库。这样对于较大的库来说,节省了大量重做备库时间。

pg_rewind 会将目标库的数据文件,配置文件复制到本地目录,由于 pg_rewind 不会读取所有未发生变化的数据块,所以速度比重做备库要快很多,

一 环境准备

流复制环境
192.168.2.37/1931 主节点(主机名 db1)
192.168.2.38/1931 备节点(主机名 db2)
备注:流复制环境参考 PostgreSQL:使用 pg_basebackup 搭建流复制环境 , 本文略。

--pg_rewind 前提条件
1 full_page_writes
2 wal_log_hints 设置成 on 或者 PG 在初始化时开启 checksums 功能

二 主备切换

--备节点 recovery.conf 配置: db2 上操作

[pg95@db2 pg_root]$ grep ^[a-z] recovery.conf 
recovery_target_timeline = "latest"
standby_mode = on
primary_conninfo = "host=192.168.2.37 port=1931 user=repuser"           # e.g. "host=localhost port=5432"

--激活备节点: db2 上操作

[pg95@db2 pg_root]$ pg_ctl promote -D $PGDATA
server promoting

[pg95@db2 pg_root]$ pg_controldata | grep cluster
Database cluster state:               in production

--备节点激活后,创建一张测试表并插入数据

[pg95@db2 pg_root]$ psql
psql (9.5alpha1)
Type "help" for help.

postgres=# create table test_2(id int4);
CREATE TABLE
                   
postgres=# insert into test_2(id) select n from generate_series(1,10000) n;
INSERT 0 10000

--停原来主节点: db1 上操作

[pg95@db1 ~]$ pg_controldata | grep cluster
Database cluster state:               in production

[pg95@db1 ~]$ pg_ctl stop -m fast -D $PGDATA
waiting for server to shut down....... done
server stopped

备注:停完原主库后,千万不能立即以备节点形式拉起老库,否则在执行 pg_rewind 时会报,"target server must be shut down cleanly" 错误。

--pg_rewind: db1 上操作

[pg95@db1 pg_root]$ pg_ctl stop -m fast -D $PGDATA
waiting for server to shut down......... done
server stopped

[pg95@db1 pg_root]$ pg_rewind --target-pgdata $PGDATA --source-server="host=192.168.2.38 port=1931 user=postgres dbname=postgres" -P 
connected to server
target server needs to use either data checksums or "wal_log_hints = on"

备注:执行 pg_rewind 抛出以上错误,错误内容很明显。

--pg_rewind 代码分析

  364     /*
  365      * Target cluster need to use checksums or hint bit wal-logging, this to
  366      * prevent from data corruption that could occur because of hint bits.
  367      */
  368     if (ControlFile_target.data_checksum_version != PG_DATA_CHECKSUM_VERSION &&
  369         !ControlFile_target.wal_log_hints)
  370     {
  371         pg_fatal("target server needs to use either data checksums or "wal_log_hints = on"
");
  372     }
  373 

备注:数据库在 initdb 时需要开启 checksums 或者设置 "wal_log_hints = on", 接着设置主,备节点的 wal_log_hints 参数并重启数据库。

--再次 pg_rewind, db1 上操作

[pg95@db1 pg_root]$ pg_rewind --target-pgdata $PGDATA --source-server="host=192.168.2.38 port=1931 user=postgres dbname=postgres" -P
connected to server
The servers diverged at WAL position 0/1300CEB0 on timeline 5.
Rewinding from last common checkpoint at 0/1200008C on timeline 5
reading source file list
reading target file list
reading WAL in target
need to copy 59 MB (total source directory size is 76 MB)
61185/61185 kB (100%) copied
creating backup label and updating control file
Done!

备注:pg_rewind 成功。

--调整 recovery.conf 文件: db1 操作
[pg95@db1 ~]$ cd $PGDATA
[pg95@db1 pg_root]$ mv recovery.done recovery.conf

备注:注意是否需要修改 primary_conninfo 配置。

[pg95@db1 pg_root]$ grep ^[a-z] recovery.conf 
recovery_target_timeline = "latest"
standby_mode = on
primary_conninfo = "host=192.168.2.38 port=1931 user=repuser"           # e.g. "host=localhost port=5432"

--启动原主库, db1 上操作

[pg95@db1 pg_root]$ pg_ctl start -D $PGDATA
server starting

[pg95@db1 pg_root]$ pg_controldata | grep cluster
Database cluster state:               in archive recovery

--数据验证, db1 上操作

[pg95@db1 pg_root]$ psql
psql (9.5alpha1)
Type "help" for help.

postgres=# select count(*) from test_2;
 count 
-------
 10000
(1 row)

备注:pg_rewind 成功,原主库现在是以备库角色启动,而且数据表 test_2 也同步过来了。

三 pg_rewind 原理
The basic idea is to copy everything from the new cluster to the old cluster, except for the blocks that we know to be the same.

    1)Scan the WAL log of the old cluster, starting from the last checkpoint before the point where the new cluster"s timeline history forked off from the old cluster. For each WAL record, make a note of the data blocks that were touched. This yields a list of all the data blocks that were changed in the old cluster, after the new cluster forked off.

    2)Copy all those changed blocks from the new cluster to the old cluster.

    3)Copy all other files like clog, conf files etc. from the new cluster to old cluster. Everything except the relation files.

    4) Apply the WAL from the new cluster, starting from the checkpoint created at failover. (Strictly speaking, pg_rewind doesn"t apply the WAL, it just creates a backup label file indicating that when PostgreSQL is started, it will start replay from that checkpoint and apply all the required WAL.) 

四 参考

PostgreSQL HOT-Standby 的主备切换
PostgreSQL:使用 pg_basebackup 搭建流复制环境
pg_rewind

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/38973.html

相关文章

  • 阿里 HBase 超详实践总结:一文读懂大数据时代的结构化存储

    摘要:阿里是一家综合生态型公司,内部庞大业务矩阵高速发展,在基础存储方面,需要更好的功能灵活性基础设施适应性服务稳定性效率成本。今天,阿里会在多地部署多集群,集群间数据相互流动,以满足单元化业务的需求。 概述HBase 是一个开源的非关系型分布式数据库(NoSQL),基于谷歌的 BigTable 建模,是一个高可靠性、高性能、高伸缩的分布式存储系统,使用 HBase 技术可在廉价 PC Serve...

    fnngj 评论0 收藏0
  • 企业数字化转型,数据如何保护

    摘要:在企业这场数字化转型的马拉松赛跑中,聪明的正在寻求新的技术方案以保护企业的数据和业务安全,而英方不管在技术方案还是在实践案例方面,都以全新的奔跑姿态与们在同一条跑道的同一水平上。企业数字化转型就像一场马拉松赛跑,在漫长的赛道上,哪怕最顶级的选手,也有可能会被后来者赶超。因为在数字化进程中,除了业务方向跑对之外,企业的信息安全是会影响企业战略大局的关键。这绝非危言耸听,而是有事实依据。美国德克...

    bovenson 评论0 收藏0
  • 私有灾云解决方案

    摘要:灾备服务支持本地灾备异地灾备公有云灾备两地三中心等多种服务方式,可根据业务特点和需求,灵活选择灾备方式,保证业务的和。公有云灾备架构公有云灾备服务支持多种业务部署方式,为云平台业务提供不同指标,控制云平台业务灾备成本。UCloudStack 云平台通过分布式存储系统保证本地数据的安全性,同时通过远程数据备份服务,为用户提供远程数据备份和容灾备服务,可以将本地云端数据统一归档、备份至远程云...

    youkede 评论0 收藏0
  • Mongo、Redis、Memcached对比及知识总结

    摘要:当重启时,将会读取文件进行重放以恢复到关闭前的最后时刻。伸缩性受到线程数的限制,线程数的调度对也是不小的负担。所以允许我们设置线程池的大小,对需要从文件中加载相应数据的读取请求进行并发操作,减少阻塞的时间。 存储原理(持久化) Mongo Mongo的数据将会保存在底层文件系统,因此存储容量远大于redis和memcached。一个database中所有的collections以及索...

    孙淑建 评论0 收藏0
  • Mongo、Redis、Memcached对比及知识总结

    摘要:当重启时,将会读取文件进行重放以恢复到关闭前的最后时刻。伸缩性受到线程数的限制,线程数的调度对也是不小的负担。所以允许我们设置线程池的大小,对需要从文件中加载相应数据的读取请求进行并发操作,减少阻塞的时间。 存储原理(持久化) Mongo Mongo的数据将会保存在底层文件系统,因此存储容量远大于redis和memcached。一个database中所有的collections以及索...

    Vultr 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<