基于PG 12.4 版本安装
主服务器ip:172.24.6.227
从服务器ip:172.24.6.228
docker pull postgres:12.4-alpine
官方镜像地址:https://hub.docker.com/_/postgres?tab=description
可根据自己需要下载其他版本的 PG。注意PG12版本前新建recovery.conf2步骤3:运行容器
通过指定端口映射,目录映射,容器名运行容器。
- mkdir -p /home/pg_p/data
-
- docker run --name pg_p --restart=always -v /home/pg_p/data:/var/lib/postgresql/data -e POSTGRES_PASSWORD=123456 -p 15432:5432 -d postgres:12.4-alpine
- mkdir -p /home/pg_p/data
-
- docker run --name pg_p --restart=always -v /home/pg_p/data:/var/lib/postgresql/data -e POSTGRES_PASSWORD=123456 -p 15432:5432 -d postgres:12.4-alpine
参数说明:
进入容器命令:
- [root@lei ~]# docker exec -it pg_p /bin/bash
- bash-5.0# su - postgres
- bff818d570c9:~$ psql
- psql (12.4)
- Type "help" for help.
-
- postgres=#
- CREATE ROLE replica login replication encrypted password 'replica';
-
- postgres=# \du
- List of roles
- Role name | Attributes | Member of
- -----------+------------------------------------------------------------+-----------
- postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
- replica | Replication | {}
-
pg_hba.conf
修改pg_hba.conf
,允许 replica
用户从 172.24.6.228从库上连接主库。配置文件在本地:/home/pg_p/data
目录下,如
host replication replica 172.24.6.228/32 trust #允许172.24.6.228使用 replica 用户来复制
postgresql.conf
必须修改的配置是前面三个,后面几个看情况修改。
- listen_addresses = '*' # 监听所有IP
- archive_mode = on # 允许归档
- wal_level = replica # 开启热备
- archive_command = '/bin/date' # 用该命令来归档logfile segment,这里取消归档。
- max_wal_senders = 32 # 这个设置了可以最多有几个流复制连接,差不多有几个从,就设置几个
- wal_keep_segments = 64 # 设置流复制保留的最多的xlog数目,一份是 16M,注意机器磁盘 16M*64 = 1G
- wal_sender_timeout = 60s # 设置流复制主机发送数据的超时时间
- max_connections = 200 # 这个设置要注意下,从库的max_connections必须要大于主库的
docker restart pg_p
- postgres=# show archive_mode;
- archive_mode
- --------------
- on
- (1 row)
修改从库相关配置,以及从主库初始化数据过来。
进入容器删除原始数据文件,利用pg_basebackup
从主库初始化数据。
- [root@lei ~]# docker exec -it pg_s /bin/bash
- bash-5.0# su - postgres
- # 删除原有数据文件
- cedd81ee4e30:~$ rm -rf /var/lib/postgresql/data/*
- # 备份恢复
- cedd81ee4e30:~$ pg_basebackup -h 172.24.6.227 -p 15432 -U replica -Fp -Xs -Pv -R -D /var/lib/postgresql/data
- pg_basebackup: initiating base backup, waiting for checkpoint to complete
- pg_basebackup: checkpoint completed
- pg_basebackup: write-ahead log start point: 0/2000028 on timeline 1
- pg_basebackup: starting background WAL receiver
- pg_basebackup: created temporary replication slot "pg_basebackup_43"
- 24636/24636 kB (100%), 1/1 tablespace
- pg_basebackup: write-ahead log end point: 0/2000138
- pg_basebackup: waiting for background process to finish streaming ...
- pg_basebackup: syncing data to disk ...
- pg_basebackup: base backup completed
pg_basebackup参数说明:
- pg_basebackup -h 172.24.6.227 -p 15432 -U replica -Fp -Xs -Pv -R -D /var/lib/postgresql/data
-
- -h, --host=HOSTNAME 数据库服务器主机或套接字目录
- -p, --port=PORT 数据库服务器端口号
- -U, --username=NAME 以指定的数据库用户连接
- -F, --format=p|t 输出格式(plain(默认),tar)
- -X, --wal-method=none|fetch|stream 包含指定方法所需的 WAL 文件
- -P, --progress 显示进度信息
- -R, --write-recovery-conf 用于复制的写入配置
- -D, --pgdata=DIRECTORY 接收基本备份到目录
-
standby.signal
文件standby_mode = 'on'
postgresql.conf
- wal_level = replica # WAL 日志级别为 replica
- primary_conninfo = 'host=172.24.6.214 port=15432 user=replica password=123456' # 主库连接信息
- hot_standby = on # 恢复期间,允许查询
- recovery_target_timeline = latest # 默认
- max_connections = 120 # 大于等于主节点,正式环境应当重新考虑此值的大小
docker restart pg_s
方法1:主库上执行以下命令,可以看到从库信息
- postgres=# select client_addr,sync_state from pg_stat_replication;
- client_addr | sync_state
- ---------------+------------
- 172.24.6.228 | async
- (1 row)
方法2:主库上新建数据库
主库
- CREATE DATABASE test01
- postgres=#
- postgres=# \c test01
- You are now connected to database "test01" as user "postgres".
- test01=# create table test(name varchar(50));
- CREATE TABLE
- test01=# insert into test values('tom');
- INSERT 0 1
从库
- postgres-# \l
- List of databases
- Name | Owner | Encoding | Collate | Ctype | Access privileges
- -----------+----------+----------+------------+------------+-----------------------
- test01 | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
- postgres | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
- template0 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres +
- | | | | | postgres=CTc/postgres
- template1 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres +
- | | | | | postgres=CTc/postgres
- (4 rows)
-
- postgres-# \c test01
- You are now connected to database "test01" as user "postgres".
- test01=# select * from test;
- name
- ------
- tom
- (1 row)