admin管理员组

文章数量:1666616

postgresql主从复制、主从切换

一、 不再需要配置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-conf
                         write recovery.conf after backup
  -T, --tablespace-mapping=OLDDIR=NEWDIR
                         relocate tablespace in OLDDIR to NEWDIR
  -x, --xlog             include required WAL files in backup (fetch mode)
  -X, --xlog-method=fetch|stream
                         include required WAL files with specified method

PostgreSQL 12.8版本:

[postgres@psql ~]$ pg_basebackup -V
pg_basebackup (PostgreSQL) 12.8
[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-conf
                         write configuration for replication
  -T, --tablespace-mapping=OLDDIR=NEWDIR
                         relocate tablespace in OLDDIR to NEWDIR
      --waldir=WALDIR    location for the write-ahead log directory
  -X, --wal-method=none|fetch|stream
                         include 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文件,而在12.8版本中,-R则是用于创建用于replication的配置文件,其实就是生成$PGDATA/standby.signal文件。

三、 如何生成standby.signal文件

两种方式,一种是在备库执行基础备份的时候,加上-R选项用于自动创建$PGDATA/standby.signal文件:

[postgres@psql pg12.8]$ pwd
/postgres/pg12.8
[postgres@psql pg12.8]$ 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 pg12.8]$ pg_basebackup -h 192.168.40.133 -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 

本文标签: 主从postgresql