怎么使用pg_rewind
本篇内容主要讲解“怎么使用pg_rewind”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么使用pg_rewind”吧!
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=#createtabletest(idint,e_namevarchar(100),e_mailvarchar(100),d_idint);CREATETABLEpostgres=#\dListofrelationsSchema|Name|Type|Owner--------+------+-------+----------public|test|table|postgres(1row)postgres=#insertintotestvalues(1,'zbs','123@126.com',10);INSERT01postgres=#select*fromtest;id|e_name|e_mail|d_id----+--------+-------------+------1|zbs|123@126.com|10(1row)
m7 (丛):查询数据复制成功
[postgres@z_leader~]$psqlpostgrespsql(10.7)Type"help"forhelp.postgres=#\dListofrelationsSchema|Name|Type|Owner--------+------+-------+----------public|test|table|postgres(1row)postgres=#select*fromtest;id|e_name|e_mail|d_id----+--------+-------------+------1|zbs|123@126.com|10(1row)
提升丛库为新主库
[postgres@z_leaderdata]$pg_ctlpromote-D/usr/local/pg/datawaitingforservertopromote....doneserverpromoted[postgres@z_leaderdata]$psqlpostgrespsql(10.7)Type"help"forhelp.postgres=#selectpg_is_in_recovery();pg_is_in_recovery-------------------f(1row)
m1(原主库)插入一条记录,模拟原主库上的数据没有复制到原丛库上
postgres=#insertintotestvalues(2,'zbs1','124@126.com',10);INSERT01postgres=#select*fromtest;id|e_name|e_mail|d_id----+--------+-------------+------1|zbs|123@126.com|102|zbs1|124@126.com|10(2rows)
m7:在原丛库上(已提升为主库)插入一条记录并查看结果
postgres=#insertintotestvalues(3,'zbs2','124@126.com',10);INSERT01postgres=#select*fromtest;id|e_name|e_mail|d_id----+--------+-------------+------1|zbs|123@126.com|103|zbs2|124@126.com|10(2rows)
m1 将原主库变为新主库的丛库
[postgres@localhost~]$kill-INT`head-1/usr/local/pg/data/postmaster.pid`
--配置流复制文件和参数
[postgres@localhostdata]$mvrecovery.donerecovery.conf[postgres@localhostdata]$catrecovery.confstandby_mode='on'restore_command='cp/usr/local/pg/arch/%f'primary_conninfo='host=192.168.56.25port=5432user=rep'recovery_target_timeline='latest'[postgres@localhostdata]$
--启动数据库
[postgres@localhost~]$/usr/local/pg/bin/pg_ctl-D/usr/local/pg/data-llogfilestartwaitingforservertostart....doneserverstarted[postgres@localhostdata]$psqlpostgrespsql(10.7)Type"help"forhelp.postgres=#selectpg_is_in_recovery();pg_is_in_recovery-------------------t(1row)postgres=#select*fromtest;id|e_name|e_mail|d_id----+--------+-------------+------1|zbs|123@126.com|102|zbs1|124@126.com|10(2rows)
--在m7上插入的记录未能复制过来
---日志信息
2019-03-0209:15:17.415CST[2492]LOG:consistentrecoverystatereachedat0/D0000982019-03-0209:15:17.415CST[2492]LOG:invalidrecordlengthat0/D000098:wanted24,got02019-03-0209:15:17.415CST[2490]LOG:databasesystemisreadytoacceptreadonlyconnections2019-03-0209:15:17.429CST[2500]LOG:fetchingtimelinehistoryfilefortimeline6fromprimaryserver2019-03-0209:15:17.460CST[2500]FATAL:couldnotstartWALstreaming:ERROR:requestedstartingpoint0/D000000ontimeline5isnotinthisserver'shistoryDETAIL:Thisserver'shistoryforkedfromtimeline5at0/C003168.cp:missingdestinationfileoperandafter`/usr/local/pg/arch/00000006.history'Try`cp--help'formoreinformation.cp:missingdestinationfileoperandafter`/usr/local/pg/arch/00000007.history'Try`cp--help'formoreinformation.cp:missingdestinationfileoperandafter`/usr/local/pg/arch/00000006.history'Try`cp--help'formoreinformation.2019-03-0209:15:17.469CST[2492]LOG:newtimeline6forkedoffcurrentdatabasesystemtimeline5beforecurrentrecoverypoint0/D000098cp:missingdestinationfileoperandafter`/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.25port=5432user=postgresdbname=postgres'-PconnectedtoserverserversdivergedatWALlocation0/C003168ontimeline5rewindingfromlastcommoncheckpointat0/C003010ontimeline5readingsourcefilelistreadingtargetfilelistreadingWALintargetneedtocopy100MB(totalsourcedirectorysizeis118MB)102599/102599kB(100%)copiedcreatingbackuplabelandupdatingcontrolfilesyncingtargetdatadirectoryDone!
--pg_rewind后此文件需要重新配置
[postgres@localhostdata]$catrecovery.confstandby_mode='on'restore_command='cp/usr/local/pg/arch/%f'primary_conninfo='host=192.168.56.25port=5432user=rep'recovery_target_timeline='latest'
[postgres@localhost~]$/usr/local/pg/bin/pg_ctl-D/usr/local/pg/data-llogfilestartwaitingforservertostart....doneserverstarted[postgres@localhost~]$psqlpostgrespsql(10.7)Type"help"forhelp.postgres=#select*fromtest;id|e_name|e_mail|d_id----+--------+-------------+------1|zbs|123@126.com|103|zbs2|124@126.com|10(2rows)postgres=#selectpg_is_in_recovery();pg_is_in_recovery-------------------t(1row)
--原主库没有复制到丛库的记录消失,在新主库上插入的记录已同步
m7(新主库)[postgres@z_leader~]$psqlpostgrespsql(10.7)Type"help"forhelp.postgres=#insertintotestvalues(4,'zbs2','124@126.com',10);INSERT01postgres=#select*fromtest;id|e_name|e_mail|d_id----+--------+-------------+------1|zbs|123@126.com|103|zbs2|124@126.com|104|zbs2|124@126.com|10(3rows)
m1(新丛库)
postgres=#select*fromtest;id|e_name|e_mail|d_id----+--------+-------------+------1|zbs|123@126.com|103|zbs2|124@126.com|104|zbs2|124@126.com|10(3rows)
到此,相信大家对“怎么使用pg_rewind”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。