码迷,mamicode.com
首页 > 数据库 > 详细

PostgreSQL12同步流复制搭建-同步不生效的问题

时间:2020-01-22 14:32:33      阅读:407      评论:0      收藏:0      [点我收藏+]

标签:名称   keep   启动数据库   sid   oss   搭建   trigger   replicat   eve   

PostgreSQL12的流复制配置不再放到recovery.conf文件中,但是基本配置还是一样的,过年了也没心情工作,就来搭一下试试。

 

官方文档:

https://www.postgresql.org/docs/12/runtime-config-replication.html

 

开始:

1)下载安装包:

https://www.postgresql.org/docs/12/runtime-config-replication.html

2)解压并安装

tar xzvf postgresql-12.1.tar.gz
cd postgresql-12.1/
./configure --prefix=/opt/pg12 --without-zlib
su root -c chown -R postgres:postgres /opt/pg12
make && make install

3)创建目录和环境变量,准备就在本机上创建两个data目录进行试验:data1为主 data2为备

cd /opt/pg12
mkdir data1
mkdir data2

vim ~/pg12.env 
source ~/pg12.env 
[postgres@localhost data1]$ cat ~/pg12.env 
export PGHOME=/opt/pg12/
export PATH=$PGHOME/bin:$PATH
export PGDATA=$PGHOME/data1
export PGPORT=54121

4)初始化数据库

cd data1

vim postgresql.conf 
修改:
port = 54121 
wal_level = replica
synchronous_commit = on
max_wal_senders = 10
wal_keep_segments = 1024
synchronous_standby_names = ‘standby_node‘
hot_standby = on
hot_standby_feedback = on
logging_collector = on

启动数据库:
pg_ctl start

cd ../data2
pg_basebackup -R -X stream -Fp -D ./ -h localhost -p 54121
vim postgresql.conf
修改:
recovery_target_timeline = ‘latest‘
primary_conninfo = ‘application_name=standby_node host=localhost port=54121 user=postgres password=postgres‘                                   
promote_trigger_file = ‘/opt/pg12/data2/promote_trigger_file‘
port=54122

启动备数据库:
pg_ctl -D ./ start

 

5)查看流复制情况,发现是异步流复制,application_name没有生效:

postgres=# select * from pg_stat_replication;
-[ RECORD 1 ]----+------------------------------
pid              | 19728
usesysid         | 10
usename          | postgres
application_name | walreceiver
client_addr      | ::1
client_hostname  | 
client_port      | 37651
backend_start    | 2020-01-21 19:55:14.881115-08
backend_xmin     | 488
state            | streaming
sent_lsn         | 0/30175C0
write_lsn        | 0/30175C0
flush_lsn        | 0/30175C0
replay_lsn       | 0/30175C0
write_lag        | 
flush_lag        | 
replay_lag       | 
sync_priority    | 0
sync_state       | async
reply_time       | 2020-01-21 20:01:38.445309-08

 

进一步排查,发现是在postgresql.auto.conf中有自动生成的primary_conninfo配置,里面没有application_name配置,而postgresql.auto.conf文件的优先级高于postgresql.conf文件。在里面添加节点名称:

[postgres@localhost data2]$ cat postgresql.auto.conf
# Do not edit this file manually!
# It will be overwritten by the ALTER SYSTEM command.
primary_conninfo = ‘application_name=standby_node user=postgres passfile=‘‘/home/postgres/.pgpass‘‘ host=localhost port=54121 sslmode=disable sslcompression=0 gssencmode=disable target_session_attrs=any‘

6)在从节点上reload是不会生效的,必须重启从节点:

pg_ctl -D ./ restart

 

7)在主节点查看流复制,同步生效:

postgres=# select * from pg_stat_replication;
-[ RECORD 1 ]----+------------------------------
pid              | 20151
usesysid         | 10
usename          | postgres
application_name | standby_node
client_addr      | ::1
client_hostname  | 
client_port      | 37657
backend_start    | 2020-01-21 20:03:52.765047-08
backend_xmin     | 488
state            | streaming
sent_lsn         | 0/3017670
write_lsn        | 0/3017670
flush_lsn        | 0/3017670
replay_lsn       | 0/3017670
write_lag        | 
flush_lag        | 
replay_lag       | 
sync_priority    | 1
sync_state       | sync
reply_time       | 2020-01-21 20:04:02.884156-08

  

8)将主节点的synchronous_standby_names配置为any的方式,reload即可生效:

vim postgresql.conf
synchronous_standby_names = ‘any 1 (standby_node,node1)‘

pg_ctl reload

postgres=# select * from pg_stat_replication; -[ RECORD 1 ]----+------------------------------ pid | 20663 usesysid | 10 usename | postgres application_name | standby_node client_addr | ::1 client_hostname | client_port | 37659 backend_start | 2020-01-21 20:24:48.714207-08 backend_xmin | 490 state | streaming sent_lsn | 0/3022AD0 write_lsn | 0/3022AD0 flush_lsn | 0/3022AD0 replay_lsn | 0/3022AD0 write_lag | flush_lag | replay_lag | sync_priority | 1 sync_state | quorum reply_time | 2020-01-21 22:20:01.980318-08

  

说明:

sync_priority
Priority of this standby server for being chosen as the synchronous standby in a priority-based synchronous replication. This has no effect in a quorum-based synchronous replication. sync_state
Synchronous state of this standby server. Possible values are:      async: This standby server is asynchronous.      potential: This standby server is now asynchronous, but can potentially become synchronous if one of current synchronous ones fails.     sync: This standby server is synchronous.     quorum: This standby server is considered as a candidate for quorum standbys.

  

 

PostgreSQL12同步流复制搭建-同步不生效的问题

标签:名称   keep   启动数据库   sid   oss   搭建   trigger   replicat   eve   

原文地址:https://www.cnblogs.com/kuang17/p/12228349.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!