pgsql + pgbouncer高并发配置

来源:互联网 发布:算法博弈论中文版 pdf 编辑:程序博客网 时间:2024/06/05 03:35

pgbouncer 介绍

PGBouncer是一个轻量级的针对PostgreSQL的数据库连接池工具,能够给客户端提供一个统一的链接视图。
PgBouncer的作用 
    a.PgBouncer可以在后端数据库和前端应用间建立连接的桥梁,由PgBouncer去处理和后端数据库的连接关系。 
    b.对客户端连接进行限制,预防过多或者恶意的连接请求。 
PgBouncer的特点 
    a.内存消耗低(默认为2k/连接),因为Bouncer不需要每次都接受完整的数据包 
    b.可以把不同的数据库连接到一个机器上,而对客户端保持透明 
    c.支持在线的重新配置而无须重启 
    d.仅支持V3协议,因此后端版本须>=7.4 (引自pgsqldb中文站)

pgbouncer安装

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

$ tar -zxvf 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

$ tar -zxvf 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会出错
$ vim /etc/profileexport LD_LIBRARY_PATH=/usr/local/libevent/lib:$LD_LIBRARY_PATH

pgbouncer 配置

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

复制这两个文件到/usr/local/pgbouncer/目录下面.

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

[databases]server_main = host=localhost port=5432 dbname=booksair user=postgres password=123456 connect_query='SELECT 1' [pgbouncer]listen_port = 6432listen_addr = localhostauth_type = md5auth_file = /usr/local/pgbouncer/userlist.txtlogfile = /usr/local/pgbouncer/pgbouncer.logpidfile = /usr/local/pgbouncer/pgbouncer.pidadmin_users = postgrespool_mode = sessionignore_startup_parameters = extra_float_digitsmax_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

更多的参数说明请查看这里。

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/pgbouncer.ini
然后通过 postgresql 的psql登录pgbouncer工具的数据库pgbouncer,以便查看pgbouncer工具的状态。

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

xxxx-MacBook-Pro:~ xxxx$ psql -h 192.168.3.104 -p 6432 -U postgres server_mainPassword for user postgres: psql (9.5.1)Type "help" for help.pgsql=# help


php pdo连接pgbouncer


$db = new PDO( "pgsql:host=localhost;port=6432;dbname=server_main", $username, $password);


0 0
原创粉丝点击