PostgreSQL libpq multi-host 配置与简单HA实现

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
云原生数据库 PolarDB 分布式版,标准版 2核8GB
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

标签

PostgreSQL , HA , libpq , multi host , target_session_attrs


背景

PostgreSQL libpq 是数据库的一个连接驱动,支持多主机配置,同时支持target_session_attrs 主机角色判断配置。

当配置了多个主机时,会按顺序尝试连接,之道获取到成功的连接为止。

什么是成功的连接,满足以下两点:

1、能与之成功建立连接。

2、如果target_session_attrs配置为read-write,那么SHOW transaction_read_only 如果返回ON,则也是不成功的连接。

target_session_attrs配置为read-write的意思是,只连接到可以读写的节点,比如standby就是只读的,所以不能算成功的连接。

利用libpq的这个特性,结合数据库自动HA的一些软件,可以实现在不引入VIP以及中间路由节点的情况下实现数据库应用系统层级的高可用。

例如,有流复制组成的两个节点,或者由共享存储组成的两个节点。

在libpq中配置两个节点的HOST:PORT,当数据库某个主机发生异常切换到另一个主机,不需要漂移VIP,libpq可以帮你实现主节点的自动选择。(当然,需要配合自动断开重连的机制来实现)

也可以与三节点(多副本同步复制)结合来使用。

例子

格式

postgresql://[user[:password]@][netloc][:port][,...][/dbname][?param1=value1&...]  

1、多HOST配置例子

postgresql://host1:123,host2:456/somedb?target_session_attrs=any&application_name=myapp  

说明

https://www.postgresql.org/docs/10/static/libpq-connect.html#LIBPQ-MULTIPLE-HOSTS

33.1.1.3. Specifying Multiple Hosts  
  
It is possible to specify multiple hosts to connect to, so that they are tried in the given order.   
In the Keyword/Value format, the host, hostaddr, and port options accept a comma-separated list of values.   
The same number of elements must be given in each option, such that e.g.   
the first hostaddr corresponds to the first host name,   
the second hostaddr corresponds to the second host name, and so forth.   
As an exception, if only one port is specified, it applies to all the hosts.  
  
In the connection URI format, you can list multiple host:port pairs separated by commas,   
in the host component of the URI. In either format, a single hostname can also translate   
to multiple network addresses. A common example of this is a host that has both an IPv4 and an IPv6 address.  
  
When multiple hosts are specified, or when a single hostname is translated to multiple addresses,   
all the hosts and addresses will be tried in order, until one succeeds.   
If none of the hosts can be reached, the connection fails. If a connection is established successfully,   
but authentication fails, the remaining hosts in the list are not tried.  
  
If a password file is used, you can have different passwords for different hosts.   
All the other connection options are the same for every host, it is not possible to e.g.   
specify a different username for different hosts.  
  
  
target_session_attrs  
  
If this parameter is set to read-write, only a connection in which read-write transactions are accepted by default is considered acceptable.   
The query SHOW transaction_read_only will be sent upon any successful connection;   
if it returns on, the connection will be closed.   
  
If multiple hosts were specified in the connection string, any remaining servers will be tried just as if the connection attempt had failed.   
The default value of this parameter, any, regards all connections as acceptable.  

如何判断是否为备节点。

  
postgres=# select pg_is_in_recovery();  
 pg_is_in_recovery   
-------------------  
 t  
(1 row)  
  
  
备节点transaction_read_only参数为ON   
  
postgres=# show transaction_read_only ;  
 transaction_read_only   
-----------------------  
 on  
(1 row)  
  

小结

优点:HA简化了网络结构,因为不再需要管VIP漂移的事情。

缺点:因为CLIENT直接连物理主机IP,如果物理主机搬迁IP变化,需要调整libpq配置。

参考

https://www.postgresql.org/docs/10/static/libpq-connect.html#LIBPQ-MULTIPLE-HOSTS

《PostgreSQL 一主多从(多副本,强同步)简明手册 - 配置、压测、监控、切换、防脑裂、修复、0丢失 - 珍藏级》

《PG多节点(quorum based), 0丢失 HA(failover,switchover)方案》

https://github.com/digoal/PostgreSQL_HA_with_primary_standby_2vip

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
29天前
|
SQL 关系型数据库 MySQL
MySQL中主从复制的原理和配置命令
要原因包括提高性能、实现高可用性、数据备份和灾难恢复。了解两大线程( I/O 和 SQL)I/O线程:目的:I/O线程主要负责与MySQL服务器之外的其他MySQL服务器进行通信,以便复制(replication)数据。 功能: 当一个MySQL服务器作为主服务器(master)时,I/O线程会将变更日志(binary log)中的事件传输给从服务器(slave)。从服务器上的I/O线程负责接收主服务器的二进制日志,并将这些事件写入本地的中继日志(relay log)。 配置: 在MySQL配置文件中,你可以通过配置参数如和来启用二进制日志和指定服务器ID。log-bin server
MySQL中主从复制的原理和配置命令
|
28天前
|
SQL 缓存 关系型数据库
mysql性能优化-慢查询分析、优化索引和配置
mysql性能优化-慢查询分析、优化索引和配置
92 1
|
1天前
|
关系型数据库 MySQL 数据库
MySQL集群 双主架构(配置命令)
MySQL集群 双主架构(配置命令)
|
6天前
|
SQL 关系型数据库 MySQL
【MySQL-3】图形化界面工具DataGrip安装&配置&使用
【MySQL-3】图形化界面工具DataGrip安装&配置&使用
|
6天前
|
关系型数据库 MySQL Linux
【MySQL-2】MySQL的下载&安装&启停&配置环境变量【一条龙教程】
【MySQL-2】MySQL的下载&安装&启停&配置环境变量【一条龙教程】
|
7天前
|
分布式计算 DataWorks 关系型数据库
DataWorks操作报错合集之DataWorks集成实例绑定到同一个vpc下面,也添加了RDS的IP白名单报错:数据源配置有误,请检查,该怎么处理
DataWorks是阿里云提供的一站式大数据开发与治理平台,支持数据集成、数据开发、数据服务、数据质量管理、数据安全管理等全流程数据处理。在使用DataWorks过程中,可能会遇到各种操作报错。以下是一些常见的报错情况及其可能的原因和解决方法。
22 0
|
7天前
|
Ubuntu 关系型数据库 MySQL
Ubuntu 20.04 + mysql8 安装以及配置大小写不敏感
Ubuntu 20.04 + mysql8 安装以及配置大小写不敏感
|
7天前
|
DataWorks 关系型数据库 MySQL
DataWorks产品使用合集之在DataWorks中配置RDS MySQL数据源的步骤如何解决
DataWorks作为一站式的数据开发与治理平台,提供了从数据采集、清洗、开发、调度、服务化、质量监控到安全管理的全套解决方案,帮助企业构建高效、规范、安全的大数据处理体系。以下是对DataWorks产品使用合集的概述,涵盖数据处理的各个环节。
21 0
|
8天前
|
存储 关系型数据库 MySQL
linux安装MySQL8.0,密码修改权限配置等常规操作详解
linux安装MySQL8.0,密码修改权限配置等常规操作详解
|
8天前
|
关系型数据库 MySQL 数据库
【MySQL】:超详细MySQL完整安装和配置教程
【MySQL】:超详细MySQL完整安装和配置教程
87 1

相关产品

  • 云原生数据库 PolarDB