文章目录
- 一、 不再需要配置recovery.conf文件
- 二、 备库执行基础备份时新的命令行选项-R
- 三、 如何生成standby.signal文件
- 四、初次主备切换流程
- 1、主库停止
- 2、备库提升为新主库,对外提供服务
- 3、新主库修改pg_hba.conf文件
- 4、原主库新建$PGDATA/standby.signal文件
- 5、原主库修改$PGDATA/postgresql.auto.conf文件
- 6、启动原主库,变为新备库
- 五、以后主备切换流程
- 1、主库停止
- 2、备库提升为新主库,对外提供服务
- 3、原主库新建$PGDATA/standby.signal文件
- 4、启动原主库,变为新备库
一、 不再需要配置recovery.conf文件
从PostgreSQL 12开始,在执行通过流复制来配置主备数据库的时候,不再需要配置额外配置recovery.conf文件了。取而代之的是在备库环境的$PGDATA路径下配置一个standby.signal文件,注意该文件是1个普通的文本文件,内容为空。理解起来就是,该文件是一个标识文件。如果备库通过执行pg_ctl promote提升为主库的话,那么该文件将自动消失。
二、 备库执行基础备份时新的命令行选项-R
PostgreSQL 9.4.10版本:
[postgres@psql ~]$ pg_basebackup -V
pg_basebackup (PostgreSQL) 9.4.10
[postgres@psql ~]$ pg_basebackup --help
pg_basebackup takes a base backup of a running PostgreSQL server.Usage:pg_basebackup [OPTION]...Options controlling the output:-D, --pgdata=DIRECTORY receive base backup into directory-F, --format=p|t output format (plain (default), tar)-r, --max-rate=RATE maximum transfer rate to transfer data directory(in kB/s, or use suffix "k" or "M")-R, --write-recovery-confwrite recovery.conf after backup-T, --tablespace-mapping=OLDDIR=NEWDIRrelocate tablespace in OLDDIR to NEWDIR-x, --xlog include required WAL files in backup (fetch mode)-X, --xlog-method=fetch|streaminclude required WAL files with specified method
12版本是个分界线,12之前的如上所示,12以及之后如下所示
PostgreSQL 14.10版本:
[postgres@psql ~]$ pg_basebackup -V
pg_basebackup (PostgreSQL) 14.10
[postgres@psql ~]$ pg_basebackup --help
pg_basebackup takes a base backup of a running PostgreSQL server.pg_basebackup takes a base backup of a running PostgreSQL server.Usage:pg_basebackup [OPTION]...Options controlling the output:-D, --pgdata=DIRECTORY receive base backup into directory-F, --format=p|t output format (plain (default), tar)-r, --max-rate=RATE maximum transfer rate to transfer data directory(in kB/s, or use suffix "k" or "M")-R, --write-recovery-confwrite configuration for replication-T, --tablespace-mapping=OLDDIR=NEWDIRrelocate tablespace in OLDDIR to NEWDIR--waldir=WALDIR location for the write-ahead log directory-X, --wal-method=none|fetch|streaminclude required WAL files with specified method-z, --gzip compress tar output-Z, --compress=0-9 compress tar output with given compression level
从对比中,可以看到在9.4.10版本中,-R选项用于创建recovery.conf文件,而在14.10版本中,-R则是用于创建用于replication的配置文件,其实就是生成$PGDATA/standby.signal文件。
三、 如何生成standby.signal文件
两种方式,一种是在备库执行基础备份的时候,加上-R选项用于自动创建$PGDATA/standby.signal文件:
[postgres@psql pg14.10]$ pwd
/postgres/pg14.10
[postgres@psql pg14.10]$ ll
total 20
drwxrwxr-x 2 postgres postgres 4096 Oct 19 15:01 bin
drwxrwxr-x 6 postgres postgres 4096 Oct 19 15:01 include
drwxrwxr-x 4 postgres postgres 4096 Oct 19 15:01 lib
drwxrwxr-x 8 postgres postgres 4096 Oct 19 15:01 share
[postgres@psql pg14.10]$ pg_basebackup -h XXX -p 5432 -U replica --password -X stream -Fp --progress -D $PGDATA -R
Password:
24597/24597 kB (100%), 1/1 tablespace
[postgres@psql pg12.8]$ ll data
total 120
-rw------- 1 postgres postgres 224 Oct 20 15:11 backup_label
drwx------ 5 postgres postgres 4096 Oct 20 15:11 base
drwx------ 2 postgres postgres 4096 Oct 20 15:11 global
drwx------ 2 postgres postgres 4096 Oct 20 15:11 pg_commit_ts
drwx------ 2 postgres postgres 4096 Oct 20 15:11 pg_dynshmem
-rw------- 1 postgres postgres 4867 Oct 20 15:11 pg_hba.conf
-rw------- 1 postgres postgres 1636 Oct 20 15:11 pg_ident.conf
drwx------ 4 postgres postgres 4096 Oct 20 15:11 pg_logical
drwx------ 4 postgres postgres 4096 Oct 20 15:11 pg_multixact
drwx------ 2 postgres postgres 4096 Oct 20 15:11 pg_notify
drwx------ 2 postgres postgres 4096 Oct 20 15:11 pg_replslot
drwx------ 2 postgres postgres 4096 Oct 20 15:11 pg_serial
drwx------ 2 postgres postgres 4096 Oct 20 15:11 pg_snapshots
drwx------ 2 postgres postgres 4096 Oct 20 15:11 pg_stat
drwx------ 2 postgres postgres 4096 Oct 20 15:11 pg_stat_tmp
drwx------ 2 postgres postgres 4096 Oct 20 15:11 pg_subtrans
drwx------ 2 postgres postgres 4096 Oct 20 15:11 pg_tblspc
drwx------ 2 postgres postgres 4096 Oct 20 15:11 pg_twophase
-rw------- 1 postgres postgres 3 Oct 20 15:11 PG_VERSION
drwx------ 3 postgres postgres 4096 Oct 20 15:11 pg_wal
drwx------ 2 postgres postgres 4096 Oct 20 15:11 pg_xact
-rw------- 1 postgres postgres 337 Oct 20 15:11 postgresql.auto.conf
-rw------- 1 postgres postgres 26718 Oct 20 15:11 postgresql.conf
-rw------- 1 postgres postgres 0 Oct 20 15:11 standby.signal
方式2:如果在备库上执行pg_basebackup对主库进行备份的时候,没有使用-R选项的话,我们可以在备库的$PGDATA路径下,touch standby.signal就好了。
记住:该文件只是一个标识文件,它的存在就是告诉数据库,当我们执行pg_ctl start启动的时候,当前库的角色是standby,不是primary角色。
四、初次主备切换流程
1、主库停止
# 主库停止进程以及服务
pg_ctl stop -m fast
# 查看状态是否停止
pg_ctl status
# 显示:pg_ctl: no server running
通过pg_ctl stop -m fast停止原来的主库之后,数据库后台进程都没有了。
2、备库提升为新主库,对外提供服务
# 备库执行
pg_ctl promote
重要1:启动备库为新主库的命令是pg_ctl promote。
提升备库为主库之后,可以看到,后台进程中不再有startup recovering,以及walreceiver streaming进程了。同时,多了postgres: walwriter 写进程。
重要2:$PGDATA/standby.signal文件自动消失了。这是告诉PostgreSQL,我现在不再是备库了,我的身份是主库了。
3、新主库修改pg_hba.conf文件
修改新主库(原备库XXXIP)的$PGDATA/pg_hba.conf文件,在其中添加允许新备库(原主库XXXIP)可以通过replica用户访问数据库的条目信息。
host replication replicaotr XXXIP/32 md5
如果不做这一步配置的话,将来启动原主库为新备库的时候,可能会遇到下述错误。
CST [11394] FATAL: could not connect to the primary server: FATAL: no pg_hba.conf entry for replication connection from host "XXXIP", user "replica", SSL off
CST [11395] FATAL: could not connect to the primary server: FATAL: no pg_hba.conf entry for replication connection from host "XXXIP", user "replica", SSL off
注意:如果主从环境的数据库没有配置浮动IP的话,则这里的IP地址,应该直接填原主库的实际IP地址。
4、原主库新建$PGDATA/standby.signal文件
touch standby.signal
注意:这一步骤非常非常重要,如果不配置该文件的话,那么原来的主库一旦重新启动话,就将成为了1个新的独立主库,脱离了主从数据库环境。
5、原主库修改$PGDATA/postgresql.auto.conf文件
vim postgresql.auto.conf
# 添加如下内容
primary_conninfo = 'user=replica password=''youpasswd'' channel_binding=disable host=XXXIP这里是新主库的IP port=5432 sslmode=disable sslcompression=0 sslsni=1 ssl_min_protocol_version=TLSv1.2 gssencmode=disable krbsrvname=postgres target_session_attrs=any'
6、启动原主库,变为新备库
pg_ctl start
这样,就完成了第一次主从数据库环境的切换操作了。
五、以后主备切换流程
1、主库停止
# 主库停止进程以及服务
pg_ctl stop -m fast
# 查看状态是否停止
pg_ctl status
# 显示:pg_ctl: no server running
2、备库提升为新主库,对外提供服务
# 备库执行
pg_ctl promote
3、原主库新建$PGDATA/standby.signal文件
touch standby.signal
4、启动原主库,变为新备库
pg_ctl start
这样就完成了一次主备切换了。