PostgreSQL中Failover的配置方法是什么

37次阅读
没有评论

共计 1989 个字符,预计需要花费 5 分钟才能阅读完成。

本篇内容介绍了“PostgreSQL 中 Failover 的配置方法是什么”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让丸趣 TV 小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

PostgreSQL 10+ 的 libpq 版本提供了 Seamless Application Failover 功能,在连接 PG 的时候可指定多个目标库并指定 target_session_attrs 属性(read-write、any、read-only),libpq 可根据目标库的状态分发 / 切换到不同的数据库上。
举个例子,比如现在在 26.28:5433 和 26.26:5432 上有两个 PG 实例,两个实例均可对外提供读写,Failover 的配置如下:
1.read-write/any

[xdb@localhost ~]$ psql  postgres://192.168.26.26:5432,192.168.26.28:5433/postgres?target_session_attrs=read-write  -U xdb -c  select inet_server_addr() 
Timing is on.
Expanded display is used automatically.
 inet_server_addr 
------------------
 192.168.26.26
(1 row)
Time: 2.439 ms
[xdb@localhost ~]$

连接到第一个匹配的实例上。关闭 26.26 上的 pg,重新连接,会自动切换到 26.28 上的 pg 上。

[xdb@localhost ~]$ pg_ctl stop
waiting for server to shut down.... done
server stopped
[xdb@localhost ~]$ 
[xdb@localhost ~]$ psql  postgres://192.168.26.26:5432,192.168.26.28:5433/postgres?target_session_attrs=read-write  -U xdb -c  select inet_server_addr() 
Timing is on.
Expanded display is used automatically.
 inet_server_addr 
------------------
 192.168.26.28
(1 row)
Time: 0.837 ms
[xdb@localhost ~]$

搭建主从流复制环境,一个主库一个备库,分别是 26.28 和 26.25

[xdb@localhost ~]$ psql  postgres://192.168.26.25:5432,192.168.26.28:5432/postgres?target_session_attrs=read-write  -c  select inet_server_addr()  -U pg12
Password for user pg12: 
 inet_server_addr 
------------------
 192.168.26.28
(1 row)
[xdb@localhost ~]$ psql  postgres://192.168.26.25:5432,192.168.26.28:5432/postgres?target_session_attrs=any  -c  select inet_server_addr()  -U pg12
Password for user pg12: 
 inet_server_addr 
------------------
 192.168.26.25
(1 row)

使用 read-write 选项会自动连接到主库上,而使用 any 选项,则会根据顺序优先连接到从库上。

2.read-only
把连接属性改为其他,如 read-only

[xdb@localhost ~]$ psql  postgres://192.168.26.26:5432,192.168.26.28:5433/postgres?target_session_attrs=read-only  -U xdb -c  select inet_server_addr() 
psql: invalid target_session_attrs value:  read-only 
[xdb@localhost ~]$

提示无效的属性值,实际上只支持 read-write 和 any 两个选项。

“PostgreSQL 中 Failover 的配置方法是什么”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注丸趣 TV 网站,丸趣 TV 小编将为大家输出更多高质量的实用文章!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-07-26发表,共计1989字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)