PostgreSQL13.0流复制尝鲜
postgresql13.0于2020年9月21日正式发布,话说现在pg的大版本从10开始发生了变化,以第一个数字代表一个大版本更新,而9之前的版本则是以9.1->9.2->9.x这样代表大版本更新。所以现在看起来pg的更新好像越来越快了,每个版本其实更新的内容不是很多。13发布后下载来了源码尝尝鲜,源码编译上没有什么改变,依旧很简单很亲和,四条简单的命令完成编译安装,对平台兼容性也很好。
其实pg从12版本开始就已经对流复制这块做了改动,最大的变化是摒弃了recovery.conf文件,将主备连接信息放入postgresql.conf主配置文件中。如果依旧使用recovery.conf方式配置主从会在启动数据库时报recovery.conf方式依旧不再支持。下面以pg13版本为例看看部署过程。
主备库源码编译安装数据库,此步骤略。
1.主库创建角色用于复制:
postgres=# create role replica login replication encrypted password 'replica';
CREATE ROLE
2.主库增加pg_hba.conf
host all all 192.168.1.2/32 trust
host replication replica 192.168.1.2/32 md5
3.从库执行基础备份
[postgres@db02 ~]$ pg_basebackup -h 192.168.1.1 -U replica -D /pgdata/pginst1 -X stream -P -R
Password:
24283/24283 kB (100%), 1/1 tablespace
-R选项会使用恢复模式创建standby.signal文件,同时将primary_conninfo写入postgresql.auto.conf文件,如下
primary_conninfo = 'user=replica password=replica channel_binding=disable host=192.168.1.1 port=5432 sslmode=disable sslcompression=0 ssl_min_protocol_version=TLSv1.2 gssencmode=disable krbsrvname=postgres target_session_attrs=any'
4.启动备库
[postgres@db02 ~]$ pg_ctl start -l logfile
waiting for server to start.... done
server started
5.查看复制关系
主库:
test=# \x
Expanded display is on.
test=# select * from pg_stat_replication;
-[ RECORD 1 ]----+------------------------------
pid | 338819
usesysid | 16384
usename | replica
application_name | walreceiver
client_addr | 192.168.1.2
client_hostname |
client_port | 44376
backend_start | 2020-10-11 15:18:59.227516+08
backend_xmin |
state | streaming
sent_lsn | 0/70128F8
write_lsn | 0/70128F8
flush_lsn | 0/70128F8
replay_lsn | 0/70128F8
write_lag |
flush_lag |
replay_lag |
sync_priority | 0
sync_state | async
reply_time | 2020-10-11 15:21:22.18738+08
从库:
test=# \x
Expanded display is on.
test=# select * from pg_stat_wal_receiver;
-[ RECORD 1 ]---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
pid | 337933
status | streaming
receive_start_lsn | 0/7000000
receive_start_tli | 1
written_lsn | 0/70128F8
flushed_lsn | 0/70128F8
received_tli | 1
last_msg_send_time | 2020-10-11 15:21:42.680431+08
last_msg_receipt_time | 2020-10-11 15:21:42.250019+08
latest_end_lsn | 0/70128F8
latest_end_time | 2020-10-11 15:20:42.502513+08
slot_name |
sender_host | 192.168.1.1
sender_port | 5432
conninfo | user=replica password=******** channel_binding=disable dbname=replication host=192.168.1.1 port=5432 fallback_application_name=walreceiver sslmode=disable sslcompression=0 ssl_min_protocol_version=TLSv1.2 gssencmode=disable krbsrvname=postgres target_session_attrs=any
评论
图片
表情
视频
全部评论