PostgreSQL 热备配置

这里只配置了热备,没有配置归档处理,可能会导致伺服器关键数据无法回滚1,或许下个文章会写一下(

认证配置2

首先我们需要创建一个可用于热备的账户,建议使用一个专用的账户用来做这个事情。

这个账户需要有 REPLICATIONLOGIN 的权限。虽然 REPLICATION 的权限很大,但是它并不能修改主伺服器的数据。

如果这个账户 foo 且账户是来自 192.168.1.100 则可以把以下配置放到主机的 pg_hba.conf 中。

# Allow the user "foo" from host 192.168.1.100 to connect to the primary
# as a replication standby if the user's password is correctly supplied.
#
# TYPE  DATABASE        USER            ADDRESS                 METHOD
host    replication     foo             192.168.1.100/32        md5

同时,把以下的连接讯息放在备用伺服器的 primary_conninfo

这里假设主伺服器的 ip 位址是 192.168.1.50

# The standby connects to the primary that is running on host 192.168.1.50
# and port 5432 as the user "foo" whose password is "foopass".
primary_conninfo = 'host=192.168.1.50 port=5432 user=foo password=foopass'

配置伺服器

主伺服器

首先我们需要配置主伺服器,修改部分 postgresql.conf 文件内容

wal_level = replica
full_page_writes = on
wal_log_hints = on

配置好后,这里我们需要重启一下伺服器

root@:~ $ systemctl restart posgresql.service

(然后我在这边折腾了一个多小时,原因是配置文件全部改到旧的文件上去了,我说怎么老是用不了)

从伺服器

首先确保从伺服器 postgresql.service 没有在运行,而后确认从伺服器没有重要资料后,将从伺服器的数据直接删除(当然,你想备份把它移走也是可以的)

postgres@:~ $ rm -rf ~/data/

使用 pg_basebackup 指令,从远端拷贝一份数据到本地

此处加上 --write-recovery-conf 指令,这样可以直接将 primary_conninfo 写入到配置文件中。

此处也可以加上 --wal-method=stream 避免复制时间太久导致最后一步失败3

postgres@:~ $ pg_basebackup --write-recovery-conf -h 192.168.1.50 -U foo -D data -P
Password:
713513/713513 kB (100%), 1/1 tablespace

复制完成后,可以检查 data/postgres.conf 文件,可以看到 primary_conninfo 已经帮我们写上了

primary_conninfo = 'user=foo password=foopass host=192.168.1.50 port=5432' # connection string to sending server

但是还没完,我们还要进一步修改内容

hot_standby = on

上面跟主伺服器重复的选项我这里把它注释掉了,好像没有什么影响。

修改完后保存,并且创建一个 standby.signal 文件

postgres@:~ $ touch data/standby.signal

这样就可以启动伺服器了。

检查状态4

主伺服器

postgres@:~ $ psql
psql (13.2)
Type "help" for help.

postgres=# \x
Expanded display is on.
postgres=# select * from pg_stat_replication;
-[ RECORD 1 ]----+------------------------------
pid              | 12911
usesysid         | 16570
usename          | rundll32
application_name | walreceiver
client_addr      | 10.1.96.3
client_hostname  |
client_port      | 57676
backend_start    | 2021-04-17 01:21:38.533617+08
backend_xmin     |
state            | streaming
sent_lsn         | 1/1D114338
write_lsn        | 1/1D114338
flush_lsn        | 1/1D114338
replay_lsn       | 1/1D114338
write_lag        |
flush_lag        |
replay_lag       |
sync_priority    | 0
sync_state       | async
reply_time       | 2021-04-17 02:20:34.118557+08

postgres=#

从伺服器

postgres@:~ $ psql
psql (13.2)
Type "help" for help.

postgres=# \x
Expanded display is on.
postgres=# select * from pg_stat_wal_receiver;
-[ RECORD 1 ]---------+---------------------------------------------
pid                   | 871359
status                | streaming
receive_start_lsn     | 1/1D000000
receive_start_tli     | 1
written_lsn           | 1/1D117658
flushed_lsn           | 1/1D117658
received_tli          | 1
last_msg_send_time    | 2021-04-17 02:20:50.522413+08
last_msg_receipt_time | 2021-04-17 02:20:50.520713+08
latest_end_lsn        | 1/1D117658
latest_end_time       | 2021-04-17 02:20:50.522413+08
slot_name             |
sender_host           | 192.168.1.50
sender_port           | 5432
conninfo              | user=foo password=******** host=192.168.1.50

postgres=#

参考资料

https://www.postgresql.org/docs/current/hot-standby.html https://www.postgresql.org/docs/current/runtime-config-replication.html https://www.postgresql.org/docs/current/app-pgbasebackup.html https://linux.onlinedoc.tw/2016/05/centos7rhel7-postgresql-replication.html https://www.postgresql.org/docs/current/transaction-iso.html