PostgreSQL流复制配置
主库配置
编辑$PGDATA/postgresql.conf
文件
log_timezone = 'PRC'
datestyle = 'iso, mdy'
timezone = 'PRC'
default_text_search_config = 'pg_catalog.english'
listen_addresses = '*'
port = 5432
wal_level = hot_standby
hot_standby = on
archive_mode = on
archive_command = 'cp "%p" "/data/pgsql/archivedir/%f"'
max_wal_senders = 5
wal_keep_segments = 1280
wal_sender_timeout = 60s
max_connections = 2500
max_prepared_transactions = 2500
wal_log_hints = on
max_parallel_maintenance_workers = 16
maintenance_work_mem = 3GB
max_worker_processes = 64
max_parallel_workers_per_gather = 24
max_parallel_workers = 36
effective_cache_size = 64GB
huge_pages = try
shared_buffers = 64GB
#idle_in_transaction_session_timeout = 180000
wal_buffers = 16MB
shared_preload_libraries = 'pg_stat_statements'
pg_stat_statements.max = 10000
pg_stat_statements.track = all
checkpoint_timeout = 30min
max_sync_workers_per_subscription = 32
max_wal_size = 64GB
min_wal_size = 4GB
wal_compression = on
tcp_keepalives_idle = 600
tcp_keepalives_interval = 10
tcp_keepalives_count = 6
autovacuum = on
autovacuum_max_workers = 8
autovacuum_work_mem = 4GB
log_autovacuum_min_duration = 0
autovacuum_vacuum_scale_factor = 0.02
autovacuum_analyze_scale_factor = 0.01
autovacuum_freeze_max_age = 1200000000
autovacuum_multixact_freeze_max_age = 1250000000
autovacuum_vacuum_cost_delay = 0ms
vacuum_freeze_table_age = 200000000
vacuum_multixact_freeze_table_age = 200000000
min_parallel_table_scan_size =0
min_parallel_index_scan_size =0
parallel_tuple_cost =0
parallel_setup_cost =0
配置pg_hba.conf文件
因为安全原因,使用replica
用户用作复制
假设所有的pgpool2服务器和PostgreSql服务器都在200.100.128.0/23子网,编辑pg_hba.conf
文件支持此用户的md5验证。
PostgreSql数据库中创建用户
# 创建流复制用户
create role replica login replication encrypted password '<replicaPass>';
修改pg_hba.conf
文件
#TYPE DATABASE USER ADDRESS METHOD
local all postgres peer
local all all peer
host all all 127.0.0.1/32 md5
host all all 15.16.0.0/24 md5
host replication all 127.0.0.1/32 md5
host replication replica 15.16.22.14/24 md5
host replication replica 15.16.22.15/24 md5
使用pgpool2的故障转移和在线恢复功能,需要所有pgpool2和PostgreSql服务器的postgres
系统用户允许ssh免密登录。
# 生成postgres用户的ssh免密互登录
su postgres
ssh-keygen
配置免密略
# 流复制用户replica免密
su - postgres
cat > .pgpass <<EOF
hhhtlt-pg1:5432:replication:replica:APzqI4B6d5h2k
hhhtlt-pg2:5432:replication:replica:APzqI4B6d5h2k
EOF
配置 recovery.done文件
standby_mode = 'on'
primary_conninfo = 'host=hhhtlt-pg2 port=5432 user=replica'
restore_command = 'scp -P 22 hhhtlt-pg2:/data/pgsql/archivedir/%f %p'
重启
su - postgres
/usr/local/postgresql/11/bin/pg_ctl -D /data/pgsql/11/data -l /var/log/postgres/postgresql-11-main.log stop
/usr/local/postgresql/11/bin/pg_ctl -D /data/pgsql/11/data -l /var/log/postgres/postgresql-11-main.log start
从库配置
配置完主节点后使用pg_basebackup
将主库的配置同步到从库
注: 执行前要先把data目录清空
pg_basebackup -X stream -c fast -P -D /data/pgsql/11/data -h hhhtlt-pg1 -p 5432 -U replica
修改recovery.done文件
# mv recovery.done recovery.conf
standby_mode = 'on'
primary_conninfo = 'host=sxsz-spzw-pg1 port=5432 user=replica'
restore_command = 'scp -P 22 sxsz-spzw-pg1:/data/pgsql/archivedir/%f %p'
启动
su - postgres
/usr/local/postgresql/11/bin/pg_ctl -D /data/pgsql/11/data -l /var/log/postgres/postgresql-11-main.log start
验证
主库执行
postgres=# select * from pg_stat_replication;
-[ RECORD 1 ]----+------------------------------
pid | 3174
usesysid | 16384
usename | replica
application_name | walreceiver
client_addr | 15.16.22.15
client_hostname |
client_port | 34444
backend_start | 2021-09-09 21:59:17.238057+08
backend_xmin |
state | streaming
sent_lsn | 0/40000D0
write_lsn | 0/40000D0
flush_lsn | 0/40000D0
replay_lsn | 0/40000D0
write_lag |
flush_lag |
replay_lag |
sync_priority | 0
sync_state | async