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

相关文章
|
SQL 关系型数据库 数据库连接
|
弹性计算 测试技术 数据库
MogDB修改服务器IP
MogDB修改服务器IP
180 0
MogDB修改服务器IP
|
安全 算法 网络协议
PostgreSQL集群篇——pg_hba.confg的配置文件解析
在我们生产环境使用PostgreSQL时,通常需要一定的安全限定,而pg_hba.conf就是我们比较常用的限定方式之一,其配置可以进行限定具体IP、IP段、可访问的数据库、可访问的账号、访问时使用的验证方式。下面我们就对于该配置文件进行一次简单的解析,便于我们日常配置使用。
275 0
PostgreSQL集群篇——pg_hba.confg的配置文件解析
|
SQL 关系型数据库 MySQL
MySQL 默认最大连接数是多少?
上午刚工作10分左右,同事说在使用jira时出现问题,具体截图如下: 通过上图的报错信息:定位为mysql数据库连接数的问题
394 0
MySQL 默认最大连接数是多少?
|
SQL 关系型数据库 数据库连接
|
Web App开发 关系型数据库 数据库
PostgreSQL recovery.conf 配置文件整合到 postgresql.conf
标签 PostgreSQL , recovery.conf , postgresql.conf 背景 PostgreSQL 12版本以前,配置PostgreSQL数据库恢复、流复制STANDBY,都需要配置recovery.conf,如果要修改配置,需要重启数据库。
3042 0
|
关系型数据库 Shell 数据库
Postgresql服务器配置-设置参数
Postgresql服务器配置-设置参数 1、Parameter Names and Values 每个参数都有一个值。所有参数名称都不区分大小写。每个参数值都采用五种类型之一: 布尔、字符串、整数、浮点或枚举 (枚举) Boolean:值可以是off, true, false, yes, no, 1, 0 (区分大小写)或其中一个值的任何明确前缀。
2740 0
|
关系型数据库 MySQL
haproxy mysql实例配置
--------------------------------------------------------------------- Global settings code by www.
1042 0
|
监控
zabbix server配置参数调优
StartPollers=160 StartPollersUnreachable=80 StartTrappers=20 StartPingers=100 StartDiscoverers=120 CacheSize=1024M StartDBSync...
1458 0
|
网络安全 应用服务中间件
Confluence 6 LDAP 连接池配置参数
初始连接池大小(Initial Pool Size) 当初始化 LDAP 连接池的时候初始化创建的 LDAP 连接数量。
1218 0