首页  :: 新随笔  :: 管理

PostgreSQL连接池-pgbouncer

Posted on 2021-11-30 20:33  高&玉  阅读(2809)  评论(0编辑  收藏  举报

1 介绍

pgbouncer是一个PostgreSQL连接池。任何目标应用程序都可以像连接PostgreSQL服务器一样连接到pgbouncer,并且pgbouncer将创建到实际服务器的连接,或者重用其现有的连接。

pgbouncer的目的是降低打开新连接到PostgreSQL的性能影响。

 

为了不影响连接池的事务语义,pgbouncer在旋转连接时支持几种类型的池:

  • Session pooling:最有礼貌的方法。当客户端连接时,服务器连接将在整个客户端连接期间分配给它。当客户端断开连接时,服务器连接将被放回池中。这是默认方法。
  • Transaction pooling:服务器连接只在事务期间分配给客户端。当PgBouncer注意到事务结束时,服务器连接将被放回池中。
  • Statement pooling:最积极的方法。查询完成后,服务器连接将立即放回池中。在此模式下不允许多语句事务,因为它们会中断。

提示:对于使用了绑定变量的客户端,需使用session pooling,因为session pooling模式中需要保存并复用named prepared statemen的信息。

 

官网:https://www.pgbouncer.org/

pgcouncer下载地址:http://www.pgbouncer.org/downloads/

2 安装

操作系统:CentOS 7.6

 

安装依赖包

[root]# yum install -y openssl openssl-devel libevent libevent-devel

 

源码安装pgbouncer

[root]# wget http://www.pgbouncer.org/downloads/files/1.16.1/pgbouncer-1.16.1.tar.gz
[root]# tar -zxvf pgbouncer-1.16.1.tar.gz
[root]# cd pgbouncer-1.16.1
[root]# ./configure --prefix=/usr/local/
[root]# make && make install

 

或者通过YUM安装

[root]# yum install pgbouncer -y

 

查看pgbouncer版本

[root]# pgbouncer -V
PgBouncer 1.14.0
libevent 2.0.21-stable
adns: evdns2
tls: OpenSSL 1.0.2k-fips  26 Jan 2017

3 配置及使用

3.1 创建pgbouncer.ini

[root]# mkdir -p /etc/pgbouncer
[root]# vi /etc/pgbouncer/pgbouncer.ini
[databases]
template1 = host=10.150.57.9 port=5432 dbname=postgres user=postgres

[pgbouncer]
listen_port = 6432
listen_addr = 10.150.57.9
auth_type = md5
auth_file = /etc/pgbouncer/userlist.txt
logfile = /etc/pgbouncer/pgbouncer.log
pidfile = /etc/pgbouncer/pgbouncer.pid
admin_users = postgres
stats_users = pgmon
server_reset_query = DISCARD ALL
server_check_query = select 1
server_check_delay = 30
max_client_conn = 5000
default_pool_size = 20
reserve_pool_size = 5
dns_max_ttl = 15

 注释:

 template1是通过pgbouncer连接池连接postgres数据库的虚拟库名。

3.2 配置userlist.txt文件

[root]# vi /etc/pgbouncer/userlist.txt
"postgres" "Psswd@123"
"gaoyu" "Passwd@123"

 注释:

只有userlist.txt文件中的用户才能通过pgbouncer连接池连接PostgreSQL。

3.3 启动pgbouncer

[root]# pgbouncer -d /etc/pgbouncer/pgbouncer.ini

3.4 通过pgbouncer连接PG

[postgres]# psql -h 10.150.57.9 -p 6432 -U postgres template1
Password for user postgres: 
psql (9.6.0)
Type "help" for help.

template1=#

 4 管理pgbouncer

 pgbouncer有一个console控制台,可以通过连接pgbouncer数据库进行管理。只有配置了admin_user/admin_users的用户才能登录,或者auth_type=any则代表任何用户都可以登录console。

 

连接console

[postgres]# psql -p 6432 -U postgres pgbouncer
Password for user postgres: 
psql (9.6.0, server 1.16.1/bouncer)
Type "help" for help.

pgbouncer=# 

 

重新加载pgbouncer.ini

pgbouncer=# reload

 

 show users

pgbouncer=# show users;
   name    | pool_mode 
-----------+-----------
 pgbouncer | 
 postgres  | 

 

show databases

pgbouncer=# show databases;
-[ RECORD 1 ]-------+------------
name                | pgbouncer
host                | 
port                | 6432
database            | pgbouncer
force_user          | pgbouncer
pool_size           | 2
min_pool_size       | 0
reserve_pool        | 0
pool_mode           | statement
max_connections     | 0
current_connections | 0
paused              | 0
disabled            | 0
-[ RECORD 2 ]-------+------------
name                | template1
host                | 10.150.57.9
port                | 5432
database            | postgres
force_user          | postgres
pool_size           | 20
min_pool_size       | 0
reserve_pool        | 0
pool_mode           | 
max_connections     | 0
current_connections | 0
paused              | 0
disabled            | 0