postgresql数据库连接池pgbouncer
前端时间看了看服务器还有多余的内存,由于数据库和程序都在一个服务器上,就想看看有什么提升并发的方法。试用了下pgbouncer,记录下。 1.源码安装:wgethttps://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 wgethttp://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会出错 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/下对应的两个示例文件。 这里看下pgbouncer.ini的配置,其主要说明可以从上面说的路径中查看对应的说明 [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三个级别。
pgbouncer的默认设置是session链接。 auth_type和auth_file是bppgbouncer用以完成客户端身份认证。auth_file中保存用户名和密码,根据验证方式(auth_type)的不同,auth_file的内容也有不同。
需要说明的是:auth_file中的用户名、密码都必须使用双引号,否则还是报错。 例如:auth_type = md5,那么user.txt,需要同时包括明文和加密码后的账号密码: "postgres" "123456" 这里第二行的账号和MD5密码,md5密码为 "md5" + md5(password + username) 或者通过查询数据库中pg_authid获取 在数据库服务器上运行,生成的文件userlist.txt放至配置目录下。 更多的参数说明请查看这里。 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了。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- 没人想要 XML 里的 ENTITY 部分
- c# – 为什么我的$.ajax调用不会从cshtml文件返回一个json对
- ruby-on-rails – 设计:能够将参数传递给注册#sign_up动作
- XML中添加换行符
- postgresql – Postgres会话空闲,查询= COMMIT或ROLLBACK
- 在oracle 12c上如何禁用haip,以及打开haip
- ACE反应器(Reactor)模式
- c# – 将XML反序列化为对象 – XML文档中存在错误(0,0)
- 使用Ruby中的Net :: HTTP创建带有标头信息的HTTP POST请求
- reactjs – 使用create-react-app生成单个物理javascript文