2019独角兽企业重金招聘Python工程师标准>>>

今天在虚拟机上整理了下pgbouncer的安装使用过程,记录如下。 
 说明:pgbouncer是一款轻量级针对postgresql的数据库连接工具,可以对客户端的连接做限制,防止恶意连接,另外也可以减少数据库的实际连接数,从而减少数据库的开销。

环境:

VMWARE 8
CentOS 5.7 
PG 9.1.2
pgbouncer 1.5.2
libevent-2.0.19

一.安装

1.下载pgbouncer的安装包
http://pgfoundry.org/frs/?group_id=1000258&release_id=1952,本次下载的安装包是pgbouncer-1.5.2.tar.gz2.下载libevent包
http://monkey.org/~provos/libevent/,见pgbouncer源码包中的Readme
这玩意也是个好东西,在很多地方用到3.安装libevent
$ cd libevent-2.0.19-stable
$ ./configure --prefix=/home/postgres/libevent
$ make
$ make install4.安装pgbouncer
$ cd pgbouncer-1.5.2
$ ./configure --prefix=/home/postgres/pgbouncer/ --with-libevent=/home/postgres/libevent/
$ make
$ make install

查看pgbouncer是否安装成功,可以通过查看config.log中最后的返回值exit来确认,0是成功1是失败. 
二.配置 
1.配置pgbouncer的cfg文件

[postgres@localhost config]$ pwd
/home/postgres/pgbouncer/config
[postgres@localhost config]$ more pgbouncer.ini
[databases]
f_game = host=127.0.0.1 port=1233 dbname=test_db user=test password=test[pgbouncer]
listen_port = 1999
listen_addr = 127.0.0.1
auth_type = md5
auth_file = /home/postgres/pgbouncer/user.txt
logfile = /home/postgres/pgbouncer/pgbouncer.log
pidfile = /home/postgres/pgbouncer/pgbouncer.pid
admin_users = u_kenyon
pool_mode = Transaction

2.配置用户密码文件users.txt

[postgres@localhost pgbouncer]$ pwd
/home/postgres/pgbouncer
[postgres@localhost pgbouncer]$ more user.txt
"u_kenyon" "123"

三.启动 
1.启动命令

[postgres@localhost config]$ pgbouncer -d pgbouncer.ini
2012-08-21 00:29:55.573 4247 LOG File descriptor limit: 1024 (H:1024), max_client_conn: 100, max fds possible: 130

2.查看日志 
tail -f /home/postgres/pgbouncer/pgbouncer.log

3.登录

[postgres@localhost pgbouncer]$ psql -h 127.0.0.1 -p 1999 -U u_kenyon f_game
Password for user u_kenyon:
psql (9.1.2)
Type "help" for help.f_game=> \dList of relationsSchema |   Name   | Type  |  Owner
--------+----------+-------+----------public | t_kenyon | table | postgrespublic | test     | table | testpublic | test2    | table | postgres
(3 rows)f_game=> \q

4.参数使用例子 
 连接pgbouncer本身的DB

[postgres@localhost config]$ psql -h 127.0.0.1 -p 1999 -U u_kenyon pgbouncer Password for user u_kenyon:
psql (9.1.2, server 1.5.2/bouncer)
WARNING: psql version 9.1, server version 1.5.Some psql features might not work.
Type "help" for help.pgbouncer=# show help;
NOTICE:  Console usage
DETAIL:  SHOW HELP|CONFIG|DATABASES|POOLS|CLIENTS|SERVERS|VERSIONSHOW STATS|FDS|SOCKETS|ACTIVE_SOCKETS|LISTS|MEMSHOW DNS_HOSTS|DNS_ZONESSET key = argRELOADPAUSE []RESUME []KILL          SUSPENDSHUTDOWN
SHOW
pgbouncer=# show config;key            |                 value                  | changeable
---------------------------+----------------------------------------+------------job_name                  | pgbouncer                              | noconffile                  | pgbouncer.ini                          | yeslogfile                   | /home/postgres/pgbouncer/pgbouncer.log | yespidfile                   | /home/postgres/pgbouncer/pgbouncer.pid | nolisten_addr               | 127.0.0.1                              | nolisten_port               | 1999                                   | nolisten_backlog            | 128                                    | nounix_socket_dir           | /tmp                                   | nounix_socket_mode          | 511                                    | nounix_socket_group         |                                        | noauth_type                 | md5                                    | yesauth_file                 | /home/postgres/pgbouncer/user.txt      | yespool_mode                 | transaction                            | yesmax_client_conn           | 100                                    | yesdefault_pool_size         | 20                                     | yesmin_pool_size             | 0                                      | yesreserve_pool_size         | 0                                      | yesreserve_pool_timeout      | 5                                      | yessyslog                    | 0                                      | yessyslog_facility           | daemon                                 | yessyslog_ident              | pgbouncer                              | yesuser                      |                                        | noautodb_idle_timeout       | 3600                                   | yesserver_reset_query        | DISCARD ALL                            | yesserver_check_query        | select 1                               | yesserver_check_delay        | 30                                     | yesquery_timeout             | 0                                      | yesquery_wait_timeout        | 0                                      | yesclient_idle_timeout       | 0                                      | yesclient_login_timeout      | 60                                     | yesidle_transaction_timeout  | 0                                      | yesserver_lifetime           | 3600                                   | yesserver_idle_timeout       | 600                                    | yesserver_connect_timeout    | 15                                     | yesserver_login_retry        | 15                                     | yesserver_round_robin        | 0                                      | yessuspend_timeout           | 10                                     | yesignore_startup_parameters |                                        | yesdisable_pqexec            | 0                                      | nodns_max_ttl               | 15                                     | yesdns_zone_check_period     | 0                                      | yespkt_buf                   | 2048                                   | nosbuf_loopcnt              | 5                                      | yestcp_defer_accept          | 1                                      | yestcp_socket_buffer         | 0                                      | yestcp_keepalive             | 1                                      | yestcp_keepcnt               | 0                                      | yestcp_keepidle              | 0                                      | yestcp_keepintvl             | 0                                      | yesverbose                   | 0                                      | yesadmin_users               | u_kenyon                               | yesstats_users               |                                        | yesstats_period              | 60                                     | yeslog_connections           | 1                                      | yeslog_disconnections        | 1                                      | yeslog_pooler_errors         | 1                                      | yes
(56 rows)

本次测试环境修改的参数不多,还有其他参数如server_idle_timeout,max_connections也比较重要,后期再写。 
 使用reload可以不用重启pgbouncer而使修改的参数生效

pgbouncer=# reload;
RELOAD
pgbouncer=#

5.使用效果 
 在连接池端看连接数:

pgbouncer=# show clients;type |   user   | database  | state  |   addr    | port  | local_addr | local_port |    connect_time     |    request_time     |   ptr    | link
------+----------+-----------+--------+-----------+-------+------------+------------+---------------------+---------------------+----------+------C    | u_kenyon | f_game    | active | 127.0.0.1 | 59555 | 127.0.0.1  |       1999 | 2012-08-21 00:32:19 | 2012-08-21 01:47:14 | 0xbc4670 | C    | u_kenyon | f_game    | active | 127.0.0.1 | 59558 | 127.0.0.1  |       1999 | 2012-08-21 00:32:35 | 2012-08-21 02:09:04 | 0xbc4940 | C    | u_kenyon | f_game    | active | 127.0.0.1 | 59573 | 127.0.0.1  |       1999 | 2012-08-21 02:09:08 | 2012-08-21 02:09:10 | 0xbc43a0 | C    | u_kenyon | f_game    | active | 127.0.0.1 | 59576 | 127.0.0.1  |       1999 | 2012-08-21 02:09:44 | 2012-08-21 02:09:46 | 0xbc4aa8 | C    | u_kenyon | f_game    | active | 127.0.0.1 | 59578 | 127.0.0.1  |       1999 | 2012-08-21 02:09:58 | 2012-08-21 02:10:10 | 0xbc4c10 | C    | u_kenyon | pgbouncer | active | 127.0.0.1 | 59570 | 127.0.0.1  |       1999 | 2012-08-21 02:01:58 | 2012-08-21 02:10:12 | 0xbc4508 | C    | u_kenyon | pgbouncer | active | 127.0.0.1 | 59574 | 127.0.0.1  |       1999 | 2012-08-21 02:09:17 | 2012-08-21 02:09:29 | 0xbc47d8 |
(7 rows)pgbouncer=# show fds;fd |  task  |   user   | database |   addr    | port  |       cancel        | link | client_encoding | std_strings | datestyle |  timezone
----+--------+----------+----------+-----------+-------+---------------------+------+-----------------+-------------+-----------+------------8 | pooler |          |          | 127.0.0.1 |  1999 |                   0 |    0 |                 |             |           | 9 | pooler |          |          | unix      |  1999 |                   0 |    0 |                 |             |           | 13 | client | u_kenyon | f_game   | 127.0.0.1 | 59555 | 8879718699069493200 |    0 | UTF8            | on          | ISO, MDY  | US/Pacific15 | client | u_kenyon | f_game   | 127.0.0.1 | 59558 | 2718240829565746463 |    0 | UTF8            | on          | ISO, MDY  | US/Pacific14 | client | u_kenyon | f_game   | 127.0.0.1 | 59573 | 9376066101244793463 |    0 | UTF8            | on          | ISO, MDY  | US/Pacific16 | client | u_kenyon | f_game   | 127.0.0.1 | 59576 | 9911177087544032756 |    0 | UTF8            | on          | ISO, MDY  | US/Pacific17 | client | u_kenyon | f_game   | 127.0.0.1 | 59578 | 7987308370302986343 |    0 | UTF8            | on          | ISO, MDY  | US/Pacific12 | server | test     | f_game   | 127.0.0.1 |  1233 |      20770794712503 |    0 | UTF8            | on          | ISO, MDY  | US/Pacific
(8 rows)

在DB端看连接数:

f_game=> select count(1) from pg_stat_activity;count
-------2
(1 row)

只有2个,看来池的效果已经初显成效,可预计高并发的时候池连接的可重复性使用,实际连接到DB端的进程并不是很多.

6.pool_mode说明 
有三种模式:session,transaction,statement 
session是默认的模式,每开启一个进程,DB端也会开启一个新的进程 
transaction是基于事务模式的 
statement是基于每个查询的,开启此模式不适合执行事务,会报错 
 原文见安装后pgbouncer中的usage.txt,本次文件地址 /home/postgres/pgbouncer/share/doc/pgbouncer

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.

四.总结 
 1.本次在虚拟机上测试,使用的模式是修改后的transcation模式,在服务端开启6个psql进程,但在数据库端实际只有1个连接,池的效果可见一斑,对并发高的访问,可以有效减少DB端的连接进程,减少消耗。 
 2.测试过程中,假如第一次访问被拒绝以后(不是密码不对),后续访问会直接拒绝,此举可以有效降低攻击性的访问。

转载于:https://my.oschina.net/mickelfeng/blog/1031142

PostgreSQL连接池pgbouncer的使用相关推荐

  1. postgresql数据库连接池pgbouncer的搭建

    最近有些项目我们是采用PostgreSQL进行后端数据库,由于要对客户端的连接使用类型和连接数进行控制,因此我们采用PgBouncer来进行实施. PgBouncer相关的基本文档pgsqldb中文站 ...

  2. Postgresql数据库连接池

    连接池的作用及原理 正常访问数据库的过程中,每次访问都需要创建数据库的连接,这会消耗大量的资源:连接池的就是为数据库连接建立一个"缓冲区",预先在缓冲池中放入一定数量的连接对象,当 ...

  3. 内置PostgreSQL多线程连接池Odyssey

    为什么80%的码农都做不了架构师?>>>    1.Worker PG支持内置Worker,可以自定义伴随主进程一起启停的守护进程. 详细文档:Background Worker P ...

  4. Druid连接池报错:org.postgresql.util.PSQLException: FATAL: too many connections for role “*********“

    一 错误描述 1 负责的BI中的数据数据可视化项目一直运行的比较稳定,突然早上用户反馈,报表查询非常慢,非常卡,而且有些网页直接打不开(单体应用,没有降级和熔断). 2 没一会CPU就报警了,CPU使 ...

  5. springboot添加多数据源连接池并配置Mybatis

    springboot添加多数据源连接池并配置Mybatis 转载请注明出处:https://www.cnblogs.com/funnyzpc/p/9190226.html May 12, 2018  ...

  6. php+sqlrelay+mysql实现连接池及读写负载均衡

    本文主要介绍sqlrelay的配置安装.通过其性能和一些具体环境的测试来帮助开发者应用在相应的场合中去应付大并发的mysql数据库连接. 什么是sqlrelay? Sqlrelay是一个开源的数据库连 ...

  7. Druid 连接池 JDBCUtils 工具类的使用

    Druid工具介绍 它不仅仅是一个数据库连接池,它还包含一个ProxyDriver,一系列内置的JDBC组件库,一个SQL Parser. 支持所有JDBC兼容的数据库,包括Oracle.MySQL. ...

  8. druid连接池mysql5.7_Spring Boot 使用Druid连接池整合Mybatis-Plus连接Mysql数据库

    一.连接池 在普通的数据库访问程序中,客户程序得到的连接对象是物理连接,调用连接对象的close()方法将关闭连接,而采用连接池技术,客户程序得到的连接对象是连接池中物理连接的一个句柄,调用连接对象的 ...

  9. 性能优化之数据库和数据源连接池配置

    什么?!数据库连接拿不到? 今天在公司对系统进行压测,由于我的sit和dev和uat环境都是用的用的是一个数据库服务器,我让用户在的sit进行压测,分别是单线程测试,并发测试,但是用户一开始测,我的三 ...

最新文章

  1. 【数据挖掘】卷积神经网络 ( 池化 | 丢弃 | 批量规范化 | 卷积神经网络完整流程示例 | 卷积 | 池化 | 全连接 | 输出 | 卷积神经网络总结 )
  2. Windows和Virtualbox虚拟机之间拷贝文件
  3. 响应格式html,设置响应格式的HTML邮件
  4. jupiter 依赖_Jupiter 介绍
  5. 右边菜单_AI基础教程65:使用文字菜单编辑文字(七)查找字体
  6. 初识Xen-CentOS5.8_x64位系统安装xen文档(-)
  7. 印象笔记不同步(EDAMSystemException: errorCode=INTERNAL_ERROR)
  8. 正则表达式基础知识(转)
  9. 【文文殿下】[BZOJ4008] [HNOI2015] 亚瑟王
  10. freemarker,自定义指令
  11. 开始我们的Snippets!
  12. 【有图有真相】全国软考高级三连冠感悟
  13. 光纤接口类型及光纤收发器指示灯图解
  14. 同频切换的事件_目前现网中,LTE同频切换主要是通过A5事件进行触发
  15. jp.ne.so_net.ga2.no_ji.jcom.JComException: createInstance() failed HRESULT=0x800401F3L
  16. word2016毕业论文不同章节设置不同页眉方法
  17. Scala 034 特质trait
  18. 有序的uuid(32位)
  19. java 代码覆盖率
  20. ADSL拨号密码如何修改?

热门文章

  1. java英文字符编码_Java邮件编码非英文字符
  2. 高斯拟合原理_看得见的高斯过程:这是一份直观的入门解读
  3. mysql三次握手_一文彻底搞懂 TCP三次握手、四次挥手过程及原理
  4. 【java】关于面向对象优点的个人理解
  5. JDK11使用IDEA,配置JavaFX
  6. linux esd转iso,window_Win10 TH2正式版10586官方ESD映像怎么转换成ISO镜像?,今天phpstudy分享了Win10 TH2(Build - phpStudy...
  7. 文件流能转换格式吗_amr转换mp3格式文件
  8. vue注册新节点_vue怎么重新组装slots节点
  9. 8 iOS中KVO 的本质
  10. subst将文件夹目录虚拟成虚拟磁盘