天天看點

pgbouncer的安裝使用

由于 postgresql 采用程序模式,大量連接配接對資料庫性能會産生較大影響,在大量連接配接情況下, 

一般會在資料庫和應用程式之間配置 pgbouncer,pgbouncer 可以配置在資料庫主機上,也可以配置在 

單獨一台伺服器上,但是采用了 pgbouncer 後,在維護方面和以前會有些差別 

================================================================================ 

說白了就是一連接配接池。現在有好多子產品,plproxy,pgpool什麼的,都有這功能。pgbouncer與之相比,最大的好處是簡單,小型。 

如果不需要負載均衡什麼的功能,用pgbouncer就足夠了。 

pgbouncer的特點 

    a.記憶體消耗低(預設為2k/連接配接),因為bouncer不需要每次都接受完整的資料包 

    b.可以把不同的資料庫連接配接到一個機器上,而對用戶端保持透明 

    c.支援線上的重新配置而無須重新開機 

/////////////////////////////////////////////////////////////////////// 

實際操作: 

環境: 

centos 6.5  64位 

pg  9.1.1 

pgbouncer 1.5.4  最新版本 

libevent 2.0.22 

1.下載下傳pgbouncer的安裝包 

http://pgfoundry.org/frs/?group_id=1000258&release_id=1952 

2.下載下傳libevent包 

http://monkey.org/~provos/libevent/ 

這玩意也是個好東西,在很多地方用到 

3.安裝libevent 

[root@pgtina packages]# wget http://192.168.10.100/pgbouncer-1.5.4.tar.gz 

[root@pgtina packages]# tar -xzvf pgbouncer-1.5.4.tar.gz 

[root@pgtina packages]# ll 

total 1176 

drwxr-xr-x.  8 1000 1000   4096 jan  5  2015 libevent-2.0.22-stable 

-rw-r--r--.  1 root root 854987 nov 12 14:51 libevent-2.0.22-stable.tar.gz 

drwxr-xr-x. 10 1000 1000   4096 nov 28  2012 pgbouncer-1.5.4 

-rw-r--r--.  1 root root 339610 nov 12 14:46 pgbouncer-1.5.4.tar.gz 

cd /root/packages/ 

cd libevent-2.0.22-stable 

./configure --prefix=/home/pgsql/libevent 

make 

make install 

4.安裝pgbouncer 

/root/packages/pgbouncer-1.5.4 

./configure --prefix=/home/pgsql/pgbouncer/ --with-libevent=/home/pgsql/libevent/ 

檢視pgbouncer是否安裝成功,可以通過檢視config.log中最後的傳回值exit來确認,0是成功1是失敗. 

[root@pgtina pgbouncer-1.5.4]# tail -5 config.log 

#define have_event_base_new 1 

#define have_evdns_base_new 1 

#define use_evdns 1 

configure: exit 0    ----安裝成功 

二.配置 

修改屬主: 

drwxr-xr-x. 5 root     root     4096 nov 12 15:05 libevent 

drwxr-xr-x. 4 root     root     4096 nov 12 15:07 pgbouncer 

[root@pgtina pgsql]# chown -r postgres:postgres pgbouncer/ 

[root@pgtina pgsql]# chown -r postgres:postgres libevent/ 

1.配置pgbouncer的cfg檔案 

cp /root/packages/pgbouncer-1.5.4/etc/pgbouncer.ini  /home/pgsql/pgbouncer/ 

chown postgres:postgres pgbouncer.ini 

[postgres@localhost config]$ pwd 

/home/postgres/pgbouncer/config 

[postgres@localhost config]$ more pgbouncer.ini 

[databases] 

yunwei = host=192.168.12.250 port=5432 user=postgres  password=1qaz@wsx68 dbname=yunwei pool_size=100 

tm_samples = host=192.168.12.250 port=5432 user=postgres  password=1qaz@wsx68 dbname=tm_samples pool_size=30 

yunwei = host=192.168.12.250 port=5432 user=postgres  password=** dbname=yunwei pool_size=50 

[pgbouncer] 

listen_port = 1999 

listen_addr = 192.168.12.250 

auth_type = md5 

auth_file = /home/pgsql/pgbouncer/user.txt 

logfile = /home/pgsql/pgbouncer/pgbouncer.log 

pidfile = /home/pgsql/pgbouncer/pgbouncer.pid 

admin_users = root 

pool_mode = transaction 

//////////可選參數 

max_client_conn = 100 

default_pool_size = 900 

reserve_pool_timeout = 0 

reserve_pool_size = 30 

server_reset_query = discard all; 

admin_users = pgadmin 

stats_users = pgstats 

ignore_startup_parameters = extra_float_digits,application_name,geqo 

stats_period = 30 

server_idle_timeout,max_connections也比較重要,後期再寫。 

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.否則會報錯的。 

admin_users:列出哪些使用者可以登入pgbouncer進行管理,以逗号進行分隔 

stats_users:列出哪些使用者可以登入pgbouncer進行隻讀操作,如可以列出伺服器狀态,通路連結等,但是不能執行reload。 

///////// 

2.配置使用者密碼檔案users.txt 

[postgres@pgtina pgbouncer]$ more user.txt 

"root" "tina" 

"postgres"  "test12" 

"fenxi" "fenxi" 

"sqluser" "sqlpassword" 

三.啟動 

1.啟動指令 

[postgres@pgtina ~]$ /home/pgsql/pgbouncer/bin/pgbouncer -d /home/pgsql/pgbouncer/pgbouncer.ini 

如果報錯: 

error while loading shared libraries: libevent-2.0.so.5: 

就安裝兩個包: 

[root@pgtina libevent-2.0.22-stable]# yum install libevent 

[root@pgtina libevent-2.0.22-stable]# yum install libevent-devel 

2015-11-12 15:46:56.901 16634 log file descriptor limit: 1024 (h:4096), max_client_conn: 100, max fds possible: 130   ---啟動成功 

2.檢視日志 

[root@pgtina pgbouncer]# ll 

total 28 

drwxr-xr-x. 2 postgres postgres 4096 nov 12 15:32 bin 

-rw-r--r--. 1 postgres postgres 5832 nov 12 15:35 pgbouncer.ini 

-rw-rw-r--. 1 postgres postgres  438 nov 12 15:47 pgbouncer.log 

-rw-r--r--. 1 postgres postgres    5 nov 12 15:46 pgbouncer.pid 

drwxr-xr-x. 4 postgres postgres 4096 nov 12 15:32 share 

-rw-r--r--. 1 postgres postgres   39 nov 12 15:35 user.txt 

tail -f /home/postgres/pgbouncer/pgbouncer.log 

3.登入 

[postgres@pgtina ~]$ psql -h 192.168.12.250 -p 1999 -u postgres -d yunwei     ---連接配接的時候一定要帶-h ip 

password for user postgres: 

psql (9.1.1) 

type "help" for help. 

yunwei=# \d 

                        list of relations 

schema |               name                |   type   |  owner   

--------+-----------------------------------+----------+---------- 

public | ali_download_daily                | view     | postgres 

public | am_android_wildmob_ru             | table    | postgres 

public | am_android_wildmob_ru_id_seq      | sequence | postgres 

4.參數使用例子 

連接配接pgbouncer本身的db 

[postgres@pgtina pgbouncer]$ psql -h 192.168.12.250 -p 1999 -u root -d pgbouncer    --注意端口跟資料庫的本身端口要區分開,密碼就是tina ,且連接配接的時候一定要帶-h ip 

password for user root: 

psql (9.1.1, server 1.5.4/bouncer) 

warning: psql version 9.1, server version 1.5. 

         some psql features might not work. 

pgbouncer=# show help; 

notice:  console usage 

detail:  

show help|config|databases|pools|clients|servers|version 

show stats|fds|sockets|active_sockets|lists|mem 

show dns_hosts|dns_zones 

set key = arg 

reload 

pause [<db>] 

resume [<db>] 

kill <db> 

suspend 

shutdown 

show 

pgbouncer 三中模式: 

1.session pooling:一個用戶端連接配接對應一個伺服器連接配接。用戶端斷開,伺服器連接配接回收到連接配接池中。是預設的模式,每開啟一個程序,db端也會開啟一個新的程序 

2.transaction pooling:伺服器連接配接在一個事務裡是才給予用戶端,事務結束,連接配接回收回連接配接池。 

3.statement pooling:不允許多語句的事務,最激進的模式。主要給pl/proxy使用。是基于每個查詢的,開啟此模式不适合執行事務,會報錯 

show config; #檢視相關配置 

show stats; 

show lists;#檢視連接配接相關資訊,如,資料庫個數,空閑連接配接數等等 

show pools;#檢視池中連接配接資訊 

show databases;#檢視相關資料庫 

show clients; #檢視連接配接數 

如果修改了配置檔案,那麼需要reload重新加載 

pgbouncer=# reload; 

pgbouncer是一個針對postgresql資料庫的輕量級連接配接池,任何目标應用都可以把 pgbouncer 當作一個 postgresql 伺服器來連接配接,然後pgbouncer 會處理與伺服器連接配接,或者是重用已存在的連接配接。 

pgbouncer 的目标是降低因為建立到 postgresql 的連接配接而導緻的性能損失。 

pgbouncer=# show databases; 

    name    |   host    | port |  database  | force_user | pool_size | reserve_pool 

------------+-----------+------+------------+------------+-----------+-------------- 

mvds_url   | 192.168.12.250 | 5432 | mvds_url   | postgres   |        50 |            0 

pgbouncer  |           | 1999 | pgbouncer  | pgbouncer  |         2 |            0 

tm_samples | 192.168.12.250 | 5432 | tm_samples | postgres   |        30 |            0 

yunwei     | 192.168.12.250 | 5432 | yunwei     | postgres   |       100 |            0 

===== 

2012-12-11 19:36:44.881 19072 warning c-0x3620a20: (nodb)/(nouser)@172.65.20.35:55419 pooler error: unsupported startup parameter: extra_float_digits 

報錯很明顯,有一個參數不支援, unsupported startup parameter  extra_float_digits ,pgbouncer中有一個參數是可以忽略啟動參數的,叫ignore_startup_parameters 

處理辦法: 

修改配置檔案pgbouncer.ini,在[pgbouncer]下面新增一行 

ignore_startup_parameters = extra_float_digits 

重新開機pgbouncer,再次檢視日志,該參數已被忽略,測試資料正常。 

======== 

5.連接配接數測試 

psql -u sqluser -d dw_report -p 1999 

psql -u postgres -d devops -p 1999 

devops=# select * from pg_tables where schemaname='public'; 

schemaname |  tablename   | tableowner | tablespace | hasindexes | hasrules | hastriggers 

------------+--------------+------------+------------+------------+----------+------------- 

public     | t_test_wubao | postgres   |            | f          | f        | f 

pgbouncer=# show clients; 

type |  user   | database  | state  |   addr    | port  | local_addr | local_port |    connect_time     |    request_time     |   ptr    | link 

------+---------+-----------+--------+-----------+-------+------------+------------+---------------------+---------------------+----------+------ 

c    | fenxi   | devops    | active | unix      |  1999 | unix       |       1999 | 2015-11-12 16:19:36 | 2015-11-12 16:19:45 | 0xc71b88 | 

c    | sqluser | dw_report | active | unix      |  1999 | unix       |       1999 | 2015-11-12 16:21:07 | 2015-11-12 16:21:35 | 0xc71cf0 | 

c    | root    | pgbouncer | active | 127.0.0.1 | 54713 | 127.0.0.1  |       1999 | 2015-11-12 15:59:48 | 2015-11-12 16:21:41 | 0xc71a20 | 

(3 rows) 

pgbouncer=# show pools; 

database  |   user    | cl_active | cl_waiting | sv_active | sv_idle | sv_used | sv_tested | sv_login | maxwait 

-----------+-----------+-----------+------------+-----------+---------+---------+-----------+----------+--------- 

devops    | postgres  |         1 |          0 |         0 |       0 |       1 |         0 |        0 |       0 

dw_report | postgres  |         1 |          0 |         0 |       0 |       1 |         0 |        0 |       0 

pgbouncer | pgbouncer |         1 |          0 |         0 |       0 |       0 |         0 |        0 |       0 

yunwei    | postgres  |         0 |          0 |         0 |       0 |       0 |         0 |        0 |       0 

如果是通過 pgsql -h 127.0.0.1 -p 6000 -u readonly pgbouncer 方式登入,在執行reload時就會提示“error:  admin access needed”的錯誤資訊了。 

7. 如何停止 

     以admin登入pgbouncer, 執行shutdown即可。 

===========================================================    

差別一 pg_hba.conf 功能減弱 

      采用了 pgbouncer 卑鄙,pg_hba.conf 功能減弱,這裡說減弱,是因為原庫上 pg_hba.conf 

可以配置,但已經形同虛設,不能發揮原有的作用,因為可以配置原庫上的 pg_hba.conf ,配置 

成隻允許 pgbouncer,原庫 pg_hba.conf 配置如下。 

# type  database        user            address                 method 

# "local" is for unix domain socket connections only 

local   all             all                                     trust 

#pgbouncer ip 

host all all 192.168.1.10/32 md5 

     盡管在原庫上配置了 pg_hba.conf ,隻允許 pgbouncer主機連庫, 但是不能限制其它ip通過 

pg_bouncer  連接配接資料庫,從這個角度來說,采用 pgbouncer 無疑是增加了安全漏洞,當然可以 

采用其它措施彌補。 

差別二 主庫上無法查詢應用伺服器 ip 

         在資料庫維護過程中,經常需要查詢某個庫的業務伺服器ip資訊,查詢哪些伺服器在連接配接資料庫, 

采用了 pgbouncer 後,這方面的查詢沒以前友善,例如在主庫上查詢連接配接 skytf 庫的用戶端 ip 資訊, 

如下所示: 

postgres=# select datname,usename ,client_addr,client_port from pg_stat_activity where datname='skytf'; 

  datname  |  usename  |   client_addr   | client_port 

-----------+-----------+-----------------+------------- 

skytf | skytf | 192.168.1.10 |       32119 

skytf | skytf | 192.168.1.10 |       18583 

skytf | skytf | 192.168.1.10 |       31723 

skytf | skytf | 192.168.1.10 |       32363 

skytf | skytf | 192.168.1.10 |       58929 

skytf | skytf | 192.168.1.10 |       58930 

skytf | skytf | 192.168.1.10 |       58931 

skytf | skytf | 192.168.1.10 |       58932 

skytf | skytf | 192.168.1.10 |       58933 

skytf | skytf | 192.168.1.10 |       58934 

skytf | skytf | 192.168.1.10 |       58935 

skytf | skytf | 192.168.1.10 |       58936 

skytf | skytf | 192.168.1.10 |       58937 

skytf | skytf | 192.168.1.10 |       58938 

     從上面查詢結果來看,上面的 clietn_addr 隻顯示 pgbouncer 的 ip,而無法顯示真正的用戶端 ip, 

那麼如何擷取用戶端的ip呢? 有兩種方法: 

--4 方法一: 登陸 pgbouncer 控制台擷取用戶端ip資訊 

            登陸 pgbouncer 控制台,并且執行 "show clients" 指令。 

postgres@db> psql -h 127.0.0.1 -p 1922  pgbouncer pgbouncer_admin 

psql (9.1.2, server 1.4.2/bouncer) 

warning: psql version 9.1, server version 1.4. 

type |      user       |  database  | state  |         addr          | port  |       local_addr       | local_port |    connect_tim 

e     |    request_time     |    ptr     |    link    

------+-----------------+------------+--------+-----------------------+-------+------------------------+------------+--------------- 

------+---------------------+------------+------------ 

c    | skytf       | skytf  | active | ::ffff:192.168.1.15 | 57791 | ::ffff:192.168.173.215 |       1922 | 2012-03-20 06: 

45:28 | 2012-03-20 10:36:31 | 0x1350c9d0 | 0x1352e330 

    備注:為了格式輸出,上面隻列出一條記錄,例如上面可以看出連接配接 skytf 庫的業務伺服器ip為 

              192.168.1.15,顯然通過 pgbouncer 查詢用戶端 ip 不太友善。