pg流复制实施步骤.docx

上传人:b****1 文档编号:2023687 上传时间:2023-05-02 格式:DOCX 页数:22 大小:25.67KB
下载 相关 举报
pg流复制实施步骤.docx_第1页
第1页 / 共22页
pg流复制实施步骤.docx_第2页
第2页 / 共22页
pg流复制实施步骤.docx_第3页
第3页 / 共22页
pg流复制实施步骤.docx_第4页
第4页 / 共22页
pg流复制实施步骤.docx_第5页
第5页 / 共22页
pg流复制实施步骤.docx_第6页
第6页 / 共22页
pg流复制实施步骤.docx_第7页
第7页 / 共22页
pg流复制实施步骤.docx_第8页
第8页 / 共22页
pg流复制实施步骤.docx_第9页
第9页 / 共22页
pg流复制实施步骤.docx_第10页
第10页 / 共22页
pg流复制实施步骤.docx_第11页
第11页 / 共22页
pg流复制实施步骤.docx_第12页
第12页 / 共22页
pg流复制实施步骤.docx_第13页
第13页 / 共22页
pg流复制实施步骤.docx_第14页
第14页 / 共22页
pg流复制实施步骤.docx_第15页
第15页 / 共22页
pg流复制实施步骤.docx_第16页
第16页 / 共22页
pg流复制实施步骤.docx_第17页
第17页 / 共22页
pg流复制实施步骤.docx_第18页
第18页 / 共22页
pg流复制实施步骤.docx_第19页
第19页 / 共22页
pg流复制实施步骤.docx_第20页
第20页 / 共22页
亲,该文档总共22页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

pg流复制实施步骤.docx

《pg流复制实施步骤.docx》由会员分享,可在线阅读,更多相关《pg流复制实施步骤.docx(22页珍藏版)》请在冰点文库上搜索。

pg流复制实施步骤.docx

pg流复制实施步骤

PostgreSQL12.2流复制配置手册

一主备机器规划主机名

角色主机名ip

Maswer:

Pg1192.168.18.211

Slave:

Pg2192.168.18.212

PG版本:

用源码编译安装的12.2版本

二创建流复制

2.1设置host

master,slave两节点都要操作。

#vim/etc/hosts

#编辑内容如下:

192.168.18.211pg1

192.168.18.212pg2

2.2在主库设置

2.2.1先初始化新数据库

$initdb-D/usr/local/pg12.1/data–Upostgres

2.2.2启动数据库并建立同步用户

$pg_ctl-D/usr/local/pg12.1/datastart

创建用户:

createrole同步用的用户名loginreplicationencryptedpassword'密码';

postgres=#createrolereplloginreplicationencryptedpassword'oracle';

CREATEROLE

postgres=#\q--退出psql

2.2.3配置$PGDATA/data/pg_hba.conf,添加下面内容:

格式:

hostreplication同步用的用户名备库IP地址或域名/24trust

hostreplicationreplpg2trust

hostreplicationrepl192.168.18.0/24trust

hostallall192.168.18.0/24trust

2.2.4配置主备库的postgres.con文件,因为以后要进行角色切换,所有现在都添加一样的参数。

主库配置~/data/postgres.conf查找并修改成以下内容

listen_addresses='*'

wal_level=replica--10以后的版本为replica主从设置为在线模式,流复制必选

max_wal_senders=10--流复制允许连接进程,主备库这个参数值必须一样

wal_keep_segments=64

archive_mode=on--设置归档模式

archive_command='cp%p/home/postgres/arch/%f'--设置归档cp命令

listen_addresses='*'

wal_level=replica

max_wal_senders=20

wal_keep_segments=64

archive_mode=on

archive_command='cp%p/home/postgres/arch/%f'

restore_command='cp/home/postgres/arch/%f%p'

recovery_target_timeline='latest'

full_page_writes=on

wal_log_hints=on

2.2.5重启主库服务,以更新配置

$pg_ctl-D~/data/-l~/log/pglog.logrestart

2.3在备库设置

2.3.1不需要初始化,直接从主库备份就行,如有DATA直接删掉或改名掉:

$pg_basebackup-hpg1-p1922-Urepl-R-Fp-P-D$PG_HOME/data

备注:

-h,主库主机,-p,主库服务端口;

-U,复制用户;

-F,p是默认输出格式,输出数据目录和表空间相同的布局,t表示tar格式输出;

-P,同--progress,显示进度;

-D,输出到指定目录;

-R创建一个recovery.conf文件,10版本后就没有该文件,改为standby.signal文件,需要自己创建,所以该参数可以省略

如果报错:

pg_basebackup:

error:

couldnotconnecttoserver:

couldnotconnecttoserver:

NoroutetohostIstheserverrunningonhost"pg1"(192.168.18.211)andacceptingTCP/IPconnectionsonport1922?

解决方法:

发现是系统防火墙的问题:

#查看防火墙状态

serviceiptablesstatus

#停止防火墙

serviceiptablesstop

#永久关闭防火墙

chkconfigiptablesoff

2.3.2备库修改配置文件

postgres@NanoPI-006:

~$vi~/data/postgresql.conf

添加以下内容

listen_addresses='*'

wal_level=replica

max_wal_senders=20

wal_keep_segments=64

archive_mode=on

archive_command='cp%p/home/postgres/arch/%f'

restore_command='cp/home/postgres/arch/%f%p'

recovery_target_timeline='latest'

full_page_writes=on

wal_log_hints=on

hot_standby=on#在备份的同时允许查询,默认值max_standby_streaming_delay=30s#可选,流复制最大延迟wal_receiver_status_interval=10s#可选,从向主报告状态的最大间隔时间hot_standby_feedback=on#可选,查询冲突时向主反馈

2.3.3配置~/data/pg_hba.conf

添加下面内容

hostreplicationreplpg1trust

hostreplicationrepl192.168.18.0/24trust

hostallall192.168.18.0/24trust

#在备库中维护的主库IP地址是为了以后切换使用

2.3.4、创建备库文件standby.signal

primary_conninfo='host=pg1port=1922user=replpassword=oracleoptions=''-cwal_sender_timeout=5000'''

restore_command='cp/home/postgres/arch/%f%p'

archive_cleanup_command='pg_archivecleanup/home/postgres/arch%r'

standby_mode=on

第一行参数:

#连接到主库信息

第二行参数:

将来变成主库时需要用到的参数。

第三行参数:

变成主库后需要清空的归档日志。

第四行参数:

把备库变成read-onlytransaction模式,不允许进行写操作。

允许查询。

这一点非常好。

2.3.5启动备库数据服务

$pg_ctl-D$PGDATA-l~/log/pglog.logstart

增加日志输出设置,从而获得更多的信息。

三、验证

3.1、观察主从两库的归档日志的位置,或者主库两边的pg_wal目录下的内容,发现主库日志切换后,备库pg_wal目录下就会产生新的日志文件,但是在备库的归档目录下没有内容,应该是主库的归档日志传递到备库的pg_wal目录下了。

3.2、主库修改后,日志没有归档,但是备库已经同步了,类似于oracle同步时用lgwr方式进行写standby_logfile进行同步。

3.3、查看当前备库状态:

testdb=#selectpg_is_in_recovery();

pg_is_in_recovery

-------------------

t

t:

true,意味着处于recovery状态

f:

false,意味着处于正常服务状态

主库查询:

testdb=#\x

testdb=#select*frompg_stat_replication;

-[RECORD1]----+------------------------------

pid|3711

usesysid|16384

usename|repl

application_name|walreceiver

client_addr|192.168.18.212

client_hostname|pg2

client_port|49206

backend_start|2020-03-0322:

08:

47.924435-05

backend_xmin|

state|streaming

sent_lsn|0/210000D8

write_lsn|0/210000D8

flush_lsn|0/210000D8

replay_lsn|0/210000D8

write_lag|

flush_lag|

replay_lag|

sync_priority|0

sync_state|async

reply_time|2020-03-0322:

13:

02.990258-05

#application_name很重要,以后同步复制需要用到。

3.4、备库数据库日志内容:

cp:

cannotstat`/home/postgres/arch/00000002.history':

Nosuchfileordirectory

cp:

cannotstat`/home/postgres/arch/000000010000000000000009':

Nosuchfileordirectory

2020-02-2904:

48:

45.734EST[4938]FATAL:

couldnotconnecttotheprimaryserver:

couldnotconnecttoserver:

Connectionrefused

Istheserverrunningonhost"pg1"(192.168.18.211)andaccepting

TCP/IPconnectionsonport1922?

cp:

cannotstat`/home/postgres/arch/00000002.history':

Nosuchfileordirectory

cp:

cannotstat`/home/postgres/arch/000000010000000000000009':

Nosuchfileordirectory

2020-02-2904:

48:

50.747EST[4941]LOG:

startedstreamingWALfromprimaryat0/9000000ontimeline1

如果主库关闭,备库数据库日志内容:

cp:

cannotstat`/home/postgres/arch/00000002.history':

Nosuchfileordirectory

cp:

cannotstat`/home/postgres/arch/00000001000000000000000C':

Nosuchfileordirectory

2020-02-2905:

22:

55.757EST[5048]FATAL:

couldnotconnecttotheprimaryserver:

couldnotconnecttoserver:

Connectionrefused

Istheserverrunningonhost"pg1"(192.168.18.211)andaccepting

TCP/IPconnectionsonport1922?

cp:

cannotstat`/home/postgres/arch/00000002.history':

Nosuchfileordirectory

3.5、主库后台进程:

ps-ef|grep"wal"

postgres37533749021:

21?

00:

00:

00postgres:

walwriter

postgres38443749021:

49?

00:

00:

00postgres:

walsenderrepl192.168.18.212(33595)streaming0/8000148

3.6、备库后台进程,一个进程负责接收,一个负责recovery:

ps-ef|greppostgres

postgres34723471021:

49?

00:

00:

00postgres:

startuprecovering000000010000000000000008

postgres34753471021:

49?

00:

00:

00postgres:

checkpointer

postgres34763471021:

49?

00:

00:

00postgres:

backgroundwriter

postgres34783471021:

49?

00:

00:

00postgres:

statscollector

postgres34793471021:

49?

00:

00:

00postgres:

walreceiverstreaming0/8000148

四、主从切换

4.1、停掉主库

pg_ctlstop-mfast

4.2、执行以下命令进行主从切换,把备库改成主库,执行之后发现standby.signal被删除了:

pg_ctlpromote

查看最新状态:

pg_controldata|grepcluster

Databaseclusterstate:

inproduction

4.3、这一步非常关键,注意原来的备库的postgresql.auto.conf文件中会自动添加一行primary_conninfo的信息,要把这一行给注释掉,否则虽然现在是主库了,但是配置还是当作备库,自相矛盾,且在跟踪日志中会报“backgroundworker"logicalreplicationlauncher"(PID6304)exitedwithexitcode1”错误。

这可能是PG12.2的bug。

postgresql.auto.conf文件内容如下,注意下面内容只是一行数据,/home/postgres/.pgpass其实没有没有这个文件,不需要创建:

primary_conninfo='user=replpassfile=''/home/postgres/.pgpass''host=pg1port=1922sslmode=disablesslcompression=0gssencmode=disablekrbsrvname=postgrestarget_session_attrs=any'

重启数据库,查看后台进程,实验发现walsender进程要等备库正常启动后才会启动,备库关闭时该进程也自动中断:

ps-ef|greppostgres|grep-vsshd|grep-vbash

postgres321531640Feb29pts/300:

00:

00tail-fpg_log

postgres63291007:

08?

00:

00:

00/usr/local/pg12.2/bin/postgres

postgres63316329007:

08?

00:

00:

00postgres:

checkpointer

postgres63326329007:

08?

00:

00:

00postgres:

backgroundwriter

postgres63336329007:

08?

00:

00:

00postgres:

walwriter

postgres63346329007:

08?

00:

00:

00postgres:

autovacuumlauncher

postgres63356329007:

08?

00:

00:

00postgres:

archiver

postgres63366329007:

08?

00:

00:

00postgres:

statscollector

postgres63376329007:

08?

00:

00:

00postgres:

logicalreplicationlauncher

postgres63536329007:

12?

00:

00:

00postgres:

walsenderrepl192.168.18.212(33609)streaming0/1A01C7F8

4.4、在新备库上创建一个standby.signal文件,添加如下内容:

primary_conninfo='host=pg1port=1922user=replpassword=oracleoptions=''-cwal_sender_timeout=5000'''

restore_command='cp/home/postgres/arch/%f%p'

archive_cleanup_command='pg_archivecleanup/home/postgres/arch%r'

standby_mode=on

4.5、在新备库的postgresql.auto.conf文件中添加如下内容,这一步非常关键,第一次搭建备库的时候会自动添加,但是切换后却不能:

primary_conninfo='user=replpassfile=''/home/postgres/.pgpass''host=pg2port=1922sslmode=disablesslcompression=0gssencmode=disablekrbsrvname=postgrestarget_session_attrs=any'

注意/home/postgres/.pgpass其实没有没有这个文件,不需要创建。

4.6、启动新备库:

pg_ctlstart-lpg_log

4.7、查看后台进程:

ps-ef|greppostgre|grep-vssh|grep-vbash

postgres327432370Feb29pts/300:

00:

00tail-fpg_log

postgres64411007:

12?

00:

00:

00/usr/local/pg12.2/bin/postgres

postgres64426441007:

12?

00:

00:

00postgres:

startuprecovering00000003000000000000001A

postgres64476441007:

12?

00:

00:

00postgres:

checkpointer

postgres64486441007:

12?

00:

00:

00postgres:

backgroundwriter

postgres64506441007:

12?

00:

00:

00postgres:

statscollector

postgres64516441007:

12?

00:

00:

05postgres:

walreceiverstreaming0/1A01C7F8

4.8、验证主备库是否能够同步

在主库进行dml操作,发现备库能够正常同步,切换成功。

4.9、主库变成备库时,有时候启动会失败,日志显示找不到xxx.history日志文件,解决办法,在postgresql.conf中指定明确的timeline,把原来的latest替换掉:

recovery_target_timeline='3'

总结:

经过实验,发现主备切换不太灵活和智能,需要后续进行手动修改,特别是postgresql.auto.conf文件中自动添加的一行,在主备切换的时候不会自动删除,没有相关文档,造成了隐性的问题,给DBA造成了很大的麻烦,不容易故障排除。

主库在正常运行中,备库可以随意切换为主库,没有一个制约机制,感觉不严谨,此时变成两个主库,数据无法同步。

如果此时两边的数据库都各自发生变化,将来想把一台主库当作备库,则需要在备库上对当前的数据进行同步,然后就可以变成备库,用以下的命令进行同步:

pg_rewind--target-pgdata$PGDATA--source-server='host=192.168.18.211port=1922user=postgresdbname=testdb'

pg_rewind:

serversdivergedatWALlocation0/1C01F280ontimeline5

pg_rewind:

rewindingfromlastcommoncheckpointat0/1C01F1D0ontimeline5

pg_rewind:

Done!

五、pg_rewind工具

如果备库是意外崩溃,如果新的主库修改了数据,经过的时间很长,归档日志又删除了,无法同步,原来的数据库如果想变成备库,需要对数据库做一次同步,那么就可以用到pg_rewind工具进行同步。

pg_rewind—使一个PostgreSQL数据目录与另一个数据目录(该目录从第一个PostgreSQL数据目录创建而来)一致。

描述

pg_rewind是一个在集群的时间线参数偏离之后,用于使一个PostgreSQL集群与另一个相同集群的拷贝同步的工具。

一个典型的场景是在故障转移之后,让一个老的主服务器重新在线作为一个standby跟随新主服务器。

其结果相当于使用源数据目录替换目标数据目录。

所有的文件都被拷贝,包括配置文件。

与做一个基础备份或者像rsync这样的工具相比,pg_rewind的优势是pg_rewind不需要读取所有集群中没有更改的文件。

当数据库很大,并且只有一小部分不同的集群之间,使它的速度快得多。

pg_rewind检查源集群与目标集群的时间线历史来检测它们产生分歧的点,并希望在目标集群的pg_xlog目录找到WAL回到分歧点的所有方式。

在典型的故

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 求职职场 > 简历

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2