容县建设工程交易中心网站,专业模板建站公司,网站分为那几个模块,青岛建设局官方网站文章目录 一、 不再需要配置recovery.conf文件二、 备库执行基础备份时新的命令行选项-R三、 如何生成standby.signal文件四、初次主备切换流程1、主库停止2、备库提升为新主库#xff0c;对外提供服务3、新主库修改pg_hba.conf文件4、原主库新建$PGDATA/standby.signal文件5、… 文章目录 一、 不再需要配置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版本
[postgrespsql ~]$ pg_basebackup -V
pg_basebackup (PostgreSQL) 9.4.10
[postgrespsql ~]$ pg_basebackup --help
pg_basebackup takes a base backup of a running PostgreSQL server.Usage:pg_basebackup [OPTION]...Options controlling the output:-D, --pgdataDIRECTORY receive base backup into directory-F, --formatp|t output format (plain (default), tar)-r, --max-rateRATE 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-mappingOLDDIRNEWDIRrelocate tablespace in OLDDIR to NEWDIR-x, --xlog include required WAL files in backup (fetch mode)-X, --xlog-methodfetch|streaminclude required WAL files with specified method
12版本是个分界线12之前的如上所示12以及之后如下所示 PostgreSQL 14.10版本
[postgrespsql ~]$ pg_basebackup -V
pg_basebackup (PostgreSQL) 14.10
[postgrespsql ~]$ 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, --pgdataDIRECTORY receive base backup into directory-F, --formatp|t output format (plain (default), tar)-r, --max-rateRATE 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-mappingOLDDIRNEWDIRrelocate tablespace in OLDDIR to NEWDIR--waldirWALDIR location for the write-ahead log directory-X, --wal-methodnone|fetch|streaminclude required WAL files with specified method-z, --gzip compress tar output-Z, --compress0-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文件:
[postgrespsql pg14.10]$ pwd
/postgres/pg14.10
[postgrespsql 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
[postgrespsql 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
[postgrespsql 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 userreplica passwordyoupasswd channel_bindingdisable hostXXXIP这里是新主库的IP port5432 sslmodedisable sslcompression0 sslsni1 ssl_min_protocol_versionTLSv1.2 gssencmodedisable krbsrvnamepostgres target_session_attrsany6、启动原主库变为新备库
pg_ctl start这样就完成了第一次主从数据库环境的切换操作了。
五、以后主备切换流程
1、主库停止
# 主库停止进程以及服务
pg_ctl stop -m fast
# 查看状态是否停止
pg_ctl status
# 显示pg_ctl: no server running2、备库提升为新主库对外提供服务
# 备库执行
pg_ctl promote3、原主库新建$PGDATA/standby.signal文件
touch standby.signal4、启动原主库变为新备库
pg_ctl start这样就完成了一次主备切换了。