一、安装: PostgreSQL12
二 、配置主节点
创建用户:
-
su postgres
createuser --replication -P -e replicator # -P:设置密码,-e:回显
修改配置
- vim /etc/postgresql/12/main/pg_hba.conf
添加:如下配置
- #### repl 47.91.228.160 为 从服务器IP
- # TYPE DATABASE USER ADDRESS METHOD
- host replication replicator 47.91.228.160/32 md5
- ##### repl
- # 需要远程访问,记得加(还有一个监听,在后面配置)
- # 0.0.0.0/0为任意IP
- host all all 47.91.228.160/32 md5
編輯 postgresql.conf
vim /etc/postgresql/12/main/postgresql.conf
- listen_addresses = '*'
- archive_mode = on # 开启归档
- archive_command = 'test ! -f /var/lib/postgresql/12/main/pg_archive/%f && cp %p /var/lib/postgresql/12/main/pg_archive/%f'
- wal_keep_segments = 15 # in logfile segments; 0 disables
- wal_sender_timeout = 60s # in milliseconds; 0 disables
- max_connections = 1000 # 最大连接数,据说从机需要大于或等于该值
# 保存退出
# 建立归档文件夹
- mkdir /var/lib/postgresql/12/main/pg_archive
重启PostgreSQL
- service postgresql restart
三 、从服务器配置
首先 测试远程连接 【此处开始建议进入 postgres 用户进行接下来的操作】
- psql -h 47.100.55.100 -U replicator -d postgres
[postgres@world-alive:~]$ psql -h 47.100.55.100 -U replicator -d postgres
Password for user replicator:
psql (12.4 (Debian 12.4-1.pgdg90+1))
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
Type "help" for help.
停止从节点的 PostgreSQL (主用户root 操作)
- service postgresql stop
清空数据文件夹
建议重命名稍后删除
- cd /var/lib/postgresql/12
- mv main old-main
- mkdir /var/lib/postgresql/12/main/
为新建的文件夹设置权限 否则启动报错
- data directory "/var/lib/postgresql/12/main" has invalid permissions
-
chmod 750 /var/lib/postgresql/12/main/
从主节点拷贝数据 * 需要使用postgres用户进行操作否则启动从节点将会有问题
Password:
pg_basebackup: initiating base backup, waiting for checkpoint to complete
pg_basebackup: checkpoint completed
pg_basebackup: write-ahead log start point: 0/2000028 on timeline 1
pg_basebackup: starting background WAL receiver
pg_basebackup: created temporary replication slot "pg_basebackup_19352"
32418/32418 kB (100%), 1/1 tablespace
pg_basebackup: write-ahead log end point: 0/2000138
pg_basebackup: waiting for background process to finish streaming ...
pg_basebackup: syncing data to disk ...
pg_basebackup: base backup completed
如果用户不对执行如下代码(否则跳过):
- chown -R postgres:postgres /var/lib/postgresql/12/main
编辑 standby.signal 文件 添加如下
- standby_mode = 'on'
编辑 postgresql.conf 新增或修改下列属性设置 (注意查看下 port 测试时发现 port不是默认值 5432 , 可能是我自己改的,不确定设置时注意看下)
- primary_conninfo = 'host=主节点IP port=5432 user=replica password=replica用户的密码'
- recovery_target_timeline = latest
- max_connections = 1500 #大于主节点
- hot_standby = on
- max_standby_streaming_delay = 30s
- wal_receiver_status_interval = 10s
- hot_standby_feedback = on
四、测试主从服务
验证主从是否成功
登录主节点数据库执行如下命令
- su postgres
- psql
- select client_addr,sync_state from pg_stat_replication;
1. 查看从服务(WAL接收器进程)状态:
扩展显示已打开.
-[ RECORD 1 ]---------+----------------------------
pid | 3240
status | streaming
receive_start_lsn | 0/3000000
receive_start_tli | 1
received_lsn | 0/3013BF8
received_tli | 1
last_msg_send_time | 2020-06-22 18:12:29.985512+08
last_msg_receipt_time | 2020-06-22 18:12:29.761575+08
latest_end_lsn | 0/3013BF8
latest_end_time | 2020-06-22 17:02:51.977569+08
slot_name | pgstandby1
sender_host | 10.20.20.1
sender_port | 5432
conninfo | user=replicator password=******** dbname=replication host=10.20.20.1 port=5432 fallback_application_name=walreceiver sslmode=prefer sslcompression=0 gssencmode=prefer krbsrvname=postgres target_session_attrs=any
查看进程信息,会有walsender的相关信息。
- $ ps aux | grep postgres
- postgres ... postgres: walsender replicator 10.20.20.2(38418) streaming 0/3013BF8
- ...
2. 查看主服务(WAL发送器进程)状态:
-[ RECORD 1 ]----+------------------------------
pid | 4647
usesysid | 16384
usename | replicator
application_name | walreceiver
client_addr | 10.20.20.2
client_hostname |
client_port | 38418
backend_start | 2020-06-22 16:54:54.391772+08
backend_xmin |
state | streaming # ☆☆☆服务状态☆☆☆
sent_lsn | 0/3013BF8
write_lsn | 0/3013BF8
flush_lsn | 0/3013BF8
replay_lsn | 0/3013BF8
write_lag |
flush_lag |
replay_lag |
sync_priority | 0
sync_state | async # ☆☆☆同步状态☆☆☆
reply_time | 2020-06-22 18:18:20.293569+08
查看进程信息,会有walreceiver的相关信息。
- $ ps aux | grep postgres
- postgres ...postgres: walreceiver streaming 0/3013BF8
- ...
3. 数据测试
在master创建replica_demo数据库,在standby上可以看到同步效果
- [master] #CREATE DATABASE replica_demo;
- [standby]#\l
配置优化
参考:https://www.cnblogs.com/VicLiu/p/11854730.html
没有评论