postgresql数据库链接池pgbouncer

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

1.源码安装:

wget 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

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

$ cd pgbouncer-1.5.4
$ ./configure --prefix=/usr/local/pgbouncer/ --with-libevent=/usr/local/libevent/
$ make 
$ make install
注意设置libevent 的环境变量否则后面启动 pgbouncer会出错
vim /ect/profile

export LD_LIBRARY_PATH=/usr/local/libevent/lib:$LD_LIBRARY_PATH

2.pgbouncer配置

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

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

[databases]
server_main = host=localhost port=5432 dbname=booksair user=postgres password=12
3456 connect_query='SELECT 1'
 
[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三个级别。 web

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

pgbouncer的默认设置是session连接。 sql

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

  • 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,须要同时包括明文和加密码后的帐号密码: vim

"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的状态。

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了。 

相关文章
相关标签/搜索