最近一些事情提醒了我,关于讲技术的事情,讲技术如果只是讲技术讲无法满足现在新的学习知识的形式,在知识泛滥的今天,有用的东西叫知识,没有用的叫什么我就不大清楚了。或许也这也是很多技术类的文章阅读量不高的原因,我们从自身找原因,还是东西没有打到需求上,所以没有需求就没有人看。
今天先说一个需求,然后再说技术。需求来自于一个同学,他们单位的PostgreSQL承载了多个逻辑库,后续数据积累业务发展一个库无法再满足业务的需求,就需要进行分库,但原来的乙方已经不在了,而乙方开发的程序中配置互访,WEB界面只能填写一个IP地址,或者DNS地址。所以如果分库到不同的实例,需要两个或多个实例使用一个IP。
不少由乙方开发的应用程序可能早期在业务量或者量不大的情况下一个物理库的确可以支持,可到了单库无法支持的时候,我们就需要考虑如何进行拆库了。这个案例基于开发的因素只能有一个IP,在多个物理实例的情况下,怎么处理。
需求分析
这里操作的方法其实我们还真有,就是我们非常熟悉的pgbouncer,通过pgbouncer来进行处理,实现业务访问pgbouncer代理,而我们的代理进行多个实例的集成。
安装pgbouncer
root@pg16 pgbouncer-1.24.1]# sudo yum install libevent-devel
Last metadata expiration check: 0:11:56 ago on Mon 30 Jun 2025 03:41:31 AM EDT.
Dependencies resolved.
===========================================================================================================================================================================
Package Architecture Version Repository Size
===========================================================================================================================================================================
Installing:
libevent-devel x86_64 2.1.8-5.el8 appstream 103 k
Transaction Summary
===========================================================================================================================================================================
Install 1 Package
Total download size: 103 k
Installed size: 418 k
Is this ok [y/N]: y
Downloading Packages:
libevent-devel-2.1.8-5.el8.x86_64.rpm 348 kB/s | 103 kB 00:00
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total 87 kB/s | 103 kB 00:01
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : libevent-devel-2.1.8-5.el8.x86_64 1/1
Running scriptlet: libevent-devel-2.1.8-5.el8.x86_64 1/1
Verifying : libevent-devel-2.1.8-5.el8.x86_64 1/1
Installed:
libevent-devel-2.1.8-5.el8.x86_64
Complete!
[root@pg16 pgbouncer-1.24.1]# ./configure --prefix=/usr/local
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target host type... unix
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether the compiler supports GNU C... yes
checking whether gcc accepts -g... yes
checking for gcc option to enable C11 features... none needed
[root@pg16 pgbouncer-1.24.1]# make install
INSTALL pgbouncer /usr/local/bin
INSTALL README.md /usr/local/share/doc/pgbouncer
INSTALL NEWS.md /usr/local/share/doc/pgbouncer
INSTALL etc/pgbouncer-minimal.ini /usr/local/share/doc/pgbouncer
INSTALL etc/pgbouncer.ini /usr/local/share/doc/pgbouncer
INSTALL etc/pgbouncer.service /usr/local/share/doc/pgbouncer
INSTALL etc/pgbouncer.socket /usr/local/share/doc/pgbouncer
INSTALL etc/userlist.txt /usr/local/share/doc/pgbouncer
INSTALL doc/pgbouncer.1 /usr/local/share/man/man1
INSTALL doc/pgbouncer.5 /usr/local/share/man/man5
[root@pg16 pgbouncer-1.24.1]# su - postgres
Last login: Thu Jun 19 03:21:30 EDT 2025 on pts/0
[postgres@pg16 ~]$
我们将核心的两个配置文件 userlist.txt 和 pgboucner.ini两个部分,拷贝/etc/ 目录下。
[pgbouncer]
listen_addr = 0.0.0.0
listen_port = 6432
logfile = /pgdata/pgbouncer.log
pidfile = /pgdata/pgbouncer.pid
pool_mode = transaction
[databases]
database1 = host=192.168.198.100 port=5432 user=db1 client_encoding=UNICODE connect_query='select 1'
database2 = host=192.168.198.101 port=5432 user=db2 client_encoding=UNICODE connect_query='select 1'
配置信息的详情就不在详述了,PGbouncer不会还玩PG的人比较少,这里嘱咐几句
1 pool_mode:建议一律选择transaction
2 pgbouncer:出来可以应对PG并发,其他功能大家应该知晓
1 整合链接到一个地址,就如今天的案例
2 抵御DDOS 攻击,暴露PGBOUNCER的地址不要暴露数据库的地址给程序,有利于出现问题后的解决和应对
3 pgbouncer 单点故障问题的解决,这就与部署的模式和数量有关了
4 解决了PG链接重启和删除的问题
下面我们配置pgboucner.ini 和userlists.txt后,进行登录发现不成功,最近私信有的同学问这个问题的多。
原因
因为问的人多,原因主要就是在于新版本PG的问题,新版本的密码是sha-256,你要注意pgbouncer本身是否支持,一般支持MD5,所以你必须调整你的数据库的密码加密形式。同时之前通过sha-256添加后的密码是无法再被pgbouncer所识别的,所以必须重建账号。
[postgres@pg16 pgdata]$
[postgres@pg16 pgdata]$
[postgres@pg16 pgdata]$ psql -Udb1 -p6432 -h 192.168.198.100 -ddb1
Password for user db1:
psql (17.4)
Type "help" for help.
db1=# exit
[postgres@pg16 pgdata]$ psql -Udb2 -p6432 -h 192.168.198.100 -ddb2
Password for user db2:
这里第二个连不上是正确的,因为在实验环境里面没有192.168.198.101这个地址,在pgbouncer.ini配置文件里面的地址是无法连接所以卡主了。
总结:在工具的使用中,我们大多根据工具的厂商或厂家的预先设计来去使用产品,其实作为用户我们应该根据自己的工作经验,发现更多的产品使用中可以发掘的功能解决我们遇到的问题。
本文分享自 AustinDatabases 微信公众号,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文参与 腾讯云自媒体同步曝光计划 ,欢迎热爱写作的你一起参与!