本篇内容介绍了“PostgreSQL HA环境分析”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

基于streaming replication搭建的PostgreSQL HA环境,Old Standby节点升级为New Master节点后,时间线会切换为新的时间线,比如从n变为n + 1.而Old Master节点的时间线仍然为原来的时间线,比如仍为n,通过使用pg_rewind工具,可使原来其实”完好”的Old Master成为New Standby节点.
图解
如下图所示:
在执行主备切换后,New Master节点的时间线切换为n + 1,通过pg_rewind可使Old Master在分叉点开始与New Master同步,成为New Standby节点.

实测
New Master
切换后为New Master后,执行以下SQL:

testdb=#createtablet_new(idint,flagvarchar(40));CREATETABLEtestdb=#insertintot_newselectc,'flag'||cfromgenerate_series(1,1000000)asc;INSERT01000000testdb=#

Old Master
执行pg_rewind前,重启主库,执行以下SQL

testdb=#createtablet_fork(idint,flagvarchar(40));CREATETABLEtestdb=#insertintot_forkselectc,'flag'||cfromgenerate_series(1,1000000)asc;INSERT01000000testdb=#

执行pg_rewind

[xdb@localhosttestdb]$cp/data/archivelog/*./pg_wal[xdb@localhosttestdb]$pg_rewind--target-pgdata=$PGDATA--source-server="host=192.168.26.25port=5432dbname=testdb"--progressconnectedtoserverserversdivergedatWALlocation0/B41F12B8ontimeline23rewindingfromlastcommoncheckpointat0/AFCF99E0ontimeline23readingsourcefilelistreadingtargetfilelistreadingWALintargetneedtocopy360MB(totalsourcedirectorysizeis501MB)369312/369312kB(100%)copiedcreatingbackuplabelandupdatingcontrolfilesyncingtargetdatadirectoryDone!

配置recovery.conf文件

[xdb@localhosttestdb]$mvrecovery.donerecovery.conf[xdb@localhosttestdb]$vimrecovery.conf[xdb@localhosttestdb]$catrecovery.confstandby_mode='on'primary_conninfo='user=replicatorpassword=replicatorhost=192.168.26.26port=5432sslmode=prefersslcompression=0krbsrvname=postgrestarget_session_attrs=any'restore_command='cp/data/archivelog/%f%p'

重启数据库

[xdb@localhosttestdb]$pg_ctlstartwaitingforservertostart....2019-03-2812:39:31.918CST[1961]LOG:listeningonIPv4address"0.0.0.0",port54322019-03-2812:39:31.918CST[1961]LOG:listeningonIPv6address"::",port54322019-03-2812:39:31.920CST[1961]LOG:listeningonUnixsocket"/tmp/.s.PGSQL.5432"2019-03-2812:39:31.970CST[1961]LOG:redirectinglogoutputtologgingcollectorprocess2019-03-2812:39:31.970CST[1961]HINT:Futurelogoutputwillappearindirectory"pg_log"....doneserverstarted

与New Master数据同步了,而在原TL上的t_fork数据表消失了.

testdb=#selectcount(*)fromt_new;count---------1000000(1row)testdb=#selectcount(*)fromt_old;count---------1000000(1row)testdb=#selectcount(*)fromt_fork;ERROR:relation"t_fork"doesnotexistLINE1:selectcount(*)fromt_fork;^testdb=#

“PostgreSQL HA环境分析”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!