Postgresql12 实现主从复制

2020年9月8日

一、安装: PostgreSQL12  

Debian Install PostgreSQL 10.2 安装最新版的

二 、配置主节点

创建用户

  1. su postgres
    createuser --replication -P -e replicator # -P:设置密码,-e:回显

修改配置

  1. vim /etc/postgresql/12/main/pg_hba.conf

添加:如下配置

  1. ####   repl    47.91.228.160 为 从服务器IP
  2. # TYPE  DATABASE        USER            ADDRESS                 METHOD
  3. host    replication     replicator      47.91.228.160/32        md5
  4. ##### repl
  5. # 需要远程访问,记得加(还有一个监听,在后面配置)
  6. # 0.0.0.0/0为任意IP     
  7. host    all             all             47.91.228.160/32        md5

編輯 postgresql.conf

vim  /etc/postgresql/12/main/postgresql.conf

  1. listen_addresses = '*'  
  2. archive_mode = on # 开启归档
  3. archive_command = 'test ! -f /var/lib/postgresql/12/main/pg_archive/%f && cp %p /var/lib/postgresql/12/main/pg_archive/%f'
  4. wal_keep_segments = 15          # in logfile segments; 0 disables
  5. wal_sender_timeout = 60s        # in milliseconds; 0 disables
  6. max_connections = 1000  # 最大连接数,据说从机需要大于或等于该值

# 保存退出
# 建立归档文件夹

  1. mkdir  /var/lib/postgresql/12/main/pg_archive

 

重启PostgreSQL

  1. service    postgresql   restart

三 、从服务器配置
首先 测试远程连接 【此处开始建议进入 postgres 用户进行接下来的操作】

  1. 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 操作)

  1. service    postgresql   stop

清空数据文件夹  

rm  -rf  /var/lib/postgresql/12/main (不建议)

建议重命名稍后删除

  1. cd    /var/lib/postgresql/12
  2. mv  main  old-main
  3. mkdir /var/lib/postgresql/12/main/

为新建的文件夹设置权限   否则启动报错

  1. data directory "/var/lib/postgresql/12/main" has invalid permissions
  1. chmod  750 /var/lib/postgresql/12/main/

     

     

从主节点拷贝数据 * 需要使用postgres用户进行操作否则启动从节点将会有问题

[postgres@world-alive:12]$ pg_basebackup -h 47.100.55.100 -D /var/lib/postgresql/12/main  -p 5432  -U replicator -Fp -Xs -Pv -R
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


如果用户不对执行如下代码(否则跳过):

  1. chown -R postgres:postgres /var/lib/postgresql/12/main


编辑 standby.signal 文件 添加如下

  1.     standby_mode = 'on'

编辑 postgresql.conf 新增或修改下列属性设置  (注意查看下 port 测试时发现 port不是默认值 5432  , 可能是我自己改的,不确定设置时注意看下)

  1.     primary_conninfo = 'host=主节点IP port=5432 user=replica password=replica用户的密码'
  2.     recovery_target_timeline = latest
  3.     max_connections = 1500     #大于主节点
  4.     hot_standby = on
  5.     max_standby_streaming_delay = 30s
  6.     wal_receiver_status_interval = 10s
  7.     hot_standby_feedback = on


四、测试主从服务

验证主从是否成功
登录主节点数据库执行如下命令

  1. su postgres
  2. psql
  3. select client_addr,sync_state from pg_stat_replication;

1. 查看从服务(WAL接收器进程)状态:

psql -c "\x" -c "SELECT * FROM pg_stat_wal_receiver;"
扩展显示已打开.
-[ 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的相关信息。

  1. $ ps aux | grep postgres
  2. postgres   ... postgres: walsender replicator 10.20.20.2(38418) streaming 0/3013BF8
  3. ...

2. 查看主服务(WAL发送器进程)状态:

psql -c "\x" -c "SELECT * FROM pg_stat_replication;"
-[ 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的相关信息。

  1. $ ps aux | grep postgres
  2. postgres   ...postgres:  walreceiver   streaming 0/3013BF8
  3. ...

3. 数据测试

    在master创建replica_demo数据库,在standby上可以看到同步效果

  1. [master] #CREATE DATABASE replica_demo;
  2. [standby]#\l

 

 

配置优化
参考:https://www.cnblogs.com/VicLiu/p/11854730.html

 

 

 

没有评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注