4

PostgreSql 14 主从负载均衡部署记录

 2 years ago
source link: https://www.ioiox.com/archives/154.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

前些天给个环境部署PostgreSQL 主从负载均衡,这里仅仅简单记录一下命令以备后续使用,至于数据库的更多配置请自行根据需求配置.

本文为 Stille 原创文章.经实践,测试,整理发布.如需转载请联系作者获得授权,并注明转载地址.

  • 服务器系统: CentOS 7.9
  • PostgreSQL 版本: 14
  • 主数据库内网 IP : 10.0.0.2
  • 从数据库内网 IP : 10.0.0.3

主数据库 10.0.0.2

sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
sudo yum install -y postgresql14-server
## 主库初始化数据库
/usr/pgsql-14/bin/postgresql-14-setup initdb
## 启动
sudo systemctl enable postgresql-14
sudo systemctl start postgresql-14
su - postgres
psql
## 创建 postgres 密码
ALTER USER postgres WITH PASSWORD '123456';
# #创建 从库 replica 用户密码
CREATE ROLE replica login replication encrypted password 'replica';
## 检查账号
SELECT usename from pg_user;
# 结果如下

# usename
# ----------
# postgres
# replica
# (2 rows)

# 查看权限
SELECT rolname from pg_roles;
# 结果如下

# rolname
# ----------
# postgres
# replica
# (2 rows)

# 退出
\q
exit

pg_hba.conf

vi /var/lib/pgsql/14/data/pg_hba.conf
## 添加从库网段
host    all             all             0.0.0.0/0               trust
# replication privilege.
local   replication     all                                     peer
host    replication     replica            10.0.0.3/24        md5
注意此处 10.0.0.3/24 需修改为从库的 IP 段

postgresql.conf

vi /var/lib/pgsql/14/data/postgresql.conf
listen_addresses = '*'
wal_level = hot_standby
synchronous_commit = remote_write
# synchronous_commit 参考文档可选其他 on
max_wal_senders = 32     #同步最大的进程数量
wal_sender_timeout = 60s #流复制主机发送数据的超时时间
max_connections = 100    #最大连接数,从库的max_connections必须要大于主库的

从数据库 10.0.0.3

sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
sudo yum install -y postgresql14-server
## 从主库同步数据
pg_basebackup -D /var/lib/pgsql/14/data -h 10.0.0.2 -p 5432 -U replica -X stream -P
注意从库无需上文主库安装流程中的初始化步骤,修改上述 10.0.0.2 为主库 IP 地址来同步数据库.

postgresql.conf

从 PostgreSQL 12 开始已经移除了 recovery.conf 文件,相关配置合并到了 postgresql.conf 中,由于从主库同步数据库,其中配置也需要移除和修改.
vi /var/lib/pgsql/14/data/postgresql.conf
## 移除或注释 wal_level
wal_level = xxx
## 修改或添加以下
primary_conninfo = 'host=10.0.0.2 port=5432 user=replica password=replica'
recovery_target_timeline = 'latest'

创建 standby.signal

创建 standby.signal 文件,声明从库.
vi /var/lib/pgsql/14/data/standby.signal
standby_mode = on
## 声明从库
此处是踩坑过几次.
chown -R postgres.postgres /var/lib/pgsql/14/data
sudo systemctl enable postgresql-14
sudo systemctl start postgresql-14

主数据库 10.0.0.2

ps aux |grep sender
# 返回 postgres: walsender replica 10.0.0.2(56192) streaming 0/7000148

su - postgres
psql

select application_name, state, sync_priority, sync_state from pg_stat_replication;
# 返回 async
select pid,state,client_addr,sync_priority,sync_state from pg_stat_replication;
# 返回 async

从数据库 10.0.0.3

ps aux |grep receiver
# 返回 postgres: walreceiver streaming 0/7000148

本站提供免费和付费的技术支持.你可以通过留言,邮件,TG群的方式来技术交流和免费咨询.同时也可以付费支持的方式获得相关的技术支持,项目部署配置等服务.具体相关详情请点击查看 技术支持页面


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK