pgbouncer使用及配置介绍

简介:

介绍

postgres使用fork进程的方式来处理每个连接请求,本身没有连接池的概念,所以在有大量连接的业务场景中(特别是短连接较多),会在前端架设一个连接池。
目前pg主流的连接池主要有pgpool和pgbouncer。而相对于pgpool,pgbouncer较为轻量级,支持的功能也相对较少。

对比pgpool

相对与pgpool,pgbouncer对资源的需求更小,如果你仅仅只需要一个连接池的功能,选择pgbouncer是正确的。但是如果你还需要一下故障切换,负载均衡,同步的功能,pgpool更适合。

安装

官网下载源码包:

$ ./configure --prefix=/usr/local --with-libevent=libevent-prefix
$ make
$ make install

注意:需要安装libevent-devel openssl-devel两个依赖包。

三种连接模式

Session pooling
Most polite method. When client connects, a server connection will be assigned to it for the whole duration the client stays connected. When the client disconnects, the server connection will be put back into the pool. This is the default method.

Transaction pooling
A server connection is assigned to client only during a transaction. When PgBouncer notices that transaction is over, the server connection will be put back into the pool.

Statement pooling
Most aggressive method. The server connection will be put back into pool immediately after a query completes. Multi-statement transactions are disallowed in this mode as they would break.

区分这几个模式是何时在回收连接,在会话结束后回收,事务结束后回收,sql语句执行之后回收。默认的选项是session。建议还是修改成transaction比较好,具体原因可以参考这篇文章

example config

[databases]
template1 = host=127.0.0.1 port=5432 dbname=template1

[pgbouncer]
listen_port = 6543
listen_addr = 127.0.0.1
auth_type = md5
auth_file = users.txt
logfile = pgbouncer.log
pidfile = pgbouncer.pid
admin_users = someuser

创建文件pgbouncer.ini
其中databases地址写的是实际的postgresql连接地址
第二个是pgbouncer的配置,登陆用户帐号密码需要写在另一个文件users.txt里面。
格式:

"someuser" "same_password_as_in_server"

启动

pgbouncer -d pgbouncer.ini
会在当前目录下生成log和pid文件,其中pid文件记录的是pgbouncer的pid,停止程序时直接用kill。

连接到数据库

psql -p 6543 -U someuser template1

连接到pgbouncer

psql -p 6543 -U someuser pgbouncer

这个模式下可以查看一些pgbouncer参数信息。
show help
show stats
show servers
show clients
show pools

链接

link
link
link

相关文章
|
关系型数据库 数据库 文件存储
|
关系型数据库 MySQL 数据库
|
运维 监控 关系型数据库
【一文搞懂PGSQL】7. PostgreSQL + repmgr + witness 高可用架构
该文档介绍了如何构建基于PostgreSQL的高可用架构,利用repmgr进行集群管理和故障转移,并引入witness节点增强网络故障检测能力。repmgr是一款轻量级的开源工具,支持一键部署、自动故障转移及分布式节点管理。文档详细描述了环境搭建步骤,包括配置postgresql参数、安装与配置repmgr、注册集群节点以及配置witness节点等。此外,还提供了故障手动与自动切换的方法及常用命令,确保集群稳定运行。
|
SQL 关系型数据库 MySQL
MySQL主从:延时从库恢复全解
MySQL主从:延时从库恢复全解
|
监控 关系型数据库 数据库
RDS PostgreSQL内置连接池PgBouncer
2023年7月,阿里云RDS PostgreSQL支持内置数据库连接池PgBouncer,本篇文章从以下角度探讨PgBouncer:1. PgBouncer是什么;2. 应用场景 ;3. 性能对比;4. 如何使用;5. 总结
|
SQL 关系型数据库 中间件
postgresql从入门到精通 - 第35讲:中间件PgBouncer部署
postgresql技术大讲堂,从入门到精通 - 第35讲:中间件PgBouncer部署
497 1
|
负载均衡 关系型数据库 网络安全
Pgpool-II实现高可用+读写分离+负载均衡(二)---- 配置篇
Pgpool-II是一款工作在PostgreSQL服务器和PostgreSQL数据库客户端之间的中间件。提供了连接池、复制、负载均衡、限制过多连接、看门狗、查询缓存等功能。本篇介绍详细配置。
|
关系型数据库 API 数据库
基于Patroni的PostgreSQL高可用环境部署
在部署PostgreSQL到生产环境中时,选择适合的高可用方案是一项必不可少的工作。本文介绍基于Patroni的PostgreSQL高可用的部署方法,供大家参考。
7670 1
|
SQL 弹性计算 算法
PostgreSQL 普通表在线转换为分区表 - online exchange to partition table
标签 PostgreSQL , 分区表 , 在线转换 背景 非分区表,如何在线(不影响业务)转换为分区表? 方法1,pg_pathman分区插件 《PostgreSQL 9.5+ 高效分区表实现 - pg_pathman》 使用非堵塞式的迁移接口 partition_table_concurrently( relation REGCLASS,
2959 0
|
存储 负载均衡 监控
【数据库架构】使用pgpool II的PostgreSQL高可用性
在本文中,我们讨论了使用pgpool II(PostgreSQL开源扩展之一)的高可用性系统设置。它是我们的解决方案之一,可以满足这两个要求:高可用性和可扩展性。