故障转移后是否可以使用 postgres libpq 的多主机连接字符串连接到已恢复的主机?
我有 2 个数据库主机:host1 和 host2。 我使用多主机连接字符串连接到数据库:postgresql://host1:port1,host2:port2/
。根据 libpq 文档,与第一个成功的主机建立连接给出的列表。当两台主机都运行良好时,将建立与 host1 的连接,并且所有查询均由 host1 提供服务。当主机 1 关闭时,将建立与主机 2 的连接,并从主机 2 提供查询服务。现在,当主机 1 恢复并再次运行时,我希望再次与主机 1 建立连接,并且希望再次从主机 1 提供查询服务。但这并没有发生。
我使用 golang 的 pgx 库作为支持多主机连接字符串功能的驱动程序。是否有机制检查 host1 是否启动并在启动后与其建立连接?
编辑:更多上下文
host1 是只读副本数据库,host2 是读写主数据库。我有这个多连接字符串设置仅用于读取查询。我希望这些读取查询主要由只读数据库提供服务。所以我想将连接切换回host1。
I have 2 db hosts: host1 and host2.
I connect to db using multi host connection string: postgresql://host1:port1,host2:port2/
. According to libpq documentation, connection is established with first successful host in the list given. When both hosts are running fine, connection with host1 is established and all queries are served from host1. When host1 becomes down, connection with host2 is established and queries are served from host2. Now, when host1 is recovered and running back again, I want to the connection to be established with host1 again and I want queries to be served from host1 again. But it doesn't happen.
I am using golang's pgx
library as driver which supports multi-host connection string feature. Is there mechanism which checks if host1 is up or not and establish connection with it once it is up?
Edit: more context
host1 is read-only replica db and host2 is read-write primary db. I have this multi-connection string setup only for read queries. I want those read queries to be served primarily by read-only db. So I want to switch connection back to host1.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当它必须建立连接时,它将与列表中的第一个健康主机建立连接。在故障转移期间,与主机 1 的所有连接都丢失了,因此与主机 2 建立了新的连接。但只要主机 2 保持运行,这些连接仍然良好,因此不需要打开新连接(这会导致主持 1).您始终可以使主机 2 失败以强制断开所有这些连接。
It will establish connections with the first healthy host in the list when it has to establish a connection. During the failover, all connections to host 1 were lost, so new connections were established to host 2. But as long as host 2 stays up, those connections are still good, so it doesn't need to open new connections (which would go to host 1). You can always fail host 2 to force all those connections to disconnect.