前端時間看了看服務器還有多餘的內存,因爲數據庫和程序都在一個服務器上,就想看看有什麼提高併發的方法。試用了下pgbouncer ,記錄下。 php
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
主要兩個文件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
pgbouncer的默認設置是session連接。 sql
auth_type和auth_file是bppgbouncer用以完成客戶端身份認證。auth_file中保存用戶名和密碼,根據驗證方式(auth_type)的不一樣,auth_file的內容也有不一樣。 shell
須要說明的是: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
更多的參數說明請查看這裏。
啓動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的狀態。
那麼要如何使程序鏈接生效呢? 好比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了。