PostgreSQL 12 流复制环境搭建
Posted actively
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PostgreSQL 12 流复制环境搭建相关的知识,希望对你有一定的参考价值。
1、异步流复制简单配置示例
服务器规划
192.168.0.130(主库)
192.168.0.131(备库)
192.168.0.132(备库)
主节点配置
1、初始化数据集簇
./initdb ../data-primary -U postgres
2、启动服务,创建用户
ALTER USER postgres WITH PASSWORD ‘123456‘;
CREATE ROLE repl WITH PASSWORD ‘123456‘ REPLICATION LOGIN;
3、配置 postgresql.conf
listen_addresses=‘*‘
archive_mode=on
archive_command=‘cp "%p" "/data/pg12/archivedir"‘
max_wal_senders=10
max_replication_slots=10
wal_level=replica
wal_keep_segments=512
hot_standby=on
synchronous_commit=on
synchronous_standby_names=‘any 1(*)‘
logging_collector = on
log_destination = ‘csvlog‘
log_truncate_on_rotation = on
log_filename = ‘postgresql-%w.log‘
log_connections = off
log_disconnections = off
log_error_verbosity = verbose
log_statement = ddl
log_min_duration_statement = 3s
log_checkpoints = on
4、配置 pg_hba.conf
host replication all 0.0.0.0/0 trust
5、重启服务
node1备节点配置
1、删除备机data目录
rm -rf /data/pg12/pgdata
2、基础备份
pg_basebackup -h 192.168.0.130 -p 5432 -U repl -D /data/pg12/pgdata/ -v -P -R -X stream -C -S node1
在pgdata路径下会生成:standby.signal 文件
在pgdata路径下: postgresql.auto.conf 文件会添加连接信息
[sun@localhost data-standby01]$ cat postgresql.auto.conf
# Do not edit this file manually!
# It will be overwritten by the ALTER SYSTEM command.
primary_conninfo = ‘user=repl password=123456 host=192.168.0.130 port=5432 sslmode=disable sslcompression=0 gssencmode=disable target_session_attrs=any‘
2、启动备库(同一服务器实验,需要先修改 postgresql.conf 的端口)
node2备节点配置
1、基础备份
pg_basebackup -h 192.168.0.130 -p 5432 -U repl -D /data/pg12/pgdata/ -v -P -R -X stream -C -S node2
在pgdata路径下会生成:standby.signal 文件
在pgdata路径下: postgresql.auto.conf 文件会添加连接信息
[sun@localhost data-standby02]$ cat postgresql.auto.conf
# Do not edit this file manually!
# It will be overwritten by the ALTER SYSTEM command.
primary_conninfo = ‘user=repl password=123456 host=192.168.0.130 port=5432 sslmode=disable sslcompression=0 gssencmode=disable target_session_attrs=any‘
2、启动备库(同一服务器实验,需要先修改 postgresql.conf 的端口)
查看
postgres=# x
Expanded display is on.
postgres=# select * from pg_stat_replication ;
-[ RECORD 1 ]----+------------------------------
pid | 24416
usesysid | 16384
usename | repl
application_name | walreceiver
client_addr | 192.168.0.131
client_hostname |
client_port | 55580
backend_start | 2020-06-20 18:12:28.701572+08
backend_xmin |
state | streaming
sent_lsn | 0/5000060
write_lsn | 0/5000060
flush_lsn | 0/5000060
replay_lsn | 0/5000060
write_lag |
flush_lag |
replay_lag |
sync_priority | 0
sync_state | quorum
reply_time | 2020-06-20 18:14:50.806858+08
-[ RECORD 2 ]----+------------------------------
pid | 24433
usesysid | 16384
usename | repl
application_name | walreceiver
client_addr | 192.168.0.132
client_hostname |
client_port | 55582
backend_start | 2019-10-28 18:12:33.452452+08
backend_xmin |
state | streaming
sent_lsn | 0/5000060
write_lsn | 0/5000060
flush_lsn | 0/5000060
replay_lsn | 0/5000060
write_lag |
flush_lag |
replay_lag |
sync_priority | 0
sync_state | quorum
reply_time | 2020-06-20 18:14:53.639337+08
3、同步流复制示例
在异步流复制的基础上:
主库 postgresql.conf 添加配置
synchronous_commit = on
synchronous_standby_names = ‘node1,node2‘
备库 postgresql.auto.conf 修改配置
# Do not edit this file manually!
# It will be overwritten by the ALTER SYSTEM command.
primary_conninfo = ‘application_name=standby01 user=repl password=123456 host=192.168.0.130 port=5432 sslmode=disable sslcompression=0 gssencmode=disable target_session_attrs=any‘
# Do not edit this file manually!
# It will be overwritten by the ALTER SYSTEM command.
primary_conninfo = ‘application_name=standby02 user=repl password=123456 host=192.168.0.130 port=5432 sslmode=disable sslcompression=0 gssencmode=disable target_session_attrs=any‘
重启主、备服务,查看状态
postgres=# x
Expanded display is on.
postgres=# select * from pg_stat_replication ;
-[ RECORD 1 ]----+------------------------------
pid | 24880
usesysid | 16384
usename | repl
application_name |node2
client_addr | 192.168.0.132
client_hostname |
client_port | 55598
backend_start | 2020-06-20 18:25:09.462092+08
backend_xmin |
state | streaming
sent_lsn | 0/60000D8
write_lsn | 0/60000D8
flush_lsn | 0/60000D8
replay_lsn | 0/60000D8
write_lag |
flush_lag |
replay_lag |
sync_priority | 2
sync_state | potential
reply_time | 2020-06-20 18:25:29.613489+08
-[ RECORD 2 ]----+------------------------------
pid | 24858
usesysid | 16384
usename | repl
application_name | node1
client_addr | 192.168.0.131
client_hostname |
client_port | 55596
backend_start | 2020-06-20 18:25:06.144197+08
backend_xmin |
state | streaming
sent_lsn | 0/60000D8
write_lsn | 0/60000D8
flush_lsn | 0/60000D8
replay_lsn | 0/60000D8
write_lag |
flush_lag |
replay_lag |
sync_priority | 1
sync_state | sync
reply_time | 2020-06-20 18:25:26.282327+08
以上是关于PostgreSQL 12 流复制环境搭建的主要内容,如果未能解决你的问题,请参考以下文章