阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

PostgreSQL 之 pg_rewind使用详解

408次阅读
没有评论

共计 5594 个字符,预计需要花费 14 分钟才能阅读完成。

pg_rewind

是 postgresql 主丛数据库之同步数据目录的工具。需要目标服务器在 postgresql.conf 中允许 wal_log_hints,或者在 initdb 初始化集群时允许 checksums,full_page_writes 也必须为 on

pg_rewind 只复制表数据文件中更改的块; 所有其他文件都被完整复制,包括配置文件。pg_rewind 相对于使用 pg_basebackup 备份或 rsync 等工具的优势在于,pg_rewind 不需要读取数据库中未更改的块。这使得在数据库很大且之间只有一小部分块不同的情况下,速度会快得多。

pg_rewind [option…] {-D | –target-pgdata} directory {–source-pgdata=directory | –source-server=connstr

参数:

-D directory –target-pgdata=directory

  此选项指定与源同步的目标数据目录。在运行 pg_rewind 之前,必须干净关闭目标服务器

–source-pgdata=directory

  指定要与之同步的源服务器的数据目录的文件系统路径。此选项要求干净关闭源服务器

–source-server=connstr

指定要连接到源 PostgreSQL 服务器的 libpq 连接字符串。连接必须是具有超级用户访问权限的正常 (非复制) 连接。此选项要求源服务器正在运行,而不是处于恢复模式。

-n –dry-run

除了实际修改目标目录之外,执行所有操作。

-P –progress

使进展报告。

实验使用两台主机,都安装 postgresql-10.7,已配置流复制

  • 主:192.168.56.5 m1
  • 丛:192.168.56.25 m7

m1(主):创建测试表和数据

postgres=# create table test (id int,e_name varchar(100),e_mail varchar(100),d_id int);
CREATE TABLE
postgres=# \d
List of relations
Schema | Name | Type | Owner
——–+——+——-+———-
public | test | table | postgres
(1 row)
postgres=# insert into test values(1,’zbs’,’123@126.com’,10);
INSERT 0 1
postgres=# select * from test;
id | e_name | e_mail | d_id
—-+——–+————-+——
1 | zbs | 123@126.com | 10
(1 row)

m7 (丛):查询数据复制成功

[postgres@z_leader ~]$ psql postgres
psql (10.7)
Type “help” for help.
postgres=# \d
List of relations
Schema | Name | Type | Owner
——–+——+——-+———-
public | test | table | postgres
(1 row)
postgres=# select * from test;
id | e_name | e_mail | d_id
—-+——–+————-+——
1 | zbs | 123@126.com | 10
(1 row)

提升丛库为新主库

[postgres@z_leader data]$ pg_ctl promote -D /usr/local/pg/data
waiting for server to promote…. done
server promoted
[postgres@z_leader data]$ psql postgres
psql (10.7)
Type “help” for help.
postgres=# select pg_is_in_recovery();
pg_is_in_recovery
——————-
f
(1 row)

m1(原主库)插入一条记录,模拟原主库上的数据没有复制到原丛库上

postgres=# insert into test values(2,’zbs1′,’124@126.com’,10);
INSERT 0 1
postgres=# select * from test;
id | e_name | e_mail | d_id
—-+——–+————-+——
1 | zbs | 123@126.com | 10
2 | zbs1 | 124@126.com | 10
(2 rows)

m7:在原丛库上 (已提升为主库) 插入一条记录并查看结果

postgres=# insert into test values(3,’zbs2′,’124@126.com’,10);
INSERT 0 1
postgres=# select * from test;
id | e_name | e_mail | d_id
—-+——–+————-+——
1 | zbs | 123@126.com | 10
3 | zbs2 | 124@126.com | 10
(2 rows)

m1 将原主库变为新主库的丛库

[postgres@localhost ~]$ kill -INT `head -1 /usr/local/pg/data/postmaster.pid`

– 配置流复制文件和参数

[postgres@localhost data]$ mv recovery.done recovery.conf
[postgres@localhost data]$ cat recovery.conf
standby_mode = ‘on’
restore_command = ‘cp /usr/local/pg/arch/%f’
primary_conninfo = ‘host=192.168.56.25 port=5432 user=rep’
recovery_target_timeline = ‘latest’
[postgres@localhost data]$

– 启动数据库

[postgres@localhost ~]$ /usr/local/pg/bin/pg_ctl -D /usr/local/pg/data -l logfile start
waiting for server to start…. done
server started
[postgres@localhost data]$ psql postgres
psql (10.7)
Type “help” for help.
postgres=# select pg_is_in_recovery();
pg_is_in_recovery
——————-
t
(1 row)
postgres=# select * from test;
id | e_name | e_mail | d_id
—-+——–+————-+——
1 | zbs | 123@126.com | 10
2 | zbs1 | 124@126.com | 10
(2 rows)

– 在 m7 上插入的记录未能复制过来

— 日志信息

2019-03-02 09:15:17.415 CST [2492] LOG: consistent recovery state reached at 0/D000098
2019-03-02 09:15:17.415 CST [2492] LOG: invalid record length at 0/D000098: wanted 24, got 0
2019-03-02 09:15:17.415 CST [2490] LOG: database system is ready to accept read only connections
2019-03-02 09:15:17.429 CST [2500] LOG: fetching timeline history file for timeline 6 from primary server
2019-03-02 09:15:17.460 CST [2500] FATAL: could not start WAL streaming: ERROR: requested starting point 0/D000000 on timeline 5 is not in this
server’s history
DETAIL: This server’s history forked from timeline 5 at 0/C003168.
cp: missing destination file operand after `/usr/local/pg/arch/00000006.history’
Try `cp –help’ for more information.
cp: missing destination file operand after `/usr/local/pg/arch/00000007.history’
Try `cp –help’ for more information.
cp: missing destination file operand after `/usr/local/pg/arch/00000006.history’
Try `cp –help’ for more information.
2019-03-02 09:15:17.469 CST [2492] LOG: new timeline 6 forked off current database system timeline 5 before current recovery point 0/D000098
cp: missing destination file operand after `/usr/local/pg/arch/00000005000000000000000D

[postgres@localhost ~]$ kill -INT `head -1 /usr/local/pg/data/postmaster.pid`

— 使得 pg_rewind 同步数据库时间线

[[postgres@localhost ~]$ pg_rewind –target-pgdata /usr/local/pg/data –source-server=’host=192.168.56.25 port=5432 user=postgres dbname=postgres’ -P
connected to server
servers diverged at WAL location 0/C003168 on timeline 5
rewinding from last common checkpoint at 0/C003010 on timeline 5
reading source file list
reading target file list
reading WAL in target
need to copy 100 MB (total source directory size is 118 MB)
102599/102599 kB (100%) copied
creating backup label and updating control file
syncing target data directory
Done!

–pg_rewind 后此文件需要重新配置

[postgres@localhost data]$ cat recovery.conf
standby_mode = ‘on’
restore_command = ‘cp /usr/local/pg/arch/%f’
primary_conninfo = ‘host=192.168.56.25 port=5432 user=rep’
recovery_target_timeline = ‘latest’

[postgres@localhost ~]$ /usr/local/pg/bin/pg_ctl -D /usr/local/pg/data -l logfile start
waiting for server to start…. done
server started
[postgres@localhost ~]$ psql postgres
psql (10.7)
Type “help” for help.
postgres=# select * from test;
id | e_name | e_mail | d_id
—-+——–+————-+——
1 | zbs | 123@126.com | 10
3 | zbs2 | 124@126.com | 10
(2 rows)
postgres=# select pg_is_in_recovery();
pg_is_in_recovery
——————-
t
(1 row)

– 原主库没有复制到丛库的记录消失,在新主库上插入的记录已同步

m7(新主库)
[postgres@z_leader ~]$ psql postgres
psql (10.7)
Type “help” for help.
postgres=# insert into test values(4,’zbs2′,’124@126.com’,10);
INSERT 0 1
postgres=# select * from test;
id | e_name | e_mail | d_id
—-+——–+————-+——
1 | zbs | 123@126.com | 10
3 | zbs2 | 124@126.com | 10
4 | zbs2 | 124@126.com | 10
(3 rows)

m1(新丛库)

postgres=# select * from test;
id | e_name | e_mail | d_id
—-+——–+————-+——
1 | zbs | 123@126.com | 10
3 | zbs2 | 124@126.com | 10
4 | zbs2 | 124@126.com | 10
(3 rows)

正文完
星哥玩云-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-22发表,共计5594字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中

星哥玩云

星哥玩云
星哥玩云
分享互联网知识
用户数
4
文章数
19348
评论数
4
阅读量
7800510
文章搜索
热门文章
开发者必备神器:阿里云 Qoder CLI 全面解析与上手指南

开发者必备神器:阿里云 Qoder CLI 全面解析与上手指南

开发者必备神器:阿里云 Qoder CLI 全面解析与上手指南 大家好,我是星哥。之前介绍了腾讯云的 Code...
星哥带你玩飞牛NAS-6:抖音视频同步工具,视频下载自动下载保存

星哥带你玩飞牛NAS-6:抖音视频同步工具,视频下载自动下载保存

星哥带你玩飞牛 NAS-6:抖音视频同步工具,视频下载自动下载保存 前言 各位玩 NAS 的朋友好,我是星哥!...
云服务器部署服务器面板1Panel:小白轻松构建Web服务与面板加固指南

云服务器部署服务器面板1Panel:小白轻松构建Web服务与面板加固指南

云服务器部署服务器面板 1Panel:小白轻松构建 Web 服务与面板加固指南 哈喽,我是星哥,经常有人问我不...
我把用了20年的360安全卫士卸载了

我把用了20年的360安全卫士卸载了

我把用了 20 年的 360 安全卫士卸载了 是的,正如标题你看到的。 原因 偷摸安装自家的软件 莫名其妙安装...
星哥带你玩飞牛NAS-3:安装飞牛NAS后的很有必要的操作

星哥带你玩飞牛NAS-3:安装飞牛NAS后的很有必要的操作

星哥带你玩飞牛 NAS-3:安装飞牛 NAS 后的很有必要的操作 前言 如果你已经有了飞牛 NAS 系统,之前...
阿里云CDN
阿里云CDN-提高用户访问的响应速度和成功率
随机文章
终于收到了以女儿为原型打印的3D玩偶了

终于收到了以女儿为原型打印的3D玩偶了

终于收到了以女儿为原型打印的 3D 玩偶了 前些日子参加某网站活动,获得一次实物 3D 打印的机会,于是从众多...
开发者必备神器:阿里云 Qoder CLI 全面解析与上手指南

开发者必备神器:阿里云 Qoder CLI 全面解析与上手指南

开发者必备神器:阿里云 Qoder CLI 全面解析与上手指南 大家好,我是星哥。之前介绍了腾讯云的 Code...
手把手教你,购买云服务器并且安装宝塔面板

手把手教你,购买云服务器并且安装宝塔面板

手把手教你,购买云服务器并且安装宝塔面板 前言 大家好,我是星哥。星哥发现很多新手刚接触服务器时,都会被“选购...
免费领取huggingface的2核16G云服务器,超简单教程

免费领取huggingface的2核16G云服务器,超简单教程

免费领取 huggingface 的 2 核 16G 云服务器,超简单教程 前言 HuggingFace.co...
飞牛NAS玩转Frpc并且配置,随时随地直连你的私有云

飞牛NAS玩转Frpc并且配置,随时随地直连你的私有云

飞牛 NAS 玩转 Frpc 并且配置,随时随地直连你的私有云 大家好,我是星哥,最近在玩飞牛 NAS。 在数...

免费图片视频管理工具让灵感库告别混乱

一言一句话
-「
手气不错
星哥带你玩飞牛NAS硬件03:五盘位+N5105+双网口的成品NAS值得入手吗

星哥带你玩飞牛NAS硬件03:五盘位+N5105+双网口的成品NAS值得入手吗

星哥带你玩飞牛 NAS 硬件 03:五盘位 +N5105+ 双网口的成品 NAS 值得入手吗 前言 大家好,我...
星哥带你玩飞牛NAS硬件 01:捡垃圾的最爱双盘,暴风二期矿渣为何成不老神话?

星哥带你玩飞牛NAS硬件 01:捡垃圾的最爱双盘,暴风二期矿渣为何成不老神话?

星哥带你玩飞牛 NAS 硬件 01:捡垃圾的最爱双盘,暴风二期矿渣为何成不老神话? 前言 在选择 NAS 用预...
星哥带你玩飞牛 NAS-9:全能网盘搜索工具 13 种云盘一键搞定!

星哥带你玩飞牛 NAS-9:全能网盘搜索工具 13 种云盘一键搞定!

星哥带你玩飞牛 NAS-9:全能网盘搜索工具 13 种云盘一键搞定! 前言 作为 NAS 玩家,你是否总被这些...
小白也能看懂:什么是云服务器?腾讯云 vs 阿里云对比

小白也能看懂:什么是云服务器?腾讯云 vs 阿里云对比

小白也能看懂:什么是云服务器?腾讯云 vs 阿里云对比 星哥玩云,带你从小白到上云高手。今天咱们就来聊聊——什...
Prometheus:监控系统的部署与指标收集

Prometheus:监控系统的部署与指标收集

Prometheus:监控系统的部署与指标收集 在云原生体系中,Prometheus 已成为最主流的监控与报警...