Postgres主从(2)Repmgr基本命令

命令简介

命令说明
repmgr primary register安装pg的repmgr扩展并注册为主节点
repmgr primary unregister注销不活动的主节点
repmgr standby clone从其他节点复制数据到从节点
repmgr standby register注册从节点(添加从的信息到repmgr元数据)
repmgr standby unregisterrepmgr元数据中移除从的信息
repmgr standby promote将从提升为主
repmgr standby follow将从跟随新主
repmgr standby switchover将从提升为主并将主降级为从
repmgr witness register注册一个观察节点
repmgr witness unregister移除一个观察节点
repmgr node status显示节点的基本信息和复制状态
repmgr node check从复制的角度对节点进行健康监测
repmgr node rejoin重新加入一个失效节点到集群
repmgr cluster show显示所有集群中注册的节点信息
repmgr cluster matrix在所有节点运行show并汇总
repmgr cluster crosscheck在节点间两两交叉监测连接
repmgr cluster event输出时间记录
repmgr cluster cleanup清理监控历史

repmgr primary register注册主

说明

在集群中注册一个主节点,并配置PostgreSQL安装repmgr扩展,必须在所有从节点注册前注册

参数

  • -f:指定配置文件
  • –dry-run:测试命令执行条件是否满足,但不实际执行

将触发primary_register事件

repmgr primary unregister注销主

说明

注销非活动状态的主,典型场景是当主宕机,新主已产生时用本命令注销老主

参数

  • –node-id:要注销的主的nodeId
  • –dry-run:测试命令执行条件是否满足,但不实际执行

将触发primary_unregister 事件

repmgr standby clone克隆数据到从

说明

从其他节点(一般是主节点)克隆数据,并自动创建recovery.conf文件

克隆后并未启动从,需要先用repmgr standby register注册从,然后再启动数据库

PGDATA中postgresql.conf, postgresql.auto.conf, pg_hba.conf和pg_ident.conf等所有文件都将被复制,需要修改配置文件

自动创建recovery.conf

自动创建的以下配置

  • standby_mode:值总是on
  • recovery_target_timeline:值总是’latest’
  • primary_conninfo
  • primary_slot_name (如果使用复制槽)

如果repmgr.conf下配置了以下参数也将被复制到recovery.conf

  • restore_command
  • archive_cleanup_command
  • recovery_min_apply_delay

参数

  • –dry-run:测试命令执行条件是否满足,但不实际执行
  • -c, --fast-checkpoint:强制fast checkpoint
  • 其他参数见https://repmgr.org/docs/repmgr.html#REPMGR-STANDBY-CLONE

将触发standby_clone 事件

repmgr standby register 注册从

参数

  • –upstream-node-id:可选,复制源的NodeID
  • –dry-run:测试命令执行条件是否满足,但不实际执行

将触发standby_register 事件

repmgr standby unregister 注销从

并不会影响复制,只将元数据中删除从信息

参数

  • –node-id:可选,要注销的NodeId

将触发standby_unregister 事件

repmgr standby promote 提升从

说明

当主宕机时将从提升为主

执行成功成为主后,不需要重启PG。其他的从需要跟随新主(使用repmgr standby follow)

示例

$ repmgr -f /etc/repmgr/10/repmgr.conf standby promote

将触发standby_promote 事件

repmgr standby follow从跟随新主

示例

测试

repmgr -f /etc/repmgr.conf standby follow --dry-run

执行操作

repmgr -f /etc/repmgr.conf standby follow

将触发standby_follow 事件

事件参数

  • %p:新主的NodeID
  • %c:conninfo字符串
  • %a:本节点的node name

repmgr standby switchover主从切换

说明

提升从为主,并将主降级为从

需要主和从间可进行无密码的SSH连接

需要任何节点的repmgrd处于关闭状态

参数

  • –always-promote:即使主从不一致也将执行提升(原来的主将被关闭)
  • –dry-run:检查
  • –siblings-follow:让其他从跟随新主
  • –force-rewind[=/path/to/pg_rewind]:当主从不一致时使用pg_rewind重新同步主从

repmgr.conf

  • reconnect_attempts: 在原主执行关闭后进行完全关闭检查的次数
  • reconnect_interval: 在原主执行关闭后进行完全关闭检查的间隔时间
  • replication_lag_critical: 复制延迟(秒)超过该值退出主从切换
  • standby_reconnect_timeout: 原主降级重启后尝试连接到其的超时秒数.

新主将触发standby_switchover和standby_promote 事件

standby_switchover参数

  • %p:旧主的node id

旧主将触发node_rejoin 事件

repmgr node status节点状态

检查当前节点的基本信息和复制状态

例如:

$ repmgr node status
Node "node1":
    PostgreSQL version: 10beta1
    Total data size: 30 MB
    Conninfo: host=node1 dbname=repmgr user=repmgr connect_timeout=2
    Role: primary
    WAL archiving: off
    Archive command: (none)
    Replication connections: 2 (of maximal 10)
    Replication slots: 0 (of maximal 10)
    Replication lag: n/a

repmgr node check 节点复制检查

从复制的角度对本地节点进行检查

例如

$ repmgr -f /etc/repmgr.conf node check
Node "node1":
    Server role: OK (node is primary)
    Replication lag: OK (N/A - node is primary)
    WAL archiving: OK (0 pending files)
    Downstream servers: OK (2 of 2 downstream nodes attached)
    Replication slots: OK (node has no replication slots)

参数说明:

  • –role: 检查角色是否如预期
  • –replication-lag: 检查延迟是否超过repmgr.conf配置的replication_lag_warning或replication_lag_critical
  • –archive-ready: 检查WAL文件是否被存档
  • –downstream: 检查下游节点是否如预期
  • –slots: 检查是否有非活动的复制槽

repmgr node rejoin节点重新加入

说明

将休眠(已停止)节点重新加入到复制群集

可选择使用pg_rewind来重新加入脱离集群的节点,通常是发生故障的主节点。

提示: 如果节点正在运行并且需要连接到当前的主节点,请使用 repmgr standby follow

参数

repmgr node rejoin

  • -d:必须,指定集群中任何可访问的节点的conninfo
  • –dry-run:测试
  • –force-rewind[=/path/to/pg_rewind]:需要时使用pg_rewind
  • –config-files:逗号间隔的保留文件,pg_rewind会重写所有文件,这里可指定保留的文件
  • –config-archive-dir:临时存放config-files的位置,默认/tmp

触发node_rejoin 事件

repmgr cluster show集群状态

repmgr cluster matrix集群连通性矩阵

repmgr cluster event 事件查询

参数

  • –all: 输出所有数据
  • –limit: 设置最大输出数量 (default: 20)
  • –node-id: 查询指定node id的事件
  • –node-name: 查询指定node name的事件
  • –event: 查询指名称的事件

repmgr cluster cleanup

如果监控数据在repmgr.conf中设置以下参数

  • monitoring_history:yes/no,是否启用监控
  • #monitor_interval_secs:监控间隔时间

监控数据量很大需要定期清理

本命令通过参数-k/–keep-history只保留指定天数的数据

例如保留30天

repmgr cluster cleanup -k30
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值