以1主1从为例
准备两个数据库(一台虚拟机上运行一个实例)
以 136(主机)140(从机)为例
以/opt/postgres/data 为持久化路径为例
①主机配置:
- 白名单配置(vim /opt/postgres/data/pg_hba.conf)
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
#host all all ::1/128 trust
host all all 0.0.0.0/0 md5
host replication repl 192.168.42.132/32 trust
- 修改核心配置文件(vim /opt/postgres/data/postgresql.conf )
- 重启(启动)数据库
listen_addresses = '*' #监听所有ip
wal_level = hot_standby
max_wal_senders= 6
wal_keep_segments = 10240
max_connections = 512
登录 su postgres
psql
create role repl login replication encrypted password '123';
②从机(140)配置
注意切换postgres用户进行,
a)删除原数据库data文件
rm -rf /opt/postgres/data/*
b)备份文件
pg_basebackup -h 192.168.42.131 -U repl -D /opt/postgres/data -X stream -P
c)编辑recovery.conf编辑主从配置
cp /opt/postgres/share/recovery.conf.sample /opt/postgres/data/recovery.conf
vim /opt/postgres/data/recovery.conf
standby_mode = on #该节点为从机
primary_conninfo = 'host=192.168.42.131 port=5432 user=repl password=123'
recovery_target_timeline = 'latest'
trigger_file = '/tmp/trigger_file0'
d)开启standby模式(在从机上配置)
hot_standby = on
vim /opt/postgres/data/postgresql.conf
重启数据库
systemctl stop postgresql
systemctl start postgresql
③查看从节点信息
主节点psql控制台
select client_addr,sync_state from pg_stat_replication;