PG主备流复制的核心部分由walsender,walreceiver和startup三个进程组成。标签:主从复制,12,postgresql,standby,pgsql,wal,data From: https://blog.51cto.com/feirenraoyuan/5721083
yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
yum install postgresql12
yum install postgresql12-server
/usr/pgsql-12/bin/postgresql-12-setup initdb
systemctl enable postgresql-12
systemctl start postgresql-12
su - postgres
psql -U postgres
ALTER USER postgres WITH PASSWORD 'postgres';s
vim /var/lib/pgsql/12/data/postgresql.conf
listen_addresses='*'
vim /var/lib/pgsql/12/data/pg_hba.conf
host all all 0.0.0.0/0 md5
一、主库配置
CREATE ROLE zhucong login replication encrypted password 'zhucong'; #创建具有流复制权限的用户
vi /var/lib/pgsql/12/data/postgresql.conf
archive_mode = on # 开启归档
archive_command = 'cp %p /var/lib/pgsql/12/data/pg_archive/%f' # 归档命令
wal_level = replica # wal_level 有3种模式,minimal<replica<logical
#最多有2个流复制连接
max_wal_senders = 2
wal_keep_segments = 16 # WAL日志文件数量,一个文件16M
wal_keep_size = 64
#流复制超时时间
wal_sender_timeout = 60s
#最大连接数,从机需要大于或等于该值
max_connections = 100
重启:systemctl restart postgresql-12.service
二、从库配置
systemctl stop postgresql-12.service
rm -rf /var/lib/pgsql/12/data/* #清空从库数据存储文件夹
su - postgres -c "pg_basebackup -h 192.168.5.131 -p 5432 -U replica -Fp -Xs -Pv -R -D /var/lib/pgsql/12/data"
此时data目录下会出现该文件: vi standby.signal 添加如下:
standby_mode = 'on'
说明:该文件standby.signal是PG12中的新增功能,用以替代旧版本standby_mode = 'on',以前在文件recovery.conf中定义。
# 修改vi postgresql.conf
#主库信息和连接用户
primary_conninfo = 'host=192.168.100.222 port=5432 user=replica password=postgres'
#说明恢复到最新状态
recovery_target_timeline = latest
#大于主节点,正式环境应当重新考虑此值的大小
max_connections = 120
#说明这台机器不仅用于数据归档,还可以用于数据查询
hot_standby = on
standby_mode = on
#流备份的最大延迟时间
max_standby_streaming_delay = 30s
#向主机汇报本机状态的间隔时间
wal_receiver_status_interval = 10s
#出现错误复制,向主机反馈
hot_standby_feedback = on
启动:systemctl restart postgresql-12.service
三、验证主从搭建是否成功
主:psql -h 172.16.123.100 -U postgres
postgres=# select client_addr,sync_state from pg_stat_replication;
主: select * from pg_stat_replication;
从: select * from pg_stat_wal_receiver;
参考:https://blog.51cto.com/suncj/5102637