前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >基于repmgr的postgresql主备高可用方案

基于repmgr的postgresql主备高可用方案

作者头像
数据库架构之美
修改2019-12-27 20:10:28
3.2K0
修改2019-12-27 20:10:28
举报
文章被收录于专栏:数据库架构之美

本文比较基础,主要介绍postgresql开源高可用工具repmgr的部署和使用,初学者可以根据本文步骤一步一步做下去,废话不多说,直接进入主题,本文以两台机器为例。

1.两台机器分别编译安装postgresql,步骤略。

2.主库配置

代码语言:javascript
复制
vi postgresql.conf
wal_log_hints=on
archive_mode=on
archive_command=’test ! -f /pgarch/%f && cp %p /pgarch/%f’

创建管理用户和库

代码语言:javascript
复制
createuser -s repmgr
createdb repmgr -O repmgr
 
vi pg_hba.conf
local   replication   repmgr                              trust
host    replication   repmgr      127.0.0.1/32            trust
host    replication   repmgr      192.168.1.1/32          trust
host    replication   repmgr      192.168.1.2/32          trust
local   repmgr        repmgr                              trust
host    repmgr        repmgr      127.0.0.1/32            trust
host    repmgr        repmgr      192.168.1.1/32          trust
host    repmgr        repmgr      192.168.1.2/32          trust

备库连接测试

代码语言:javascript
复制
psql 'host=192.168.1.1 user=repmgr dbname=repmgr connect_timeout=2'

主库创建/etc/repmgr.conf

代码语言:javascript
复制
node_id=1
node_name=node1
conninfo='host=192.168.1.1 user=repmgr dbname=repmgr connect_timeout=2'
data_directory='/pgdata'

注册主库

代码语言:javascript
复制
repmgr -f /etc/repmgr.conf primary register

查看:

代码语言:javascript
复制
repmgr -f /etc/repmgr.conf cluster show
SELECT * FROM repmgr.nodes;

3.备库配置

创建/etc/repmgr.conf

代码语言:javascript
复制
node_id=2
node_name=node2
conninfo='host=192.168.1.2 user=repmgr dbname=repmgr connect_timeout=2'
data_directory='/pgdata'

克隆备库,内部使用的是pg_basebackup来进行克隆,并且会自动创建recovery.conf文件

代码语言:javascript
复制
repmgr -h 192.168.1.1 -U repmgr -d repmgr -f /etc/repmgr.conf standby clone --dry-run
repmgr -h 192.168.1.1 -U repmgr -d repmgr -f /etc/repmgr.conf standby clone

启动并注册备库

代码语言:javascript
复制
repmgr -f /etc/repmgr.conf standby register

查看集群状态

代码语言:javascript
复制
repmgr -f /etc/repmgr.conf cluster show
[postgres@node2 pgdata]$ repmgr cluster show
 ID | Name  | Role    | Status    | Upstream | Location | Priority | Timeline | Connection string                                            
----+-------+---------+-----------+----------+----------+----------+----------+---------------------------------------------------------------
 1  | node1 | primary | * running |          | default  | 100      | 3        | host=192.168.1.1 user=repmgr dbname=repmgr connect_timeout=2
 2  | node2 | standby |   running | node1    | default  | 100      | 3        | host=192.168.1.2 user=repmgr dbname=repmgr connect_timeout=2

查看复制状态

代码语言:javascript
复制
select * from pg_stat_replication;
select * from pg_stat_wal_receiver;

切换测试,在备库操作,注意,切换操作需要配置主机间互信。

代码语言:javascript
复制
[postgres@DB2 .ssh]$ repmgr standby switchover
NOTICE: executing switchover on node "node2" (ID: 2)
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
DETAIL: executing server command "pg_ctl  -D '/pgdata' -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")
INFO: checking for primary shutdown; 3 of 60 attempts ("shutdown_check_timeout")
NOTICE: current primary has been cleanly shut down at location 0/14000028
NOTICE: promoting standby to primary
DETAIL: promoting server "node2" (ID: 2) using "pg_ctl  -w -D '/pgdata' promote"
waiting for server to promote.... done
server promoted
NOTICE: waiting up to 60 seconds (parameter "promote_check_timeout") for promotion to complete
NOTICE: STANDBY PROMOTE successful
DETAIL: server "node2" (ID: 2) was successfully promoted to primary
INFO: local node 1 can attach to rejoin target node 2
DETAIL: local node's recovery point: 0/14000028; rejoin target node's fork point: 0/14000098
NOTICE: setting node 1's upstream to node 2
WARNING: unable to ping "host=192.168.1.1 user=repmgr dbname=repmgr connect_timeout=2"
DETAIL: PQping() returned "PQPING_NO_RESPONSE"
NOTICE: starting server using "pg_ctl  -w -D '/pgdata' start"
NOTICE: NODE REJOIN successful
DETAIL: node 1 is now attached to node 2
NOTICE: switchover was successful
DETAIL: node "node2" is now primary and node "node1" is attached as standby
NOTICE: STANDBY SWITCHOVER has completed successfully

此时查看状态,已经切换完成:

代码语言:javascript
复制
[postgres@GCCX4TMP .ssh]$ repmgr cluster show
 ID | Name  | Role    | Status    | Upstream | Location | Priority | Timeline | Connection string                                            
----+-------+---------+-----------+----------+----------+----------+----------+---------------------------------------------------------------
 1  | node1 | standby |   running | node2    | default  | 100      | 3        | host=192.168.1.1 user=repmgr dbname=repmgr connect_timeout=2
 2  | node2 | primary | * running |          | default  | 100      | 4        | host=192.168.1.2 user=repmgr dbname=repmgr connect_timeout=2

看上面的切换日志其实可以看到切换的一个流程:

①停止主库

②备库promte为主库

③原主库执行rejoin操作:repmgr node rejoin -d 'host=192.168.1.1 dbname=repmgr user=repmgr' --force-rewind --config-files=postgresql.conf,postgresql.auto.conf --verbose

这里说下repmgr node rejoin操作,执行该命令之前先删除recovery.conf文件,并且要求数据库之前是干净的关闭,达到一个一致性状态。然后repmgr会检查数据库能否加入,如果不能的话就会使用pg_rewind进行恢复操作,至于pg_rewind的原理和用法见我上一篇文章。

后面的文章我们再介绍一下如何通过repmgrd实现auto failover。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-09-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 数据库架构 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
数据库
云数据库为企业提供了完善的关系型数据库、非关系型数据库、分析型数据库和数据库生态工具。您可以通过产品选择和组合搭建,轻松实现高可靠、高可用性、高性能等数据库需求。云数据库服务也可大幅减少您的运维工作量,更专注于业务发展,让企业一站式享受数据上云及分布式架构的技术红利!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档