Postgres主从(1)Repmgr安装和配置

2024-02-25 04:58

本文主要是介绍Postgres主从(1)Repmgr安装和配置,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

安装

有网安装

安装2ndquadrant的RPM仓库

yum install https://rpm.2ndquadrant.com/site/content/2ndquadrant-repo-10-1-1.el7.noarch.rpm

安装对应PG版本的repmgr,如PostgreSQL10对应repmgr10

yum install repmgr10

无网安装

在有网的服务器上下载RPM包

安装2ndquadrant的RPM仓库

yum install https://rpm.2ndquadrant.com/site/content/2ndquadrant-repo-10-1-1.el7.noarch.rpm

下载RPM包

yum install --downloadonly --downloaddir=/tmp repmgr10

将RPM包拷贝到要安装的服务器上安装

rpm -i repmgr10-*

默认位置

配置文件:/etc/repmgr/10/repmgr.conf
repmgr:位于PostgreSQL的bin下,例如/usr/pgsql-10/bin(必须用postgres用户执行,无法在root下执行)
repmgrd:位于PostgreSQL的bin下,例如/usr/pgsql-10/bin

SSH互信

需要对repmgr集群中的每个节点间配置SSH互信

对于192.168.220.135

su postgres
# 生成秘钥到用户主目录下的.ssh文件夹下
ssh-keygen -t rsa
# 将秘钥拷贝到远程机器
ssh-copy-id -i .ssh/id_rsa.pub postgres@192.168.220.136
# # 验证是否授权完成
ssh 192.168.220.136 date

对于192.168.220.136

su postgres
ssh-keygen -t rsa
ssh-copy-id -i .ssh/id_rsa.pub postgres@192.168.220.135
# 验证能无密码SSH
ssh 192.168.220.135 date

如果SSH仍旧需要密码
可以用ssh -vvv显示Debug信息

ssh -vvv 192.168.220.136 date

常见问题原因

  1. .ssh/authorized_keys权限必须是600(只允许所属用户读写)且属于postgres用户
  2. 开启了SELinux,.ssh目录及其下的文件可能缺少标签(通过ls -laZ查看),/var/log/audit/audit.log将出现如下错误
type=AVC msg=audit(1529070141.437:146): avc:  denied  { read } for  pid=1367 comm="sshd" name="authorized_keys" dev="dm-0" ino=16927777 scontext=system_u:system_r:sshd_t:s0-s0:c0.c1023 tcontext=unconfined_u:object_r:home_root_t:s0 tclass=file

通过对 .ssh文件夹恢复SELinux文件属性解决问题

restorecon -r -vv /home/postgres/.ssh

配置

PostgreSQL配置(主从)

在postgresql.conf文件最后添加

include 'postgresql-replication.conf'

使用postgres用户创建postgresql-replication.conf,以防止postgres缺少权限

su postgres
cd pgdata目录
touch postgresql-replication.conf
vi postgresql-replication.conf

postgresql-replication.conf中设置以下内容

# 打开standby的连接,max_wal_senders至少要大于2,repmgr使用的pg_basebackup需要两个wal sender
max_wal_senders = 10# 启用wal日志记录
# PostgreSQL 9.5及以前可配置为:'hot_standby' 或'logical'
# PostgreSQL 9.6及以后可配置为:'replica'或 'logical'
wal_level = 'replica'# 在standby节点配置为只读,当本节点为primary时。本配置无效,但当primary变为standby时将生效。因此建议primary和standby都设置为on
hot_standby = on# 启用WAL归档
archive_mode = on# 配置将归档文件保存到安全位置的命令,根据需要修改
archive_command = '/bin/true'# If you have configured "pg_basebackup_options"
# in "repmgr.conf" to include the setting "--xlog-method=fetch" (from
# PostgreSQL 10 "--wal-method=fetch"), *and* you have not set
# "restore_command" in "repmgr.conf"to fetch WAL files from another
# source such as Barman, you'll need to set "wal_keep_segments" to a
# high enough value to ensure that all WAL files generated while
# the standby is being cloned are retained until the standby starts up.
#
# wal_keep_segments = 5000

创建数据库用户(主从)

注:shell命令,非SQL

su postgres
createuser -s repmgr 
createdb repmgr -O repmgr
#进入psql
psql
#在psql中设置repmgr依次默认查找repmgr、同用户名、public三个schema
ALTER USER repmgr SET search_path TO repmgr, "$user", public;
#退出psql
\q

pg_hba,conf验证配置(主从)

添加复制用户的权限
假定主从为:192.168.220.135、192.168.220.136

注意:hba文档按配置的先后顺序进行匹配

# replication privilege.
local   replication     repmgr                                  trust
local   repmgr          repmgr                                  trust
host    replication     repmgr          127.0.0.1/32            trust
host    replication     repmgr          192.168.220.135/32      trust
host    replication     repmgr          192.168.220.136/32      trust
host    repmgr          repmgr          127.0.0.1/32            trust
host    repmgr          repmgr          192.168.220.135/32      trust
host    repmgr          repmgr          192.168.220.136/32      trust

检测standby

重启两台服务器PostgreSQL,使上一步的hba配置生效

systemctl restart postgresql-10

在135上测试能否访问136

#使用postgres用户
su postgres
#测试能否使用repmgr访问(无需密码)
psql 'host=192.168.220.136 user=repmgr dbname=repmgr connect_timeout=2'
#退出psql
\q

在136上测试能否访问135

#使用postgres用户
su postgres
#测试
psql 'host=192.168.220.135 user=repmgr dbname=repmgr connect_timeout=2'
#退出psql
\q

主服务器配置

主repmgr配置文件

repmgr10的配置文件默认位于/etc/repmgr/10/repmgr.conf

添加以下内容

#大于0的唯一Integer值
node_id=135
#唯一值,用于标识本服务器
node_name=135pg
#连接到本机的信息
conninfo='host=192.168.220.135 user=repmgr dbname=repmgr connect_timeout=2'
#postgresql的data路径
data_directory='/data/app/pgdata'
注册Primary

注册Primary

su postgres
repmgr -f /etc/repmgr/10/repmgr.conf primary register

查询状态

repmgr -f /etc/repmgr/10/repmgr.conf cluster show

从服务器配置

从repmgr配置文件

repmgr10的配置文件默认位于/etc/repmgr/10/repmgr.conf

添加以下内容

#大于0的唯一Integer值
node_id=136
#唯一值,用于标识本服务器
node_name=136pg
#连接到本机的信息
conninfo='host=192.168.220.136 user=repmgr dbname=repmgr connect_timeout=2'
#postgresql的data路径
data_directory='/data/app/pgdata'
Clone从

目的:将主的数据复制到从

数据库实例必须先关闭

systemctl stop postgresql-10

删除数据库实例data下所有数据

注意不要删除/data/app/pgdata文件夹,否则重新创建该文件夹并给postgres用户授权

rm -rf /data/app/pgdata/*

测试能否使用主服务器进行clone

su postgresrepmgr -h 192.168.220.135 -U repmgr -d repmgr -f /etc/repmgr/10/repmgr.conf standby clone --dry-run

如果未出现Error,执行Clone

repmgr -h 192.168.220.135 -U repmgr -d repmgr -f /etc/repmgr/10/repmgr.conf standby clone --fast-checkpoint

注:pgdat下所有文件如postgresql.conf, postgresql.auto.conf, pg_hba.conf和pg_ident.conf都将被拷贝,可能需要手动修改其中的配置

修改完PostgreSQL的配置文件后,启动服务

pg_ctl -D /data/app/pgdata start
检查复制状态

主服务器

SELECT * FROM pg_stat_replication;

从服务器

SELECT * FROM pg_stat_wal_receiver
注册从
repmgr -f /etc/repmgr/10/repmgr.conf  standby register

在从上查询状态

repmgr -f /etc/repmgr/10/repmgr.conf cluster show

应看到类似如下信息

 ID | Name  | Role    | Status    | Upstream | Location | Connection string                                               
----+-------+---------+-----------+----------+----------+------------------------------------------------------------------135 | 135pg | primary | * running |          | default  | host=192.168.220.135 user=repmgr dbname=repmgr connect_timeout=2136 | 136pg | standby |   running | 135pg    | default  | host=192.168.220.136 user=repmgr dbname=repmgr connect_timeout=2

以上则完成了一个集群的配置,为了能够在主故障时自动切换,需要使用repmgrd

Repmgrd

postgresql配置

postgresql.conf中设置加载repmgrd库

shared_preload_libraries ='repmgr'

重启数据库使配置生效

systemctl restart postgresql-10

repmgrd配置

配置/etc/repmgr/10/repmgr.conf

# 设置为自动恢复模式
failover=automatic
# 尝试连接到主的次数
reconnect_attempts=6
# 每次尝试连接到主的时间间隔(秒)
reconnect_interval=5
# 当本机要成为主时,执行的提升命令
promote_command='repmgr standby promote -f /etc/repmgr/10/repmgr.conf --log-to-file'
# 当出现新主时,执行的跟随命令
follow_command='repmgr standby follow -f /etc/repmgr/10/repmgr.conf --log-to-file --upstream-node-id=%n'

日志轮转配置

配置/etc/repmgr/10/repmgr.conf设置日志路径

log_file='/var/log/repmgr/repmgr.log'	

修改/etc/logrotate.conf
添加以下内容

/var/log/repmgr/repmgr.log {missingokcompressrotate 30dailydateextcreate 0600 postgres postgres
}

启动repmgrd

su postgres
repmgrd -f /etc/repmgr/10/repmgr.conf --pid-file /tmp/repmgrd.pid --daemonize

停止repmgrd

kill `cat /tmp/repmgrd.pid`

这篇关于Postgres主从(1)Repmgr安装和配置的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/744453

相关文章

Nginx 重写与重定向配置方法

《Nginx重写与重定向配置方法》Nginx重写与重定向区别:重写修改路径(客户端无感知),重定向跳转新URL(客户端感知),try_files检查文件/目录存在性,return301直接返回永久重... 目录一.try_files指令二.return指令三.rewrite指令区分重写与重定向重写: 请求

Nginx 配置跨域的实现及常见问题解决

《Nginx配置跨域的实现及常见问题解决》本文主要介绍了Nginx配置跨域的实现及常见问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来... 目录1. 跨域1.1 同源策略1.2 跨域资源共享(CORS)2. Nginx 配置跨域的场景2.1

gitlab安装及邮箱配置和常用使用方式

《gitlab安装及邮箱配置和常用使用方式》:本文主要介绍gitlab安装及邮箱配置和常用使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.安装GitLab2.配置GitLab邮件服务3.GitLab的账号注册邮箱验证及其分组4.gitlab分支和标签的

MySQL MCP 服务器安装配置最佳实践

《MySQLMCP服务器安装配置最佳实践》本文介绍MySQLMCP服务器的安装配置方法,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下... 目录mysql MCP 服务器安装配置指南简介功能特点安装方法数据库配置使用MCP Inspector进行调试开发指

在Windows上使用qemu安装ubuntu24.04服务器的详细指南

《在Windows上使用qemu安装ubuntu24.04服务器的详细指南》本文介绍了在Windows上使用QEMU安装Ubuntu24.04的全流程:安装QEMU、准备ISO镜像、创建虚拟磁盘、配置... 目录1. 安装QEMU环境2. 准备Ubuntu 24.04镜像3. 启动QEMU安装Ubuntu4

Python UV安装、升级、卸载详细步骤记录

《PythonUV安装、升级、卸载详细步骤记录》:本文主要介绍PythonUV安装、升级、卸载的详细步骤,uv是Astral推出的下一代Python包与项目管理器,主打单一可执行文件、极致性能... 目录安装检查升级设置自动补全卸载UV 命令总结 官方文档详见:https://docs.astral.sh/

Redis Cluster模式配置

《RedisCluster模式配置》:本文主要介绍RedisCluster模式配置,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录分片 一、分片的本质与核心价值二、分片实现方案对比 ‌三、分片算法详解1. ‌范围分片(顺序分片)‌2. ‌哈希分片3. ‌虚

SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志

《SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志》在SpringBoot项目中,使用logback-spring.xml配置屏蔽特定路径的日志有两种常用方式,文中的... 目录方案一:基础配置(直接关闭目标路径日志)方案二:结合 Spring Profile 按环境屏蔽关

Maven 配置中的 <mirror>绕过 HTTP 阻断机制的方法

《Maven配置中的<mirror>绕过HTTP阻断机制的方法》:本文主要介绍Maven配置中的<mirror>绕过HTTP阻断机制的方法,本文给大家分享问题原因及解决方案,感兴趣的朋友一... 目录一、问题场景:升级 Maven 后构建失败二、解决方案:通过 <mirror> 配置覆盖默认行为1. 配置示

Nexus安装和启动的实现教程

《Nexus安装和启动的实现教程》:本文主要介绍Nexus安装和启动的实现教程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、Nexus下载二、Nexus安装和启动三、关闭Nexus总结一、Nexus下载官方下载链接:DownloadWindows系统根