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

本文详细介绍了如何使用repmgr工具在PostgreSQL中搭建主从集群,包括安装repmgr、配置SSH互信、设置PostgreSQL参数、创建数据库用户、配置pg_hba.conf、注册主从节点、配置repmgrd等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

安装

有网安装

安装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 postgres

repmgr -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=2
 136 | 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 {
    missingok
    compress
    rotate 30
    daily
    dateext
    create 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`
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值