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

相关文章

SQL server数据库如何下载和安装

《SQLserver数据库如何下载和安装》本文指导如何下载安装SQLServer2022评估版及SSMS工具,涵盖安装配置、连接字符串设置、C#连接数据库方法和安全注意事项,如混合验证、参数化查... 目录第一步:打开官网下载对应文件第二步:程序安装配置第三部:安装工具SQL Server Manageme

Linux下进程的CPU配置与线程绑定过程

《Linux下进程的CPU配置与线程绑定过程》本文介绍Linux系统中基于进程和线程的CPU配置方法,通过taskset命令和pthread库调整亲和力,将进程/线程绑定到特定CPU核心以优化资源分配... 目录1 基于进程的CPU配置1.1 对CPU亲和力的配置1.2 绑定进程到指定CPU核上运行2 基于

Spring Boot spring-boot-maven-plugin 参数配置详解(最新推荐)

《SpringBootspring-boot-maven-plugin参数配置详解(最新推荐)》文章介绍了SpringBootMaven插件的5个核心目标(repackage、run、start... 目录一 spring-boot-maven-plugin 插件的5个Goals二 应用场景1 重新打包应用

Java中读取YAML文件配置信息常见问题及解决方法

《Java中读取YAML文件配置信息常见问题及解决方法》:本文主要介绍Java中读取YAML文件配置信息常见问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录1 使用Spring Boot的@ConfigurationProperties2. 使用@Valu

Jenkins分布式集群配置方式

《Jenkins分布式集群配置方式》:本文主要介绍Jenkins分布式集群配置方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.安装jenkins2.配置集群总结Jenkins是一个开源项目,它提供了一个容易使用的持续集成系统,并且提供了大量的plugin满

SpringBoot线程池配置使用示例详解

《SpringBoot线程池配置使用示例详解》SpringBoot集成@Async注解,支持线程池参数配置(核心数、队列容量、拒绝策略等)及生命周期管理,结合监控与任务装饰器,提升异步处理效率与系统... 目录一、核心特性二、添加依赖三、参数详解四、配置线程池五、应用实践代码说明拒绝策略(Rejected

SQL Server配置管理器无法打开的四种解决方法

《SQLServer配置管理器无法打开的四种解决方法》本文总结了SQLServer配置管理器无法打开的四种解决方法,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录方法一:桌面图标进入方法二:运行窗口进入检查版本号对照表php方法三:查找文件路径方法四:检查 S

Python中win32包的安装及常见用途介绍

《Python中win32包的安装及常见用途介绍》在Windows环境下,PythonWin32模块通常随Python安装包一起安装,:本文主要介绍Python中win32包的安装及常见用途的相关... 目录前言主要组件安装方法常见用途1. 操作Windows注册表2. 操作Windows服务3. 窗口操作

Linux中SSH服务配置的全面指南

《Linux中SSH服务配置的全面指南》作为网络安全工程师,SSH(SecureShell)服务的安全配置是我们日常工作中不可忽视的重要环节,本文将从基础配置到高级安全加固,全面解析SSH服务的各项参... 目录概述基础配置详解端口与监听设置主机密钥配置认证机制强化禁用密码认证禁止root直接登录实现双因素

嵌入式数据库SQLite 3配置使用讲解

《嵌入式数据库SQLite3配置使用讲解》本文强调嵌入式项目中SQLite3数据库的重要性,因其零配置、轻量级、跨平台及事务处理特性,可保障数据溯源与责任明确,详细讲解安装配置、基础语法及SQLit... 目录0、惨痛教训1、SQLite3环境配置(1)、下载安装SQLite库(2)、解压下载的文件(3)、