repmgr安装及常用运维指令
简介
repmgr 由 EDB 与其他个人和组织的贡献一起开发,安装部署相对较为简单
安装
repmgr官网上传对应的安装到服务器上
安装前/etc/hosts IP映射、始终同步、免密通信本文忽略
repmgr的安装相对较为简单,目前repmgr-5仅仅支持到postgresql-15
postgresql必要参数配置
shared_preload_libraries = 'repmgr'
wal_log_hints = on
synchronous_standby_names = '*'
archive_mode = on
archive_command = 'test ! -f /home/postgres/archivedir/%f && cp %p /home/postgres/archivedir/%f'
安装必要依赖
yum install -y flex libselinux-devel libxml2-devel libxslt-devel openssl-devel pam-devel readline-devel
# 或使用以下指令sudo yum check-update -ysudo yum groupinstall "Development Tools" -ysudo yum install yum-utils openjade docbook-dtds docbook-style-dsssl docbook-style-xsl -ysudo yum-builddep postgresql96 -y
# 安装repmgr
tar -zxvf repmgr-5.4.1.tar.gz
cd repmgr-5.4.1/
./configure && make install
# 数据库创建repmgr数据库createuser -s repmgrcreatedb repmgr -O repmgr
# 修改repmgr用户的seach_path
ALTER USER repmgr SET search_path TO repmgr, "$user", public;# 配置repmgr配置项目node_id=134
node_name='vm134'
conninfo='host=vm134 user=repmgr dbname=repmgr'
data_directory='/home/postgres/pg/data'
replication_user='repmgr'
use_replication_slots=true
pg_bindir='/home/postgres/pg/bin'
ssh_options='-q -o ConnectTimeout=10' ## 测试链接参数是否正常 使用
psql 'host=vm134 user=repmgr dbname=repmgr connect_timeout=2' 链接测试
postgresql白名单配置
repmgr需要设置trust 登录
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host repmgr repmgr 10.0.0.136/32 trust
host repmgr repmgr 10.0.0.135/32 trust
host repmgr repmgr 10.0.0.134/32 trust
host all all 0.0.0.0/0 scram-sha-256
# IPv6 local connections:
host all all ::1/128 scram-sha-256
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all trust
host replication repmgr 10.0.0.136/32 trust
host replication repmgr 10.0.0.135/32 trust
host replication repmgr 10.0.0.134/32 trust
host replication all 0.0.0.0/0 scram-sha-256
host replication repmgr ::1/128 scram-sha-256
创建postgresql守护进程
## sudo vim /etc/systemd/system/postgresql.service
[Unit]
Description=PostgreSQL database server
After=network.target[Service]
Type=forking
User=postgres
Group=postgres# 设置环境变量(可选,根据需要修改)
Environment=PGDATA=/home/postgres/pg/data
ExecStart=/home/postgres/pg/bin/pg_ctl start -D ${PGDATA}
ExecStop=/home/postgres/pg/bin/pg_ctl stop -D ${PGDATA}
ExecReload=/home/postgres/pg/bin/pg_ctl reload -D ${PGDATA}# 重启策略
Restart=on-failure
RestartSec=5s[Install]
WantedBy=multi-user.target
注册主节点
repmgr -f repmgr.conf primary register
# 查看集群状态
repmgr -f repmgr.conf cluster show
注册克隆节点
repmgr -h vm134 -U repmgr -d repmgr -f repmgr.conf standby clone --dry-runrepmgr -h vm134 -U repmgr -d repmgr -f repmgr.conf standby clone## 启动数据库
pg_ctl start
#进行注册
repmgr -f repmgr.conf standby register
查看集群状态
repmgr -f repmgr.conf cluster show
修改配置文件路径
将repmgr.conf文件移动到以下路径,可以避免每一次指令添加-f的繁琐
[root@vm134 ~]# pg_config --sysconfdir
/home/postgres/pg/etc
常用指令
查看集群状态
repmgr cluster show
克隆节点(也可做备份)
repmgr -h vm135 -U repmgr -d repmgr standby clone
移除集群节点
repmgr standby unregister --force --node-id=136
加入集群
repmgr standby register --force ## 以备库身份加入
repmgr witness register ## 以见证者身份加入
repmgr primary register ## 以主库身份加入
手工主从切换
当前主库停库
pg_ctl stop 新主库进行升主
repmgr standby promote其余从库follow新主库repmgr standby follow 旧主库rewind追日志
repmgr node rejoin -d 'host=10.0.0.134 dbname=repmgr user=repmgr ' --force-rewindrewind 执行失败的时候使用以下指令组合,将旧的主节点注销重新以备节点身份加入移除节点
repmgr primary unregister --force --node-id=136
重新克隆
repmgr -h vm135 -U repmgr -d repmgr standby clone
重新加入repmgr standby register --force
使用pg_rewind 的时候可以使用pg_checksums -e 打开–data-checksums功能
[postgres@vm134 ~]$ pg_checksums -e
Checksum operation completed
Files scanned: 1254
Blocks scanned: 3834
Files written: 1035
Blocks written: 3834
pg_checksums: syncing data directory
pg_checksums: updating control file
Checksums enabled in cluster
配置autofailover
配置repmgr守护进程
#vim /usr/lib/systemd/system/repmgr.service[Unit]
Description=A replication manager, and failover management tool for PostgreSQL
After=syslog.target
After=network.target
[Service]
Type=forking
User=postgres
Group=postgres
# PID file
PIDFile=/home/postgres/pg/data/repmgrd.pid
# Location of repmgr conf file:
Environment=REPMGRDCONF=/home/postgres/pg/etc/repmgr.conf
Environment=PIDFILE=/home/postgres/pg/data/repmgrd.pid
# Where to send early-startup messages from the server
# This is normally controlled by the global default set by systemd
# StandardOutput=syslog
ExecStart=/home/postgres/pg/bin/repmgrd -f ${REPMGRDCONF} --pid-file ${PIDFILE} -d --verbose
ExecStop=/usr/bin/kill -TERM $MAINPID
ExecReload=/usr/bin/kill -HUP $MAINPID
# Give a reasonable amount of time for the server to start up/shut down
TimeoutSec=300
[Install]
WantedBy=multi-user.target
修改配置文件内容
node_id=136 ## 每个主机唯一整数
node_name='vm136' ## 每个主机唯一字符
conninfo='host=vm136 user=repmgr dbname=repmgr connect_timeout=2 password=repmgr' ## 链接到本地数据库
data_directory='/home/postgres/pg/data'
config_directory='/home/postgres/pg/data/postgresql.conf'
replication_user='repmgr'
replication_type='physical'
location='ShangHai' ## 此参数与主库同名的会优先升主,优于priority
use_replication_slots=true
log_level='INFO'
log_facility='STDERR'
log_file='/home/postgres/repmgrd.log'
pg_bindir='/home/postgres/pg/bin'
ssh_options='-q -o ConnectTimeout=10'
restore_command='cp /home/postgres/archivedir/%f %p'
shutdown_check_timeout=3
standby_reconnect_timeout=3
wal_receive_check_timeout=3
node_rejoin_timeout=3
failover='automatic'
priority=100 ## 升主的优先等级值越高,优先级别越高
connection_check_type='query'
reconnect_attempts=3
reconnect_interval=3
promote_command='/home/postgres/pg/bin/repmgr standby promote -f /home/postgres/pg/etc/repmgr.conf --log-to-file && /home/postgres/pg/etc/repmgr_manage_vip.sh add'
follow_command='/home/postgres/pg/bin/repmgr standby follow -f home/postgres/pg/etc/repmgr.conf --upstream-node-id=%n '
monitoring_history=true
monitor_interval_secs=2
degraded_monitoring_timeout=-1
standby_disconnect_on_failover=true
sibling_nodes_disconnect_timeout=3
primary_visibility_consensus=false
always_promote=true
failover_validation_command='/home/postgres/pg/etc/repmgr_manage_vip.sh del'
election_rerun_interval=3
service_start_command = 'sudo systemctl start postgresql'
service_stop_command = 'sudo systemctl stop postgresql'
service_restart_command = 'sudo systemctl restart postgresql'
service_reload_command = 'sudo systemctl reload postgresql'
repmgrd_service_start_command = 'sudo systemctl start repmgr.service'
repmgrd_service_stop_command = 'sudo systemctl stop repmgr.service'
新增VIP管理脚本repmgr_manage_vip.sh
#!/bin/bashVIP="10.0.0.133"
NETMASK="24"
INTERFACE="ens33"
REMOTE_USER="postgres"
LOG_FILE="/home/postgres/repmgrd.log"# 检查本地是否已绑定 VIP
function is_vip_on_local() {ip addr show dev "$INTERFACE" | grep -q "$VIP/$NETMASK"return $? # 返回 0 表示已绑定,非零表示未绑定
}# 检查 VIP 是否可达
function is_vip_pingable() {ping -c 1 -W 1 "$VIP" &>/dev/nullreturn $? # 返回 0 表示可达,非零表示不可达
}# 删除远程主机上的 VIP
function remove_vip_remote() {local remote_host="$1"for i in {1..3}; do# 通过 SSH 在远程主机删除 VIPssh "$REMOTE_USER@$remote_host" "sudo ip addr del '$VIP/$NETMASK' dev '$INTERFACE'" && {echo "$(date '+%Y-%m-%d %H:%M:%S') - VIP $VIP 从 $remote_host 上移除成功" >> "$LOG_FILE"return 0}echo "$(date '+%Y-%m-%d %H:%M:%S') - 尝试从 $remote_host 移除 VIP 失败,重试中..." >> "$LOG_FILE"sleep 1doneecho "$(date '+%Y-%m-%d %H:%M:%S') - VIP $VIP 从 $remote_host 上移除失败" >> "$LOG_FILE"return 1
}# 注册 VIP 到本地
function add_vip_local() {sudo ip addr add "$VIP/$NETMASK" dev "$INTERFACE"if [ $? -eq 0 ]; thenecho "$(date '+%Y-%m-%d %H:%M:%S') - VIP $VIP 成功注册到本地 $INTERFACE." >> "$LOG_FILE"return 0elseecho "$(date '+%Y-%m-%d %H:%M:%S') - VIP $VIP 注册到本地失败." >> "$LOG_FILE"return 1fi
}# 主逻辑
case "$1" inadd)if is_vip_on_local; then# 如果 VIP 已经绑定到本地,则什么也不做,返回状态 0echo "VIP $VIP 已经注册在本地主机,无需重复操作."exit 0else# 如果 VIP 不在本地,尝试远程删除并本地注册if is_vip_pingable; thenremote_host=$(ping -c 1 "$VIP" | grep -oP '\d+\.\d+\.\d+\.\d+')remove_vip_remote "$remote_host" && add_vip_localelse# 如果 VIP 不可达,直接在本地注册add_vip_localfiexit $? # 返回 add_vip_local 或 remove_vip_remote 的退出状态fi;;del)if is_vip_on_local; then# 如果 VIP 在本地,什么也不做,返回状态 0echo "VIP $VIP 已经绑定到本地主机,无需删除."exit 0else# 如果 VIP 不在本地,尝试远程删除if is_vip_pingable; thenremote_host=$(ping -c 1 "$VIP" | grep -oP '\d+\.\d+\.\d+\.\d+')remove_vip_remote "$remote_host"elseecho "VIP $VIP 不可达,无法删除."exit 1fiexit $? # 返回 remove_vip_remote 的退出状态fi;;*)echo "无效的操作参数。请使用 'add' 或 'del' 参数。"exit 1;;
esac
为postgres配置sudo免密权限
postgres ALL=(ALL) NOPASSWD: /bin/systemctl * postgresql, \/bin/systemctl * repmgr, \
/usr/sbin/ip addr * \
配置相关路径
创建归档路径
mkdir -p /home/postgres/archiver/
使用指令调试路径是否正确
repmgrd -f /home/postgres/pg/etc/repmgr.conf --pid-file /home/postgres/pg/data/repmgrd.pid --verbose查看日志 启动正常时,使用守护进程启动。
在repmgr安装完成后会创建一个repmgr、repmgrd两个工具,repmgr主要是对集群的管理工具,repmgrd主要是管理进程管理工具。
相关运维指令
单节点指定指令
查看集群状态
repmgr service status
repmgr cluster show
暂停服务
repmgr service pause
#此时集群不会发生故障切换,流传输仍然保持正常查看暂停状态,此时的Paused并不会马上展示出来
repmgr service status repmgr service unpause
阶段运行状态检查
repmgr node check repmgr node status
## 输出信息可用于故障排查的
执行checkpoint
repmgr node service --action=restart --checkpoint
## action 可以是"start", "stop", "restart" or "reload",避免停服导致的数据丢失
停止,启动repmgr
repmgr daemon stop repmgr daemon start
## 调用参数配置的repmgrd_service_stop_command 、repmgrd_service_stop_command 指令。默认情况下是停留15秒之后才会执行参数的指令 repmgr daemon stop --no-wait repmgr daemon start --no-wait
使用--no-wait 会立即停止或者启动
启停postgresql
重启postgresql,要求配置service_restart_command
repmgr node service --action=restart
启动postgresql,要求配置service_start_command
repmgr node service --action=start
停止postgresql,要求配置service_stop_command
repmgr node service --action=stop
加载postgresql,要求配置service_reload_command
repmgr node service --action=reload指令后缀增加--list-actions参数,可以查看对应指令指向的配置参数值repmgr node service --action=restart --list-actions
repmgr node service --list-actions
备库升主
## 备库升主有以下两个指令,不推荐使用promote ,执行切换前使用 --dry-run进行预检查,执行切换都需要停掉当前的主库
repmgr standby promote --dry-run
等价于repmgr node service --action=promote --dry-run
使用node service的时候 增加--list-actions --dry-run 预先查看参数配置是否齐全##promote 需要预先停掉当前主库,使用repmgr node service --action=stop --checkpoint ,升主成功之后,使用repmgr node rejoin -d'host=vm136 user=repmgr dbname=repmgr' 在此加入到新的主库中去。
切换后的状态
使用switchover 进行切换
repmgr standby switchover --siblings-follow --force-rewind --dry-run
去掉dry-run 之后正式执行切换。
使用–siblings-follow 参数会自动将其余备库更新follow新的主库
更新配置信息表
repmgr standby register --force ## 强制再次注册才会更新配置文件路径
## 该库下会存放着集群的监控信息以及元数据信息。该库信息不可轻易变动
\c repmgr repmgr
相关文章:

repmgr安装及常用运维指令
简介 repmgr 由 EDB 与其他个人和组织的贡献一起开发,安装部署相对较为简单 安装 repmgr官网上传对应的安装到服务器上 安装前/etc/hosts IP映射、始终同步、免密通信本文忽略 repmgr的安装相对较为简单,目前repmgr-5仅仅支持到postgresql-15 postgresql必要参数…...

RedHat系统配置静态IP
1、执行nmtui命令进入字符配置界面如下图所示 2、选择编辑连接进入 3、选择编辑进入后,将IPv4设置为手动模式后,选择显示后进行ip地址、网关、DNS的配置,配置完成后选择确定退出编辑 4、进入主界面后选择启用连接进入后,选择启用&…...
nvm和nrm的安装与使用
NVM相关请跳转: Node版本管理器nvm的安装与使用 nrm 的安装与使用 nrm(NPM Registry Manager)是一个用于管理和切换 NPM 源的工具。它允许你在多个 NPM 源之间快速切换,以提高包管理的速度和效率。以下是 nrm 的安装和使用方法&…...

10大核心应用场景,解锁AI检测系统的智能安全之道
随着工业化和自动化的快速推进,高风险作业场景的安全管理需求日益增加。思通数科AI检测系统以深度学习、计算机视觉和多模态数据融合技术为基础,通过智能化监控和实时反馈,为企业提供全面的作业安全和流程管理解决方案。本文将详细解读该系统…...

香豆烤馍:传统美食中的烟火记忆
食家巷香豆烤馍,承载着甘肃人的乡愁与记忆。它那朴实的外表下,蕴含着丰富的口感和深厚的文化底蕴。烤馍的制作过程充满了烟火气息。选用优质的面粉,经过发酵、揉制等多道工序,再放入传统的烤炉中慢慢烘烤。这个过程需要经验丰富的…...
金融量化交易模型的探索与发展
随着全球金融市场的不断变化与技术进步,量化交易逐渐成为机构和个人投资者的重要选择。作为数据驱动的交易方式,量化交易通过科学建模和技术手段,有效提升了交易效率与决策精准度。本文将探讨金融量化交易模型的创新探索与未来发展方向。 量化…...
灾难恢复计划 (DRP)
灾难恢复计划 (DRP) 目录 灾难恢复计划 (DRP) 1 1. 简介 2 2. 目的 2 3. 范围 3 4. 风险评估 3 5. 容灾方案 3 6. 关键系统恢复优先级 4 7. 恢复流程 4 8. 测试与维护 5 9. 联系信息 5 10. 批准与分发 5 11. 附录 5 1. 简介 灾难恢复计…...
Makefile 之 wordlist
wordlist $(wordlist <s>,<e>,<text> ) 名称:取单词串函数——wordlist。 功能:从字符串<text>中取从<s>开始到<e>的单词串。<s>和<e>是一个数字。 返回:返回字符串<text>中从…...
半导体工艺与制造篇1 绪论
我们为什么要研究半导体?半导体凭什么可以成为电子信息行业的基础呢? 这就要说到半导体的一个重要特点:可以通过控制掺杂率来控制它的导电性 集成电路IC的生产 集成电路IC的生产包括: #mermaid-svg-rWB59zU4pI2cGloo {font-fami…...

接雨水
接雨水 1、 题目描述2、解题思路 1、 题目描述 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。 2、解题思路 本题使用了双指针,根据下图可以得出,下标 i 处能接的雨水量由左边…...

Python蓝桥杯刷题1
1.确定字符串是否包含唯一字符 题解:调用count函数计算每一个字符出现的次数,如果不等于1就输出no,并且结束循环,如果等于1就一直循环直到计算到最后一个字符,若最后一个字符也满足条件,则输出yes import…...
实习冲刺第二十七天
3.无重复字符的最长字串 给定一个字符串 s ,请你找出其中不含有重复字符的 最长 子串 的长度。 示例 1: 输入: s "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。示例 2: 输入: s "bbbb…...

el-table-column自动生成序号在序号前插入图标
实现效果: 代码如下: 在el-table里加入这个就可以了,需要拿到值可以用scope.$index <el-table-column type"index" label"序号" show-overflow-tooltip"true" min-width"40">…...

前端工程化-node/npm/babel/polyfill/webpack 一文速通
文章主要介绍了前端工程化的相关内容,包括 Node 环境、npm 包管理器及其命令、配置和镜像,package.json 文件,babel 和 polyfill 用于解决 JavaScript 兼容性问题,以及 webpack 这一前端构建工具的作用、核心概念、构建流程、安装…...
Spring Security PasswordEncoder接口(密码编码)
密码编码(通常称为哈希)是一种安全措施,它将明文密码转换为独特的字符字符串。 主要目的是确保即使数据存储遭到破坏,存储的密码也不会轻易被破解。 与加密不同,哈希是一个单向过程——这意味着无法从哈希值中恢复原…...

C# 数据结构之【树】C#树
以二叉树为例进行演示。二叉树每个节点最多有两个子节点。 1. 新建二叉树节点模型 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace DataStructure {class TreeNode{public int Data { get;…...
树莓派2装FreeBSD14.1 Raspberry Pi2 install FreeBSD14.1 00000121:error:0A000086:SSL
树莓派2代的Model B采用Broadcom BCM2836 900MHz的四核SoC,1GB内存,是新一代开拓者,兼容1代B。相比之下,树莓派2的性能比1代提升6倍,内存翻了一番。Raspberry Pi 2不仅能跑全系列ARM GNU/Linux发行版,而且支…...

探索C/C++的奥秘之stack和queue
1. stack的介绍和使用 1.1 stack的介绍 1. stack是一种容器适配器,专门用在具有后进先出操作的上下文环境中,其删除只能从容器的一端进行元素的插入与提取操作。具体什么是适配器呢?其实就是由现有的东西进行转换,转化出我要的东…...

[开源]1.2K star!中后台方向的低代码可视化平台,超赞!
大家好,我是JavaCodexPro! “时间就是金钱,效率就是生命”,快速搭建高质量中后台的低代码可视化搭建平台尤为重要! 今天JavaCodexPro给大家分享一款超赞的低代码可视化搭建平台 - Marsview ,旨在简化开发…...

算法编程题-排序
算法编程题-排序 比较型排序算法冒泡排序选择排序插入排序希尔排序堆排序快速排序归并排序 非比较型排序算法计数排序基数排序 本文将对七中经典比较型排序算法进行介绍,并且给出golang语言的实现,还包括基数排序、计数排序等非比较型的算法的介绍和实现…...

Linux 文件类型,目录与路径,文件与目录管理
文件类型 后面的字符表示文件类型标志 普通文件:-(纯文本文件,二进制文件,数据格式文件) 如文本文件、图片、程序文件等。 目录文件:d(directory) 用来存放其他文件或子目录。 设备…...

基于FPGA的PID算法学习———实现PID比例控制算法
基于FPGA的PID算法学习 前言一、PID算法分析二、PID仿真分析1. PID代码2.PI代码3.P代码4.顶层5.测试文件6.仿真波形 总结 前言 学习内容:参考网站: PID算法控制 PID即:Proportional(比例)、Integral(积分&…...

安宝特方案丨XRSOP人员作业标准化管理平台:AR智慧点检验收套件
在选煤厂、化工厂、钢铁厂等过程生产型企业,其生产设备的运行效率和非计划停机对工业制造效益有较大影响。 随着企业自动化和智能化建设的推进,需提前预防假检、错检、漏检,推动智慧生产运维系统数据的流动和现场赋能应用。同时,…...
【SpringBoot】100、SpringBoot中使用自定义注解+AOP实现参数自动解密
在实际项目中,用户注册、登录、修改密码等操作,都涉及到参数传输安全问题。所以我们需要在前端对账户、密码等敏感信息加密传输,在后端接收到数据后能自动解密。 1、引入依赖 <dependency><groupId>org.springframework.boot</groupId><artifactId...

聊聊 Pulsar:Producer 源码解析
一、前言 Apache Pulsar 是一个企业级的开源分布式消息传递平台,以其高性能、可扩展性和存储计算分离架构在消息队列和流处理领域独树一帜。在 Pulsar 的核心架构中,Producer(生产者) 是连接客户端应用与消息队列的第一步。生产者…...
oracle与MySQL数据库之间数据同步的技术要点
Oracle与MySQL数据库之间的数据同步是一个涉及多个技术要点的复杂任务。由于Oracle和MySQL的架构差异,它们的数据同步要求既要保持数据的准确性和一致性,又要处理好性能问题。以下是一些主要的技术要点: 数据结构差异 数据类型差异ÿ…...
C++.OpenGL (10/64)基础光照(Basic Lighting)
基础光照(Basic Lighting) 冯氏光照模型(Phong Lighting Model) #mermaid-svg-GLdskXwWINxNGHso {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-GLdskXwWINxNGHso .error-icon{fill:#552222;}#mermaid-svg-GLd…...
MySQL用户和授权
开放MySQL白名单 可以通过iptables-save命令确认对应客户端ip是否可以访问MySQL服务: test: # iptables-save | grep 3306 -A mp_srv_whitelist -s 172.16.14.102/32 -p tcp -m tcp --dport 3306 -j ACCEPT -A mp_srv_whitelist -s 172.16.4.16/32 -p tcp -m tcp -…...
力扣-35.搜索插入位置
题目描述 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。 请必须使用时间复杂度为 O(log n) 的算法。 class Solution {public int searchInsert(int[] nums, …...

中医有效性探讨
文章目录 西医是如何发展到以生物化学为药理基础的现代医学?传统医学奠基期(远古 - 17 世纪)近代医学转型期(17 世纪 - 19 世纪末)现代医学成熟期(20世纪至今) 中医的源远流长和一脉相承远古至…...