PostgreSQL的数据库备份(主从复制)

一、 创建相应的目录

1.Master 节点 [10.242.135.51]

1.1 创建Master 的数据目录
mkdir -p <your postgres master node data path>
madir -p /var/testPSQL/master

cd <your postgres master node data path>
cd /var/testPSQL/master

cd ../ && chmod -R 777 <data directory's name> 
chmod -R 777 master
1.2 创建Master 容器
get current user id 或者 echo $UID
id <current username>


docker run -dit \
--name postgres \
--restart always \
-e POSTGRES_PASSWORD=<your password> \
-v <your postgres master node data's path>:/var/lib/postgresql/data \
-p 5432:5432 \
-u <current user id> \
postgres[:tag]


eg:
	docker run -dit \
	--name postgres \
	--restart always \
	-e POSTGRES_PASSWORD=123456 \
	-v /var/testPSQL/master:/var/lib/postgresql/data \
	-p 5432:5432 \
	-u 0 \
	postgres:13.1
1.3 Mater的配置
docker exec -it postgres /bin/bash
psql -U postgres
#创建用户
CREATE ROLE replica login replication encrypted password '';
eg: 
	CREATE ROLE replica login replication encrypted password '1234567';
	
# 退出	
\q
exit
cd <your postgres master node data path>
eg: 
	cd /var/testPSQL/master

echo host replication replica <postgres' slave node IP address>/32 trust >> pg_hba.conf

# 修改配置文件
vi postgresql.conf

listen_addresses = '*' 
wal_level = replica
archive_mode = on
# 重启容器
docker restart postgres

1.4Master节点测试
docker exec -it postgres /bin/bash
psql -U postgres
show archive_mode;

PostgreSQL的数据库备份(主从复制)_第1张图片

二、Slave 节点 [10.242.135.51]

2.1创建Slave的数据目录
mkdir -p <your postgres slave node data's path>
madir -p /var/testPSQL/slave 

cd <your postgres slave node data path>
cd madir -p /var/testPSQL/slave 

cd ../ && chmod -R 777 <data directory name> 
chmod -R 777 slave 
2.2 创建Slave容器
get current user id 或者 echo $UID
id <current username>


docker run -dit \
--name postgres \
--restart always \
-e POSTGRES_PASSWORD=<your password> \
-v <your postgres slave node data's path>:/var/lib/postgresql/data \
-p 5432:5432 \
-u <current user id> \
postgres[:tag]


eg:
	docker run -dit \
	--name postgres \
	--restart always \
	-e POSTGRES_PASSWORD=123456 \
	-v /var/testPSQL/slave:/var/lib/postgresql/data \
	-p 5432:5432 \
	-u 0 \
	postgres:13.1
2.3 Slave的配置
docker exec -it postgres /bin/bash
rm -rf /var/lib/postgresql/data/*
pg_basebackup -h <master node's IP> -p 5432 -U replica -Fp -Xs -Pv -R -D /var/lib/postgresql/data
eg:
	pg_basebackup -h 10.242.135.51 -p 5432 -U replica -Fp -Xs -Pv -R -D /var/lib/postgresql/data
# 在容器外部
cd <your postgres slave node data path>
eg:
	cd /var/testPSQL/slave 
touch standby.signal && echo standby_mode = 'on' >> standby.signal

# 修改配置文件
vi postgresql.conf
max_connections = 120                	# must be bigger than the
Master Node
wal_level = replica						
recovery_target_timeline = 'latest'    	
primary_conninfo = 'host=s IP> port=5432 user=replica password=<maser node user "replica"'s password>'	
hot_standby = on   

eg:
	max_connections = 120                	# must be bigger than the
	Master Node
	wal_level = replica						
	recovery_target_timeline = 'latest'    	
	primary_conninfo = 'host=< 10.242.135.51 port=5432 user=replica password=1234567 '	
	hot_standby = on   


三、重启容器测试即可

docker restart postgres

你可能感兴趣的:(postgresql)