惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Secure Thoughts
Security Latest
Security Latest
Simon Willison's Weblog
Simon Willison's Weblog
O
OpenAI News
GbyAI
GbyAI
L
LINUX DO - 最新话题
A
Arctic Wolf
T
Tor Project blog
G
GRAHAM CLULEY
I
InfoQ
博客园_首页
IT之家
IT之家
The Register - Security
The Register - Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
The GitHub Blog
The GitHub Blog
Blog — PlanetScale
Blog — PlanetScale
N
Netflix TechBlog - Medium
K
Kaspersky official blog
博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题
U
Unit 42
PCI Perspectives
PCI Perspectives
量子位
P
Palo Alto Networks Blog
S
Securelist
T
Troy Hunt's Blog
博客园 - 【当耐特】
Recorded Future
Recorded Future
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Security Affairs
Engineering at Meta
Engineering at Meta
T
The Blog of Author Tim Ferriss
博客园 - 聂微东
罗磊的独立博客
N
News and Events Feed by Topic
人人都是产品经理
人人都是产品经理
B
Blog RSS Feed
NISL@THU
NISL@THU
C
Cisco Blogs
T
Threatpost
有赞技术团队
有赞技术团队
Forbes - Security
Forbes - Security
Hugging Face - Blog
Hugging Face - Blog
Last Week in AI
Last Week in AI
T
The Exploit Database - CXSecurity.com
Cloudbric
Cloudbric
Cyberwarzone
Cyberwarzone
Google DeepMind News
Google DeepMind News
C
Cyber Attacks, Cyber Crime and Cyber Security

思有云 - IOIOX - 运维部署

甲骨文云 Oracle Cloud 服务器防回收保活 docker 命令 - 思有云 甲骨文云 Oracle Cloud 引导卷扩展和块存储卷挂载 - 思有云 甲骨文云 Oracle Cloud 解决 ARM 服务器安装军哥 LNMP 环境的几个问题 - 思有云 drone ci 工作流语法变量高级使用教程 - 思有云 部署 MinIO 通用 S3 协议对象存储服务当网盘和图床使用 - 思有云 Bark 推送服务的部署和使用小技巧 - 思有云 - IOIOX 分享三种常用的 drone 工作流 CI 配置 - 思有云 Docker 安装 Gitea + Drone 开源代码仓库及 CI/CD 教程 - 思有云 Docker Compose 部署监控系统 Prometheus + Grafana + Node Exporter + Cadvisor - 思有云
PostgreSql 14 主从负载均衡部署记录 - 思有云
博主: Stille · 2022-04-27 · via 思有云 - IOIOX - 运维部署

前言

前些天给个环境部署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

晚高峰稳定 4K 的 IPLC 机场 解锁各流媒体 支持 ChatGPT. 晚高峰稳定 4K 的 IPLC 机场 解锁各流媒体 支持 ChatGPT. RedteaGO - 最划算的大陆漫游 eSim 流量卡,原生境外 IP,注册就送 3 刀。
RedteaGO - 最划算的大陆漫游 eSim 流量卡,原生境外 IP,注册就送 3 刀。

赞赏作者

如果喜欢我的文章,觉得对你有帮助,请随意赞赏!