PostgreSQL热备之流复制环境搭建
本文介绍PostgreSQL数据库的热备技术,提供了类似Oracle的standby数据库的功能。PostgreSQL日志传送的方法有两种,一种是基于文件(base-file)的传送方式,一种是流复制(streaming replication)的方式。流复制方式是PostgreSQL提供的一种服务器间的数据复制方式,通过异步传送日志实现较低的数据延迟。

环境准备
在两台CentOS-7-x86_64操作系统的服务器上安装PostgreSQL10.10,主数据库服务器IP为192.168.88.191,备数据库服务器IP为192.168.88.192。数据目录为/db/pgsql_data/,安装目录为/db/pgsql/。
确保操作系统防火墙已关闭:
systemctl stop firewalld
systemctl disable firewalld
搭建PostgreSQL主备环境
在主节点中的操作:
1. 切换用户并启动服务:
su – postgres
pg_ctl start -D $PGDATA
2. 创建用于流复制的用户:
psql -h 127.0.0.1 -p 5432 -U postgres
create user repuser with login replication password 123456;
3. 修改pg_hba.conf文件,并添加允许复制用户和超级用户登录的内容:
host replication repuser 192.168.88.191/32 md5
host replication repuser 192.168.88.192/32 md5
host all postgres 192.168.88.191/32 trust
host all postgres 192.168.88.192/32 trust
4. 在postgresql.conf中设置一些参数,并重新启动主节点:max_wal_senders=10 wal_level=replica wal_log_hints=on wal_keep_segments=10 wal_receiver_status_interval=5s hot_standby_feedback=on
pg_ctl restart -D $PGDATA
5. 在主节点为后备服务器创建复制槽(非必须):
select * from pg_create_physical_replication_slot(postgresql_node191);
select * from pg_create_physical_replication_slot(postgresql_node192);
在备节点中的操作:
1. 停止服务并删除数据目录下的文件:
su – postgres
pg_ctl stop -D $PGDATA
cd $PGDATA
rm -rf *
2. 执行pg_basebackup命令,获取主节点的数据:
pg_basebackup -X
原创文章,作者:小编小本本,如若转载,请注明出处:https://www.benjiyun.com/yunzhujiyunwei/vps-yunwei/5875.html
