如题,关于postgres 12.1版本做主从备份
一开始安装了两个数据库,照着好多教程,配置主数据库后,数据库无法启动,查看启动日志,发现是主数据库的postgresql.conf中的配置了wal_keep_segments,配置文件中有这个参数的说明,但是配置了就无法启动,原因在查找中
折腾了一天,最后在大神的帮助下,基于网上现成的9.5版本的docker,改造调试了下,将12.1版本的整理出来
直接上配置:(将下面四个文件放在一个目录下)
docker-compose
- version: '2.0'
-
- services:
-
- # 从数据库
- postgres-slave:
- build:
- context: ./
- dockerfile: ./Dockerfile
- container_name: PG-SLAVE
- ports:
- - 20002:5432
- links:
- - postgres-master
- environment:
- REPLICATION_ROLE: slave
- POSTGRES_MASTER_SERVICE_HOST: postgres-master
- POSTGRES_PASSWORD: 123456
-
- # 主数据库
- postgres-master:
- container_name: PG-MASTER
- build:
- context: ./
- dockerfile: ./Dockerfile
- ports:
- - 20001:5432
- environment:
- REPLICATION_ROLE: master
- POSTGRES_PASSWORD: 123456
dockerfile
- # -*- mode: conf -*-
- FROM postgres:12.1
-
- MAINTAINER ***
- # common settings
- ENV MAX_CONNECTIONS 500
- ENV MAX_WAL_SENDERS 100
-
- # master/slave settings
- ENV REPLICATION_USER replication
- ENV REPLICATION_PASSWORD 123456
- # slave settings
- ENV POSTGRES_MASTER_SERVICE_HOST localhost
- ENV POSTGRES_MASTER_SERVICE_PORT 5432
-
- COPY 10-config.sh /docker-entrypoint-initdb.d/
- COPY 20-replication.sh /docker-entrypoint-initdb.d/
- # Evaluate vars inside PGDATA at runtime.
- # For example HOSTNAME in 'ENV PGDATA=/mnt/$HOSTNAME'
- # is resolved runtime rather then during build
- RUN sed -i 's/set -e/set -e -x\nPGDATA=$(eval echo "$PGDATA")/' /docker-entrypoint.sh
两个脚本文件
10-config.sh
- #!/bin/bash
- set -e
-
- echo [*] configuring $REPLICATION_ROLE instance
-
- echo "max_connections = $MAX_CONNECTIONS" >> "$PGDATA/postgresql.conf"
-
- # We set master replication-related parameters for both slave and master,
- # so that the slave might work as a primary after failover.
- echo "wal_level = hot_standby" >> "$PGDATA/postgresql.conf"
- echo "max_wal_senders = $MAX_WAL_SENDERS" >> "$PGDATA/postgresql.conf"
- # slave settings, ignored on master
- echo "hot_standby = on" >> "$PGDATA/postgresql.conf"
-
- echo "host replication $REPLICATION_USER 0.0.0.0/0 trust" >> "$PGDATA/pg_hba.conf"
20-replication.sh
- #!/bin/bash
- set -e
-
- if [ $REPLICATION_ROLE = "master" ]; then
- psql -U postgres -c "CREATE ROLE $REPLICATION_USER WITH REPLICATION PASSWORD '$REPLICATION_PASSWORD' LOGIN"
-
- elif [ $REPLICATION_ROLE = "slave" ]; then
- # stop postgres instance and reset PGDATA,
- # confs will be copied by pg_basebackup
- pg_ctl -D "$PGDATA" -m fast -w stop
- # make sure standby's data directory is empty
- rm -r "$PGDATA"/*
-
- pg_basebackup \
- --write-recovery-conf \
- --pgdata="$PGDATA" \
- --username=$REPLICATION_USER \
- --host=$POSTGRES_MASTER_SERVICE_HOST \
- --port=$POSTGRES_MASTER_SERVICE_PORT \
- --progress \
- --verbose
-
- #--xlog-method=fetch \
-
- # useless postgres start to fullfil docker-entrypoint.sh stop
- pg_ctl -D "$PGDATA" \
- -o "-c listen_addresses='*'" \
- -w start
- fi
-
- echo [*] $REPLICATION_ROLE instance configured!
文件部署后,直接运行 docker-compose up
该配置是在同一个主机上部署,如果要将两个数据库部署在不同主机
则将docker-compose中的主备分开,先在主机上跑
在从机上部署时,需要把主机的数据库ip和端口手动写入dockerfile和20-replication.sh文件中
信息加载中,请等待
微信客服(速回)
微信客服(慢回)