PostgreSQL+repmgr高可用部署

news2024/7/4 5:52:37

REPMGR 是一套在PostgreSQL服务器集群中用于管理复制和故障转移的开源工具 。它支持并增强了PostgreSQL的 内置流式复制,提供单个读/写主服务器 以及一个或多个只读备用数据库,其中包含主数据库的近实时副本服务器的数据库。

它提供了两个主要工具:

工具作用具体用途
repmgr用于执行管理任务的命令行工具设置备用服务器,将备用服务器提升为主服务器,切换主服务器和备用服务器,显示复制群集中服务器的状态
repmgrd主动监视复制群集中的服务器的守护程序监视和记录复制性能,通过检测主数据库和提升最合适的备用服务器,向用户定义的群集中事件提供有关事件的通知 可以执行任务的脚本,例如通过电子邮件发送警报

01 repmgr 版本对应支持的PostgreSQL版本

02 部署环境

操作系统:Scientific Linux release 7.9 (Nitrogen)

PostgreSQL版本:PostgreSQL 15.1

repmgr版本:repmgr-5.3.3

03 安装PostgreSQL

--安装相关依赖:

yum -y install gcc gcc_c++ libyaml  python* readline readline-devel zlib zlib-devel

--关闭防火墙,设置selinux(各节点)

systemctl stop firewalld

systemctl disable firewalld

sed -i 's/^SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

--创建目录及用户

mkdir -p /data/pg15

mkdir -p /data/pgarch

mkdir -p /usr/local/pg15

useradd postgres

chown -R postgres:postgres /data

chown -R postgres:postgres /usr/local/pg15

chmod 700 /data/pg15

--源码安装PG15.2(postgres用户,所有节点)

tar -xvf postgresql-15.2.tar.gz

cd postgresql-15.2

./configure --prefix=/usr/local/pg15/

make world && make install-world

04 设置环境变量

cat .bashrc

export PGDATA=/data/pg15

export PGHOME=/usr/local/pg15/

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$PGHOME/lib

export PATH=$PATH:$PGHOME/bin/

export PGPORT=5435

source .bashrc

05 初始化数据库(主节点)

initdb -D /data/pg15

--配置pg_hba.conf

host    all        all           192.168.126.0/24   md5

local   replication   repmgr                              trust

host    replication   repmgr      127.0.0.1/32            trust

host    replication   repmgr      192.168.126.0/24          trust

local   repmgr        repmgr                              trust

host    repmgr        repmgr      127.0.0.1/32            trust

host    repmgr        repmgr      192.168.126.0/24          trust

--配置postgresql.conf

listen_addresses = '*'

port = 5435

shared_buffers = 256MB

wal_level = replica

max_wal_size = 1GB

log_destination = 'csvlog'

logging_collector = on

log_directory = 'log'

log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'

log_file_mode = 0600

archive_mode = on

archive_command = 'cp %p /data/pgarch/%f'

max_wal_senders = 10

max_replication_slots = 10

hot_standby = on

--启动数据库

pg_ctl start -D /data/pg15

06 设置PostgreSQL开机自启(主备相同步骤设置)

[root@pg15_rh7_132 ~]# cd /home/postgres/postgresql-15.1/contrib/start-scripts/

[root@pg15_rh7_132 ~]# cp linux /etc/init.d/postgresql

[root@pg15_rh7_132 ~]# chmod +x /etc/init.d/postgresql

[root@pg15_rh7_132 ~]# vim /etc/init.d/postgresql

# Installation prefix 修改安装目录位置

prefix=/usr/local/pg15

# Data directory 修改数据目录位置

PGDATA="/data/pg15"

[root@pg15_rh7_132 ~]#  chkconfig --add postgresql

[root@pg15_rh7_132 ~]#  chkconfig --list postgresql

重启服务器

[root@pg15_rh7_132 ~]#  reboot

07 主库创建repmgr库存储元数据

postgres=# create user repmgr with superuser password 'qwe' connection limit 10;

CREATE ROLE

postgres=# create database repmgr owner  repmgr;

CREATE DATABASE

08 安装repmgr

--安装依赖

yum check-update

yum groupinstall "Development Tools" -y

yum install -y yum-utils openjade docbook-dtds docbook-style-dsssl docbook-style-xsl

yum -y install yum-builddep flex libselinux-devel libxml2-devel libxslt-devel openssl-devel pam-devel readline-devel

--解压安装repmgr(主备库都安装)

[root@pg15_rh7_132 ~]# tar -xvf repmgr-5.3.3.tar.gz -C /home/postgres/

[root@pg15_rh7_132 ~]#  chown -R postgres:postgres /home/postgres/

[root@pg15_rh7_132 ~]# su - postgres

[postgres@pg15_rh7_132 ~]$ cd repmgr-5.3.3

[postgres@pg15_rh7_132 ~]$  ./configure

[postgres@pg15_rh7_132 ~]$ make install


09 配置免密

[postgres@pg15_rh7_132 ~]$ ssh-keygen -t rsa

[postgres@pg15_rh7_132 ~]$ ssh-copy-id postgres@192.168.126.133

[postgres@pg15_rh7_133 ~]$ ssh-keygen -t rsa

[postgres@pg15_rh7_133 ~]$ ssh-copy-id postgres@192.168.126.132

[postgres@pg15_rh7_132 ~]$ cat .pgpass

192.168.126.132:5435:repmgr:repmgr:qwe

192.168.126.133:5435:repmgr:repmgr:qwe

[postgres@pg15_rh7_133 ~]$ cat .pgpass

192.168.126.132:5435:repmgr:repmgr:qwe

192.168.126.133:5435:repmgr:repmgr:qwe

10 相关的表和视图

表名 /视图用途备注
repmgr.events记录集群操作事件table
repmgr.monitoring_history历史备用监控信息table
repmgr.nodes每个服务器的连接和状态信息table
repmgr.replication_status启用 repmgrd 的监控后,会显示 每个备用数据库的当前监视状态view
repmgr.show_nodes基于repmgr.nodes ,显示服务器连接状态信息view

11 主库创建配置文件repmgr.conf

cat repmgr.conf

node_id=1

   node_name='node1'

   conninfo='host=192.168.126.132 port=5435  user=repmgr dbname=repmgr connect_timeout=2'

   data_directory='/data/pg15'

   测试repmgr是否能免密访问主库

   [postgres@pg15_rh7_133 repmgr-5.3.3]$ psql -p5435 -h 192.168.126.132 -Urepmgr

12 使用repmgr命令注册主库

[postgres@pg15_rh7_132 ~]$ chmod 600 .pgpass

[postgres@pg15_rh7_132 ~]$ /usr/local/pg15/bin/repmgr -f /home/postgres/repmgr.conf primary register --force

INFO: connecting to primary database...

NOTICE: attempting to install extension "repmgr"

NOTICE: "repmgr" extension successfully installed

NOTICE: primary node record (ID: 1) registered

--查看注册信息

[postgres@pg15_rh7_132 ~]$ repmgr -f repmgr.conf cluster show

ID | Name  | Role    | Status    | Upstream | Location | Priority | Timeline | Connection string                                                          

----+-------+---------+-----------+----------+----------+----------+----------+-----------------------------------------------------------------------------

1  | node1 | primary | * running |          | default  | 100      | 1        | host=192.168.126.132 port=5435  user=repmgr dbname=repmgr connect_timeout=2

repmgr=# select * from repmgr.nodes ;

-[ RECORD 1 ]----+----------------------------------------------------------------------------

node_id          | 1

upstream_node_id |

active           | t

node_name        | node1

type             | primary

location         | default

priority         | 100

conninfo         | host=192.168.126.132 port=5435  user=repmgr dbname=repmgr connect_timeout=2

repluser         | repmgr

slot_name        |

config_file      | /home/postgres/repmgr.conf

13 使用repmgr命令克隆备库

[postgres@pg15_rh7_133 ~]$ cat repmgr.conf

   node_id=2

   node_name='node2'

   conninfo='host=192.168.126.133 port=5435  user=repmgr dbname=repmgr connect_timeout=2'

   data_directory='/data/pg15'

[postgres@pg15_rh7_133 ~]$ /usr/local/pg15/bin/repmgr -h 192.168.126.132 -p5435 -U repmgr -d repmgr -f /home/postgres/repmgr.conf standby clone --dry-run

NOTICE: destination directory "/data/pg15" provided

INFO: connecting to source node

DETAIL: connection string is: host=192.168.126.132 port=5435 user=repmgr dbname=repmgr

DETAIL: current installation size is 29 MB

INFO: "repmgr" extension is installed in database "repmgr"

WARNING: target data directory appears to be a PostgreSQL data directory

DETAIL: target data directory is "/data/pg15"

HINT: use -F/--force to overwrite the existing data directory

INFO: replication slot usage not requested;  no replication slot will be set up for this standby

INFO: parameter "max_wal_senders" set to 10

NOTICE: checking for available walsenders on the source node (2 required)

INFO: sufficient walsenders available on the source node

DETAIL: 2 required, 10 available

NOTICE: checking replication connections can be made to the source server (2 required)

INFO: required number of replication connections could be made to the source server

DETAIL: 2 replication connections required

WARNING: data checksums are not enabled and "wal_log_hints" is "off"

DETAIL: pg_rewind requires "wal_log_hints" to be enabled

NOTICE: standby will attach to upstream node 1

HINT: consider using the -c/--fast-checkpoint option

INFO: would execute:

 pg_basebackup -l "repmgr base backup"  -D /data/pg15 -h 192.168.126.132 -p 5435 -U repmgr -X stream

INFO: all prerequisites for "standby clone" are met

--根据提示查看相对应的参数设置是否没有去掉注释

--注意备库repmgr.conf文件中的目录要为空

[postgres@pg15_rh7_133 ~]$  /usr/local/pg15/bin/repmgr -h 192.168.126.132 -p5435 -U repmgr -d repmgr -f /home/postgres/repmgr.conf standby clone --force

NOTICE: destination directory "/data/pg15" provided

INFO: connecting to source node

DETAIL: connection string is: host=192.168.126.132 port=5435 user=repmgr dbname=repmgr

DETAIL: current installation size is 29 MB

INFO: replication slot usage not requested;  no replication slot will be set up for this standby

NOTICE: checking for available walsenders on the source node (2 required)

NOTICE: checking replication connections can be made to the source server (2 required)

WARNING: data checksums are not enabled and "wal_log_hints" is "off"

DETAIL: pg_rewind requires "wal_log_hints" to be enabled

WARNING: directory "/data/pg15" exists but is not empty

NOTICE: -F/--force provided - deleting existing data directory "/data/pg15"

NOTICE: starting backup (using pg_basebackup)...

HINT: this may take some time; consider using the -c/--fast-checkpoint option

INFO: executing:

 pg_basebackup -l "repmgr base backup"  -D /data/pg15 -h 192.168.126.132 -p 5435 -U repmgr -X stream

NOTICE: standby clone (using pg_basebackup) complete

NOTICE: you can now start your PostgreSQL server

HINT: for example: pg_ctl -D /data/pg15 start

HINT: after starting the server, you need to register this standby with "repmgr standby register"

--启动备库

pg_ctl -D /data/pg15 start

14 验证复制是否正常工作

--连接到主库查看

repmgr=# SELECT * FROM pg_stat_replication;

-[ RECORD 1 ]----+------------------------------

pid              | 17730

usesysid         | 24586

usename          | repmgr

application_name | node2

client_addr      | 192.168.126.133

client_hostname  |

client_port      | 49068

backend_start    | 2023-05-04 14:21:56.605203+08

backend_xmin     |

state            | streaming

sent_lsn         | 0/150001F0

write_lsn        | 0/150001F0

flush_lsn        | 0/150001F0

replay_lsn       | 0/150001F0

write_lag        |

flush_lag        |

replay_lag       |

sync_priority    | 0

sync_state       | async

reply_time       | 2023-05-04 20:51:52.968141+08

15 注册备用数据库

repmgr -f  /home/postgres/repmgr.conf standby register

INFO: connecting to local node "node2" (ID: 2)

INFO: connecting to primary database

WARNING: --upstream-node-id not supplied, assuming upstream node is primary (node ID: 1)

INFO: standby registration complete

NOTICE: standby node "node2" (ID: 2) successfully registered

--查看集群状态

[postgres@pg15_rh7_133 ~]$ repmgr -f  /home/postgres/repmgr.conf cluster show

ID | Name  | Role    | Status    | Upstream | Location | Priority | Timeline | Connection string                                                          

----+-------+---------+-----------+----------+----------+----------+----------+-----------------------------------------------------------------------------

1  | node1 | primary | * running |          | default  | 100      | 1        | host=192.168.126.132 port=5435  user=repmgr dbname=repmgr connect_timeout=2

2  | node2 | standby |   running | node1    | default  | 100      | 1        | host=192.168.126.133 port=5435  user=repmgr dbname=repmgr connect_timeout=2

16 主备机切换(切换pg15_rh7_133为主)

[postgres@pg15_rh7_133 ~]$ repmgr -f repmgr.conf  standby switchover -U repmgr --verbose

NOTICE: using provided configuration file "repmgr.conf"

WARNING: following problems with command line parameters detected:

 database connection parameters not required when executing STANDBY SWITCHOVER

NOTICE: executing switchover on node "node2" (ID: 2)

INFO: searching for primary node

INFO: checking if node 1 is primary

INFO: current primary node is 1

INFO: SSH connection to host "192.168.126.132" succeeded

INFO: 0 pending archive files

INFO: replication lag on this standby is 0 seconds

NOTICE: attempting to pause repmgrd on 2 nodes

NOTICE: local node "node2" (ID: 2) will be promoted to primary; current primary "node1" (ID: 1) will be demoted to standby

NOTICE: stopping current primary node "node1" (ID: 1)

NOTICE: issuing CHECKPOINT on node "node1" (ID: 1)

DETAIL: executing server command "pg_ctl  -D '/data/pg15' -W -m fast stop"

INFO: checking for primary shutdown; 1 of 60 attempts ("shutdown_check_timeout")

INFO: checking for primary shutdown; 2 of 60 attempts ("shutdown_check_timeout")

NOTICE: current primary has been cleanly shut down at location 0/16000028

NOTICE: promoting standby to primary

DETAIL: promoting server "node2" (ID: 2) using pg_promote()

NOTICE: waiting up to 60 seconds (parameter "promote_check_timeout") for promotion to complete

INFO: standby promoted to primary after 1 second(s)

NOTICE: STANDBY PROMOTE successful

DETAIL: server "node2" (ID: 2) was successfully promoted to primary

INFO: node "node1" (ID: 1) is pingable

INFO: node "node1" (ID: 1) has attached to its upstream node

NOTICE: node "node2" (ID: 2) promoted to primary, node "node1" (ID: 1) demoted to standby

NOTICE: switchover was successful

DETAIL: node "node2" is now primary and node "node1" is attached as standby

NOTICE: STANDBY SWITCHOVER has completed successfully

查看集群主备状态

[postgres@pg15_rh7_133 ~]$ repmgr -f repmgr.conf  cluster show

ID | Name  | Role    | Status    | Upstream | Location | Priority | Timeline | Connection string                                                          

----+-------+---------+-----------+----------+----------+----------+----------+-----------------------------------------------------------------------------

1  | node1 | standby |   running | node2    | default  | 100      | 3        | host=192.168.126.132 port=5435  user=repmgr dbname=repmgr connect_timeout=2

2  | node2 | primary | * running |          | default  | 100      | 4        | host=192.168.126.133 port=5435  user=repmgr dbname=repmgr connect_timeout=2

[postgres@pg15_rh7_133 ~]$ psql repmgr

psql (15.2)

Type "help" for help

repmgr=# \x

Expanded display is on.

repmgr=# select * from pg_stat_replication;

-[ RECORD 1 ]----+------------------------------

pid              | 6593

usesysid         | 24586

usename          | repmgr

application_name | node1

client_addr      | 192.168.126.132

client_hostname  |

client_port      | 44310

backend_start    | 2023-05-04 22:41:31.735749+08

backend_xmin     |

state            | streaming

sent_lsn         | 0/16000910

write_lsn        | 0/16000910

flush_lsn        | 0/16000910

replay_lsn       | 0/16000910

write_lag        |

flush_lag        |

replay_lag       |

sync_priority    | 0

sync_state       | async

reply_time       | 2023-05-04 16:13:36.50688+08

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/494238.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

配电室设备监测怎么办?管理高手都是这样做的!

随着智能电网的不断推进,供配电安全也逐渐进入人们的视野,传统人工巡检的方式与当前智能化配电室的建设显得格格不入。 配电室,作为分配多路低压负荷开关的重要节点,其安全系数不言而喻,在管理和监控方面需要慎之又慎。…

数据结构初阶--链表OJ

目录 前言移除链表元素思路分析代码实现 链表的中间节点思路分析代码实现 反转链表思路分析代码实现 链表分割思路分析代码实现 合并两个有序链表思路分析代码实现 前言 本篇文章将对部分单链表的OJ题进行讲解 移除链表元素 我们先来看题 思路分析 我们可以采用双指针的方…

测试常见概念

文章目录 需求测试用例BUG软件生命周期开发模型scrum测试模型 需求 需求的概念:满足用户期望或正式规定文档(合同、标准、规范)所具有的条件和权能,包含用户需求和软件需求 用户需求:可以简单理解为甲方提出的需求,如果没有甲方&…

java错题总结(19-21页)

链接:关于Java中的ClassLoader下面的哪些描述是错误的_用友笔试题_牛客网 来源:牛客网 B:先讲一下双亲委派机制,简单来说,就是加载一个类的时候,会往上找他的父类加载器,父类加载器找它的父类加…

Netty(1)

Netty 文章目录 Netty1 Netty 基本介绍2 why Netty2.1 原生 NIO 问题2.2 Netty 优点 3 I/O 线程模型3.1 传统阻塞 I/O 模型3.2 Reactor 模式3.2.1 Reactor 模式解决传统 I/O 方案3.2.2 Reactor 模式原理图3.2.3 Reactor 的核心组件3.2.4 单 Readcot 单线程(NIO模型)3.2.5 单 Re…

非科班转码,春招总结!

作者:阿秀 校招八股文学习网站:https://interviewguide.cn 这是阿秀的第「263」篇原创 小伙伴们大家好,我是阿秀。 欢迎今年参加秋招的小伙伴加入阿秀的学习圈,目前已经超过 2300 小伙伴加入!去年认真准备和走下来的基…

Twitter 推荐算法底有多牛? 已斩获11.7K star

点击上方“Github中文社区”,关注 看Github,每天提升第070期分享 ,作者:Huber | Github中文社区 大家好,我是Huber。 在美国当地时间 3 月 31 日,马斯克履行当初的诺言,他宣布了 Twitter 算法的…

《编程思维与实践》1048.解密字符串

《编程思维与实践》1048.解密字符串 题目 思路 主要到密码是升序的,所以先将每个数字对应的个数求出,之后升序排列输出即可得到结果. 求每个数字(0-9)对应的个数可以考虑每个英文单词中特有的字符(出现单次), zero,one,two,three,four,five,six,seven,eight,nine; 下面提供其中…

系统性能压力测试

系统性能压力测试 一、压力测试 压力测试是给软件不断加压,强制其在极限的情况下运行,观察它可以运行到何种程度,从而发现性能缺陷,是通过搭建与实际环境相似的测试环境,通过测试程序在同一时间内或某一段时间内&…

深度学习—卷积神经网络

卷积神经网络 传统意义上的多层神经网络只有输入层、隐藏层和输出层。其中隐藏层的层数根据需要而定,没有明确的理论推导来说明到底多少层合适。 卷积神经网络CNN,在原来多层神经网络的基础上,加入了更加有效的特征学习部分,具…

人生四维度

人生四维度 不是有钱了就成功,你知道;人生的成功不止一种,你也知道。但成功还有哪种?你知道吗? 如果把人生的体验展开,我们可以得到四个维度,高度、深度、宽度和温度。 财富、权力、影响力 构…

2023年3月股份行GX评测盘点:招商银行稳居榜首,各项指标均居前列

易观:2023 年3月GX评测数据显示,招商银行、平安口袋银行、中信银行位居行业Top 10,浦发银行、兴业银行、光大银行紧跟其后。 股份行APP 用户体验:招商银行以绝对优势稳居第一 2023年3月股份行GX评测结果数据显示,在操作…

VScode代码编辑器官网下载慢问题解决方法-亲测有效

VScode官网下载慢的问题如何解决? 问题描述: VisualStudioCode(简称VSCode)是Microsoft开发的一款功能强大的代码编辑器,它支持Windows,Linux和macOS等操作系统以及开源代码,因此被很多开发人…

maven从入门到精通 第四章 Maven中依赖的传递、排除、继承、聚合

这里写自定义目录标题 一 maven中依赖的传递1 依赖的传递性2 使用complie范围依赖spring-core3 测试依赖是否被传递4 依赖传递性的意义 二 maven中依赖的排除1 依赖排除概述2 具体操作依赖排除 三 maven中依赖的继承四 maven中依赖的聚合 一 maven中依赖的传递 1 依赖的传递性…

thinkphp+vue+html超市零食品美食推荐系统零食购物商城网站7v281

本系统的开发使获取食品推荐系统信息能够更加方便快捷,同时也使食品推荐系统管理信息变的更加系统化、有序化。系统界面较友好,易于操作 运行环境:phpstudy/wamp/xammp等 开发语言:php 后端框架:Thinkphp5 前端框架:vu…

Linux入门2(常用命令)

Linux入门2 Linux常用命令快捷键基础命令文件查看命令文件编辑命令进程管理命令用户管理命令 Linux常用命令 快捷键 Ctrl Alt T打开终端 Ctrl shift 加号 终端字体放大 ctrl 减号 终端字体缩小 基础命令 sudo su 进入管理员目录 exit 返回到用户目录 ls 当前目录下的文…

数据导向下制造业的生产效率、交易效率提升办法

在智能制造和工业4.0成为趋势的今天,大部分制造业企业,均已在企业内部通过实施PLM系统(Product Lifecycle Management,产品生命周期管理系统),实现了对组织内产品研发过程和产品研发数据的管理,…

联发科MT8768核心板 安卓4G核心板智能模块MTK方案主板开发板

MT8768核心板是一款性能优异的芯片板,具有以下特点: 处理器方面,MT8768核心板采用联发科MTK8768平台,搭载八核A53处理器,最高主频可达2.3GHz,为您提供出色的运行速度和高效率。 内存和存储方面,…

OAK相机如何将 YOLO NAS 模型转换成blob格式?

编辑:OAK中国 首发:oakchina.cn 喜欢的话,请多多👍⭐️✍ 内容可能会不定期更新,官网内容都是最新的,请查看首发地址链接。 ▌前言 Hello,大家好,这里是OAK中国,我是助手…

数据库迁移同步 | 两地三中心到异地双活演变及关键技术探讨

两地三中心和异地多活都是分布式系统的关键技术,用于保证系统的高可用性和容错性。其中最关键的技术无疑是数据同步、同步防环和数据冲突解决。 异地容灾 & 两地三中心 两地三中心架构是一种分布式系统的架构模式,用于保证系统的高可用性和容错性。…