您的位置:首页 > 数据库

postgreSQL连接池pgbouncer的使用

2015-06-09 12:43 369 查看
转载自: 这里

  前端时间看了看服务器还有多余的内存,由于数据库和程序都在一个服务器上,就想看看有什么提升并发的方法。试用了下pgbouncer ,记录下。

1.源码安装:

https://github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz

$ cd libevent-2.0.21-stable
$ ./configure --prefix=/usr/local/libevent
$ make
$ make install


http://pgfoundry.org/frs/download.php/3393/pgbouncer-1.5.4.tar.gz

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


注意设置libevent 的环境变量不然后面启动 pgbouncer会出错

vi  /ect/profile
export LD_LIBRARY_PATH=/usr/local/libevent/lib:$LD_LIBRARY_PATH


2. pgbouncer配置

  主要两个文件pgbouncer.ini 和userlist.txt文件,可以参考/usr/local/pgbouncer/share/doc/下对应的两个示例文件。

这里看下pgbouncer.ini的配置,其主要说明可以从上面说的路径中查看对应的说明

[databases]
server_main = host=localhost port=5432 dbname=postgres user=postgres password=123456

[pgbouncer]
listen_port = 5433
listen_addr = localhost
auth_type = md5
auth_file = /usr/local/pgbouncer/user.txt
logfile = /usr/local/pgbouncer/pgbouncer.log
pidfile = /usr/local/pgbouncer/pgbouncer.pid
admin_users = postgres
pool_mode = Transaction
ignore_startup_parameters = extra_float_digits
max_client_conn = 1000


  pool_mode 指明了连接池的模型,pgbouncer目前支持三种连接池模型。分别是session, transaction和statment三个级别。

a. session. 会话级链接。只有与当客户端的会话结束时,pgbouncer才会收回已分配的链接
b. transaction 事务级连接。当事务完成后,pgbouncer会回收已分配的链接。也就是说客户端只是在事务中才能独占此链接,非事务的对数据库的请求是没有独享的链接的。
c. statement 语句级链接。任何对数据库的请求完成后,pgbouncer都会回收链接。此种模式下,客户端不能使用事务,否则会造成数据的不一致。


  pgbouncer的默认设置是session链接。

  auth_type和auth_file是bppgbouncer用以完成客户端身份认证。auth_file中保存用户名和密码,根据验证方式(auth_type)的不同,auth_file的内容也有不同。

md5: 基于md5的密码验证,auth_file中需要有普通文本和md5值两种形式的密码;
crypt: 基于crypt的密码验证(man 3 crypt), auth_file必须包含文本密码;
plain: 明文验证方式;
trust: 不进行验证,但auth_file依然需要保存用户名;
any: 也不进行验证,而且auth_file中不需要保存用户名了。但此种方式需要在pg_template1中明确说明用户名进行真实数据库的登录。如: pg_template1 = host=127.0.0.1 user=exampleuser dbname=template1.否则会报错的。


需要说明的是:auth_file中的用户名、密码都必须使用双引号,否则还是报错。

例如:auth_type = md5,那么user.txt,需要同时包括明文和加密码后的账号密码:

“postgres” “123456”

“postgres” “md5a3556571e93b0d20722ba62be61e8c2d”

这里第二行的账号和MD5密码,md5密码为

“md5” + md5(password + username)

或者通过查询数据库中pg_authid获取

在数据库服务器上运行,生成的文件userlist.txt放至配置目录下。

postgres=# \o userlist.txt
postgres=# SELECT rolname,rolpassword from pg_authid where rolname = 'postgres';
postgres=# \o
postgres=# \q


3. pgbouncer启动与说明

  启动pgbouncer ,这里必须以postgresql服务器用户启动,例如postgres。这里需要把pgbouncer的安装目录修改权限为postgres账户。chown -R postgres.postgres /usr/local/pgbouncer. 这样才能采用postgres账号启动。

su postgres
/usr/local/pgbouncer/bin/pgbouncer -d /usr/local/pgbouncer/conf/pgbouncer.ini


  然后通过 postgresql 的psql登录pgbouncer工具的数据库pgbouncer,以便查看pgbouncer工具的状态。

  这里注意连接pgbouncer 要采用pgbouncer.ini配置的pgbouncer节的listen端口:

cd /usr/local/postgresql/bin
su postgres
./psql -h localhost -p 5433 -U postgres pgbouncer


采用show config; 查看pgbouncer的配置,包括pgbouncer.ini配置文件中配置:

pgbouncer=# show config;
key            |               value                | changeable
---------------------------+------------------------------------+------------
job_name                  | pgbouncer                          | no
conffile                  | ../conf/pgbouncer.ini              | yes
logfile                   | /usr/local/pgbouncer/pgbouncer.log | yes
pidfile                   | /usr/local/pgbouncer/pgbouncer.pid | no
listen_addr               | 127.0.0.1                          | no
listen_port               | 5433                               | no
listen_backlog            | 128                                | no
unix_socket_dir           | /tmp                               | no
unix_socket_mode          | 511                                | no
unix_socket_group         |                                    | no
auth_type                 | md5                                | yes
auth_file                 | /usr/local/pgbouncer/user.txt      | yes
pool_mode                 | transaction                        | yes
max_client_conn           | 100                                | yes
default_pool_size         | 20                                 | yes
min_pool_size             | 0                                  | yes
reserve_pool_size         | 0                                  | yes
reserve_pool_timeout      | 5                                  | yes
syslog                    | 0                                  | yes
syslog_facility           | daemon                             | yes
syslog_ident              | pgbouncer                          | yes
user                      |                                    | no
autodb_idle_timeout       | 3600                               | yes
server_reset_query        | DISCARD ALL                        | yes
server_check_query        | select 1                           | yes
server_check_delay        | 30                                 | yes
query_timeout             | 0                                  | yes
query_wait_timeout        | 0                                  | yes
client_idle_timeout       | 0                                  | yes
client_login_timeout      | 60                                 | yes
idle_transaction_timeout  | 0                                  | yes
server_lifetime           | 3600                               | yes
server_idle_timeout       | 600                                | yes
server_connect_timeout    | 15                                 | yes
server_login_retry        | 15                                 | yes
server_round_robin        | 0                                  | yes
suspend_timeout           | 10                                 | yes
ignore_startup_parameters |                                    | yes
disable_pqexec            | 0                                  | no
dns_max_ttl               | 15                                 | yes
dns_zone_check_period     | 0                                  | yes
max_packet_size           | 2147483647                         | yes
pkt_buf                   | 2048                               | no
sbuf_loopcnt              | 5                                  | yes
tcp_defer_accept          | 1                                  | yes
tcp_socket_buffer         | 0                                  | yes
tcp_keepalive             | 1                                  | yes
tcp_keepcnt               | 0                                  | yes
tcp_keepidle              | 0                                  | yes
tcp_keepintvl             | 0                                  | yes
verbose                   | 0                                  | yes
admin_users               | postgres                           | yes
stats_users               |                                    | yes
stats_period              | 60                                 | yes
log_connections           | 1                                  | yes
log_disconnections        | 1                                  | yes
log_pooler_errors         | 1                                  | yes
(57 rows)


采用 show clients;查看连接状态:

pgbouncer=# show clients;
type |   user   | database  | state  |   addr    | port  | local_addr | local_port |    connect_time     |    request_time     |    ptr    | link
------+----------+-----------+--------+-----------+-------+------------+------------+---------------------+---------------------+-----------+------
C    | postgres | pgbouncer | active | 127.0.0.1 | 42782 | 127.0.0.1  |       5433 | 2013-06-13 00:05:19 | 2013-06-13 00:08:52 | 0x935c310 |
(1 row)


  这里应用程序连接数据库的还没有启用,所以只有我通过shell命令psql 的连接。还有 show stats;show list;show pools;等命令查看pgbouncer的状态。

  如果现在想通过pgbouncer连接配置的数据库postgres,通过下面的方法连接:

– 直接连数据库

psql -h localhost -p 5432 -U postgres -d postgres

– 通过pgbouncer连接数据库

psql -h localhost -p 5433 -U postgres -d server_main

4. Java 连接 postgresql生效

  那么要如何使程序连接生效呢? 比如java web程序

  之前我们连接数据库在java 配置文件采用的连接是:

  这里我们改成 jdbc:postgresql://localhost:5433/server_main,和pgbouncer配置文件一致。 账号密码为 user.txt 里面的账号密码对。这里其实就是将连接postgresql 通过pgbouncer来管理。

  通过pgbouncer数据库show clients; 和 server_main 数据库中select count (1) from pg_stat_activity;可以看到,DB的连接大幅下降了,转到pgbouncer了。

参考: https://wiki.postgresql.org/wiki/PgBouncer
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: