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 关系型数据库 数据库连接
|
7月前
|
关系型数据库 Shell C#
PostgreSQL修改最大连接数
在使用PostgreSQL时,可能遇到“too many clients already”错误,这是由于默认最大连接数(100)不足。要增加此数值,需修改`postgresql.conf`中的`max_connections`参数
320 5
|
7月前
|
SQL 关系型数据库 MySQL
mysql too many connections 解决
mysql too many connections 解决
259 4
|
SQL 关系型数据库 MySQL
MySQL 默认最大连接数是多少?
上午刚工作10分左右,同事说在使用jira时出现问题,具体截图如下: 通过上图的报错信息:定位为mysql数据库连接数的问题
449 0
MySQL 默认最大连接数是多少?
|
关系型数据库 MySQL Linux
MySQL: 修改最大连接数
MySQL: 修改最大连接数
578 0
|
SQL 关系型数据库 数据库连接
|
Web App开发 关系型数据库 数据库
PostgreSQL recovery.conf 配置文件整合到 postgresql.conf
标签 PostgreSQL , recovery.conf , postgresql.conf 背景 PostgreSQL 12版本以前,配置PostgreSQL数据库恢复、流复制STANDBY,都需要配置recovery.conf,如果要修改配置,需要重启数据库。
3153 0
|
关系型数据库 Shell 数据库
Postgresql服务器配置-设置参数
Postgresql服务器配置-设置参数 1、Parameter Names and Values 每个参数都有一个值。所有参数名称都不区分大小写。每个参数值都采用五种类型之一: 布尔、字符串、整数、浮点或枚举 (枚举) Boolean:值可以是off, true, false, yes, no, 1, 0 (区分大小写)或其中一个值的任何明确前缀。
2814 0
|
关系型数据库 数据库 运维
PGPool使用限制
PGPool使用限制
1586 0
|
网络协议 关系型数据库 网络安全
PostgreSQL 10.1 手册_部分 III. 服务器管理_第 20 章 客户端认证_20.1. pg_hba.conf文件
20.1. pg_hba.conf文件 客户端认证是由一个配置文件(通常名为pg_hba.conf并被存放在数据库集簇目录中)控制(HBA表示基于主机的认证)。在initdb初始化数据目录时,它会安装一个默认的pg_hba.conf文件。
1639 0