postgresql_以pg_basebackup的方式部署流复制

部署流复制备库的数据复制环节主要包括以下3个步骤:
1.pg_stat_backup('francs_bk1');

2.拷贝主节点$PGDATA数据文件和表空间文件到备节点;

3.pg_stop_backup();

pg_basebackup工具对数据库实例级别进行物理备份,此工具需要超级用户权限或者replication权限,注意max_wal_sender的参数配置,因为pg_basebackup将至少消耗一个wal发送进程

在备库做一个基准备份

[postgres@localhost pg10]$ pg_basebackup -D /database/pg10/pg_root -Fp -Xs -v -P -h 192.168.12.10 -p 1921 -U repuser
pg_basebackup: initiating base backup, waiting for checkpoint to complete
pg_basebackup: checkpoint completed
pg_basebackup: write-ahead log start point: 0/E000028 on timeline 1
pg_basebackup: starting background WAL receiver
23784/23784 kB (100%), 1/1 tablespace
pg_basebackup: write-ahead log end point: 0/E0000F8
pg_basebackup: waiting for background process to finish streaming ...
pg_basebackup: base backup completed

pg_basebackup命令详解Options controlling the output:

-D, --pgdata=DIRECTORY receive base backup into directory  #指定备节点接收主数据库的目标路径,与主库保持一致
  -F, --format=p|t       output format (plain (default), tar)  #生成的备份数据格式,p(plain)格式(完全一样)和t(tar)格式(被打包成.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 for replication -S, --slot=SLOTNAME replication slot to use --no-slot prevent creation of temporary replication slot -T, --tablespace-mapping=OLDDIR=NEWDIR relocate tablespace in OLDDIR to NEWDIR -X, --wal-method=none|fetch|stream                  
#在备份工程中产生的wal日志包含在备份中的方式:
f(fetch)wal日志在基准备份完成后被传送到备节点,这是主库上的wal_keep_segments参数必须设置的较大,以免备份过程中的wal还没发送到备节点之前就被主库覆盖掉,主库只启动一个基准备份wal发送进程
s(stream),主库除了启动一个基准备份wal发送进程还会额外另起一个发送进程用于发送主库生产的wal增量日志流,避免主库中wal被覆盖(生产库推荐此方式特别是大库)
    include required WAL files with specified method
--waldir=WALDIR location for the write-ahead log directory -z, --gzip compress tar output -Z, --compress=0-9 compress tar output with given compression level General options: -c, --checkpoint=fast|spread set fast or spread checkpointing -l, --label=LABEL set backup label -n, --no-clean do not clean up after errors -N, --no-sync do not wait for changes to be written safely to disk -P, --progress show progress information    #显示数据文件、表空间文件近似传输百分比 -v, --verbose output verbose messages    #打印各阶段的日志 -V, --version output version information, then exit -?, --help show this help, then exit Connection options: -d, --dbname=CONNSTR connection string -h, --host=HOSTNAME database server host or socket directory -p, --port=PORT database server port number -s, --status-interval=INTERVAL time between status packets sent to server (in seconds) -U, --username=NAME connect as specified database user -w, --no-password never prompt for password -W, --password force password prompt (should happen automatically) Report bugs to <pgsql-bugs@postgresql.org>.

 pg_basebackup执行成功之后

从家目录复制recovery.conf文件到$PGDATA目录

[postgres@localhost pg10]$ cd pg_root/
[postgres@localhost pg_root]$ cat recovery.conf
standby_mode = on

#primary_conninfo = 'host=192.168.12.10 port=1921 user=repuser password=Aa123456'
primary_conninfo = 'host=192.168.12.10 port=1921 user=repuser'
recovery_target_timeline = 'latest'

 

启动报错,可以看出是权限问题,之后重新赋权

[postgres@localhost pg10]$ pg_ctl start
waiting for server to start....2021-11-01 04:03:25.562 EDT [3849] FATAL:  data directory "/database/pg10/pg_root" has group or world access
2021-11-01 04:03:25.562 EDT [3849] DETAIL:  Permissions should be u=rwx (0700).
 stopped waiting
pg_ctl: could not start server
Examine the log output.
[postgres@localhost pg10]$ chmod 700 -R /database/pg10/pg_root
[postgres@localhost pg10]$ pg_ctl start
waiting for server to start....2021-11-01 04:04:31.103 EDT [3854] LOG:  listening on IPv4 address "0.0.0.0", port 1921
2021-11-01 04:04:31.103 EDT [3854] LOG:  listening on IPv6 address "::", port 1921
2021-11-01 04:04:31.105 EDT [3854] LOG:  listening on Unix socket "/tmp/.s.PGSQL.1921"
2021-11-01 04:04:31.142 EDT [3855] LOG:  database system was interrupted; last known up at 2021-11-01 03:35:34 EDT
2021-11-01 04:04:31.150 EDT [3855] LOG:  entering standby mode
2021-11-01 04:04:31.152 EDT [3855] LOG:  redo starts at 0/E000028
2021-11-01 04:04:31.153 EDT [3855] LOG:  consistent recovery state reached at 0/E0000F8
2021-11-01 04:04:31.154 EDT [3854] LOG:  database system is ready to accept read only connections
2021-11-01 04:04:31.164 EDT [3859] LOG:  started streaming WAL from primary at 0/F000000 on timeline 1
 done
server started

 

posted @ 2021-11-01 15:59  罗论明  阅读(303)  评论(0编辑  收藏  举报