在centos7中安装部署pg13及主从复制

    这里记录的是在互联网环境下安装部署的过程,要求服务器可以联网。

1.添加yum源

 sudo yum -y install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

yum install centos-release-scl-rh

yum install llvm-toolset-7-clang

2.开始安装

sudo yum -y install postgresql13 postgresql13-server

3.开始配置主库(master库),假设现在主库的IP地址是192.168.0.1

sudo /usr/pgsql-13/bin/postgresql-13-setup initdb

sudo systemctl start postgresql-13

sudo systemctl enable postgresql-13

以上完成了主库的数据库的初始化及正常启动

修改pg_hba.conf配置文件,设置允许远程访问的IP及复制从库规则

host   replication    all   192.168.0.2/32   trust

host   replication    all   192.168.0.3/32   trust

修改主库的postgresql.conf文件,设定监听IP等参数

listen_addresses = '*'

synchronous_standby_names = '*'

在主库里面创建一个用于数据复制的用户

su postgres

psql

create role rpl login replication encrypted password '12345678';

不要退出当前的psql环境,创建两个用于从库复制的slot

select * from pg_create_physical_replication_slot('standby1_slot');

select * from pg_create_physical_replication_slot('standby2_slot');

\q退出psql,exit退出postgres用户

systemctl restart postgresql-13

主库这边基本就完事了

4.配置从库

和前面一样,yum install postgresql13 postgresql13-server

然后建立和主库一样的数据目录,如/data/pgsql/data

vi /usr/lib/systemd/system/postgresql-13.service

将Environment=PGDATA这里的PGDATA指向你建立的目录,建议与主库保持一致

执行以下命令备份主库数据

pg_basebackup --pgdata=/data/pgsql/data --format=p --write-recovery-conf --checkpoint=fast --label=mffb --progress --host=192.168.0.1 --port=5432 --username=rpl

备份完成以后,给postgres用户访问权限

sudo chown -R postgres:postgres /data/pgsql/data

修改从库的postgresql.conf

vi /data/pgsql/data/postgresql.conf

增加以下两行内容:

primary_conninfo = 'host=192.168.0.1 port=5432 user=rpl password=12345678' 

primary_slot_name = 'standby1_slot' 

第二台从库将primary_slot_name配置为standby2_slot,注意不要配置一样了,那样的话只有一个从库能拉取到主库的变化

systemctl restart postgresql-13启动两台从库就完事了

回到主库里面,再次执行psql,执行以下查询SQL

select slot_name,slot_type,active,wal_status from pg_replication_slots;

要是active显示两行t就是配置成功了。

参考资料

https://computingforgeeks.com/how-to-install-postgresql-13-on-centos-7/

https://www.highgo.ca/2021/02/03/how-to-setup-postgres-13-wal-streaming-replication-on-ubuntu-18-04/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

peihexian

你的鼓励是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值