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

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
P
Privacy International News Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Troy Hunt's Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
Attack and Defense Labs
Attack and Defense Labs
S
Secure Thoughts
V2EX - 技术
V2EX - 技术
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
O
OpenAI News
Cloudbric
Cloudbric
Google Online Security Blog
Google Online Security Blog
Schneier on Security
Schneier on Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Help Net Security
Help Net Security
Cyberwarzone
Cyberwarzone
G
GRAHAM CLULEY
L
Lohrmann on Cybersecurity
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Spread Privacy
Spread Privacy
NISL@THU
NISL@THU
N
News and Events Feed by Topic
T
Tenable Blog
S
Security @ Cisco Blogs
N
News and Events Feed by Topic
The Hacker News
The Hacker News
C
CXSECURITY Database RSS Feed - CXSecurity.com
宝玉的分享
宝玉的分享
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell
美团技术团队
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google DeepMind News
Google DeepMind News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tailwind CSS Blog
V
Visual Studio Blog
P
Proofpoint News Feed
Webroot Blog
Webroot Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 三生石上(FineUI控件)
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Jina AI
Jina AI
雷峰网
雷峰网
T
The Blog of Author Tim Ferriss
Hugging Face - Blog
Hugging Face - Blog
腾讯CDC
L
LangChain Blog
The Register - Security
The Register - Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东

思有云 - 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 刀。

赞赏作者

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