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 关系型数据库 数据库连接
|
2月前
|
存储 缓存 关系型数据库
Mysql/etc/my.cnf参数详解
以上只是 `/etc/my.cnf`中的部分参数,实际上,`/etc/my.cnf`中的参数非常多,可以根据具体的应用需求进行调整。
28 0
|
10月前
|
安全 关系型数据库 MySQL
MySQL my.cnf参数配置优化详解
MySQL my.cnf参数配置优化详解
144 0
|
监控 关系型数据库 MySQL
MySQL系统参数
1. auto_increment_increment  和  auto_increment_offset  (重)     这两个参数用于高可用中M-M复制。auto_increment_increment控制AUTO_INCREMENT列的增量,auto_increment_offset确定AUTO_INCREMENT列值的起点。
1234 0
|
SQL 关系型数据库 数据库连接
|
Web App开发 关系型数据库 数据库
PostgreSQL recovery.conf 配置文件整合到 postgresql.conf
标签 PostgreSQL , recovery.conf , postgresql.conf 背景 PostgreSQL 12版本以前,配置PostgreSQL数据库恢复、流复制STANDBY,都需要配置recovery.conf,如果要修改配置,需要重启数据库。
3053 0
|
关系型数据库 数据库 运维
PGPool使用限制
PGPool使用限制
1566 0
|
关系型数据库 MySQL
haproxy mysql实例配置
--------------------------------------------------------------------- Global settings code by www.
1045 0
|
监控 关系型数据库
|
关系型数据库 MySQL 测试技术