吹静静

欢迎QQ交流:592590682

环境介绍

Linux主机,安装两个docker postgres 容器,并实现主从配置。

数据库版本:12

主库端口:5433

从库端口:5434

主节点操作

(1)创建工作目录

mkdir -p /home/hadoop/data/pg/master

(2)拉取镜像并启动容器

docker run -d \
-p 5433:5432 \
-e POSTGRES_PASSWORD=postgres \
-v /home/hadoop/data/pg/master/:/var/lib/postgresql/data \
--name pg12master \
-h pg12master \
-e LANG="C.UTF-8" \
-e 'TZ=Asia/Shanghai'  \
-m 256m  \
--memory-swap -1 \
postgres:12

(3)修改配置文件

pg_hba.conf

# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
host    all             all             ::1/128                 trust

# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     trust
host    replication     all             127.0.0.1/32            trust
host    replication     all             ::1/128                 trust
host    replication     all             0.0.0.0/0               md5
host    replication repuser             172.17.0.0/24           md5
host    all             all             0.0.0.0/0               md5

postgresql.conf

wal_level = logical    
archive_mode = on
archive_command = '/bin/date'
max_wal_senders = 10
wal_keep_segments = 16
synchronous_standby_names = '*'

(4)基础数据备份

进入master容器,备份基础数据。

[root@bigdata3 ~]# docker exec -it pg12master /bin/bash
root@pg12master:/# pg_basebackup -R -D /var/lib/postgresql/slave -Fp -Xs -v -P -h 172.17.0.1 -p 5433 -U postgres
Password: postgres
pg_basebackup: initiating base backup, waiting for checkpoint to complete
pg_basebackup: checkpoint completed
pg_basebackup: write-ahead log start point: 0/B000028 on timeline 1
pg_basebackup: starting background WAL receiver
pg_basebackup: created temporary replication slot "pg_basebackup_193"
24729/24729 kB (100%), 1/1 tablespace                                         
pg_basebackup: write-ahead log end point: 0/B000100
pg_basebackup: waiting for background process to finish streaming ...
pg_basebackup: syncing data to disk ...
pg_basebackup: base backup completed
root@pg12master:/#

将容器中的基础数据下载到本地。

docker cp pg12master:/var/lib/postgresql/slave /home/hadoop/data/pg

从节点操作

(1)启动从节点容器

docker run -d \
--ip 172.17.0.5 \
-p 5434:5432 \
--name pg12slave \
-h pg12slave \
-e LANG="C.UTF-8" \
-e 'TZ=Asia/Shanghai' \
-e "POSTGRES_DB=postgres" \
-e "POSTGRES_USER=postgres" \
-e "POSTGRES_PASSWORD=postgres" \
-v /home/hadoop/data/pg/slave:/var/lib/postgresql/data  \
-m 256m  \
--memory-swap -1 \
postgres:12

注意:IP地址为Docker的IP。

检查测试

检查主库是否可以检查到从库。

docker exec -it -u postgres pg12master /bin/bash
postgres@pg12master:/$ psql
psql (12.3 (Debian 12.3-1.pgdg100+1))
Type "help" for help.
 
postgres=# select * from pg_stat_replication;
 pid | usesysid | usename | application_name | client_addr  | client_hostname | client_port |         backend_start         | backend_xmin |   state   | sent_lsn  | write_lsn | flush_lsn | replay_lsn | write_l
ag | flush_lag | replay_lag | sync_priority | sync_state |          reply_time           
-----+----------+---------+------------------+--------------+-----------------+-------------+-------------------------------+--------------+-----------+-----------+-----------+-----------+------------+--------
---+-----------+------------+---------------+------------+-------------------------------
 149 |    16384 | repuser | walreceiver      | 172.17.0.1   |                 |       51752 | 2021-11-25 15:21:28.80868+08  |              | streaming | 0/A004518 | 0/A004518 | 0/A004518 | 0/A004518  |        
   |           |            |             1 | sync       | 2021-11-25 18:42:51.27092+08
(2 rows)
 
postgres=# 

使用Navcat连接主库,建表,并查看从库是否自动建表。

 

posted on 2022-06-08 13:47  吹静静  阅读(607)  评论(0编辑  收藏  举报