os : debian 8.2
pgbouncer: 1.5.4
libevent: 2.0.21
libevent-dev: 2.0.21php
本次採用的時 apt-get 方式安裝html
安裝 libeventpython
# apt-get install libevent-2.0-5
正在讀取軟件包列表... 完成
正在分析軟件包的依賴關係樹
正在讀取狀態信息... 完成
libevent-2.0-5 已是最新的版本。
升級了 0 個軟件包,新安裝了 0 個軟件包,要卸載 0 個軟件包,有 1 個軟件包未被升級。
安裝 libevent-devnginx
# apt-get install libevent-dev
正在讀取軟件包列表... 完成
正在分析軟件包的依賴關係樹
正在讀取狀態信息... 完成
將會安裝下列額外的軟件包:
libevent-core-2.0-5 libevent-extra-2.0-5 libevent-openssl-2.0-5 libevent-pthreads-2.0-5
下列【新】軟件包將被安裝:
libevent-core-2.0-5 libevent-dev libevent-extra-2.0-5 libevent-openssl-2.0-5 libevent-pthreads-2.0-5
升級了 0 個軟件包,新安裝了 5 個軟件包,要卸載 0 個軟件包,有 1 個軟件包未被升級。
須要下載 541 kB 的軟件包。
解壓縮後會消耗掉 2,120 kB 的額外空間。
您但願繼續執行嗎? [Y/n] y
獲取:1 http://mirrors.163.com/debian/ jessie/main libevent-core-2.0-5 amd64 2.0.21-stable-2+deb8u1 [108 kB]
獲取:2 http://mirrors.163.com/debian/ jessie/main libevent-extra-2.0-5 amd64 2.0.21-stable-2+deb8u1 [90.7 kB]
獲取:3 http://mirrors.163.com/debian/ jessie/main libevent-openssl-2.0-5 amd64 2.0.21-stable-2+deb8u1 [50.0 kB]
獲取:4 http://mirrors.163.com/debian/ jessie/main libevent-pthreads-2.0-5 amd64 2.0.21-stable-2+deb8u1 [44.1 kB]
獲取:5 http://mirrors.163.com/debian/ jessie/main libevent-dev amd64 2.0.21-stable-2+deb8u1 [248 kB]
下載 541 kB,耗時 5秒 (93.7 kB/s)
正在選中未選擇的軟件包 libevent-core-2.0-5:amd64。
(正在讀取數據庫 ... 系統當前共安裝有 142086 個文件和目錄。)
正準備解包 .../libevent-core-2.0-5_2.0.21-stable-2+deb8u1_amd64.deb ...
正在解包 libevent-core-2.0-5:amd64 (2.0.21-stable-2+deb8u1) ...
正在選中未選擇的軟件包 libevent-extra-2.0-5:amd64。
正準備解包 .../libevent-extra-2.0-5_2.0.21-stable-2+deb8u1_amd64.deb ...
正在解包 libevent-extra-2.0-5:amd64 (2.0.21-stable-2+deb8u1) ...
正在選中未選擇的軟件包 libevent-openssl-2.0-5:amd64。
正準備解包 .../libevent-openssl-2.0-5_2.0.21-stable-2+deb8u1_amd64.deb ...
正在解包 libevent-openssl-2.0-5:amd64 (2.0.21-stable-2+deb8u1) ...
正在選中未選擇的軟件包 libevent-pthreads-2.0-5:amd64。
正準備解包 .../libevent-pthreads-2.0-5_2.0.21-stable-2+deb8u1_amd64.deb ...
正在解包 libevent-pthreads-2.0-5:amd64 (2.0.21-stable-2+deb8u1) ...
正在選中未選擇的軟件包 libevent-dev。
正準備解包 .../libevent-dev_2.0.21-stable-2+deb8u1_amd64.deb ...
正在解包 libevent-dev (2.0.21-stable-2+deb8u1) ...
正在設置 libevent-core-2.0-5:amd64 (2.0.21-stable-2+deb8u1) ...
正在設置 libevent-extra-2.0-5:amd64 (2.0.21-stable-2+deb8u1) ...
正在設置 libevent-openssl-2.0-5:amd64 (2.0.21-stable-2+deb8u1) ...
正在設置 libevent-pthreads-2.0-5:amd64 (2.0.21-stable-2+deb8u1) ...
正在設置 libevent-dev (2.0.21-stable-2+deb8u1) ...
正在處理用於 libc-bin (2.19-18+deb8u10) 的觸發器 ...
安裝 pgbouncergit
# apt-get install pgbouncer
正在讀取軟件包列表... 完成
正在分析軟件包的依賴關係樹
正在讀取狀態信息... 完成
將會安裝下列額外的軟件包:
postgresql-client-common postgresql-common
建議安裝的軟件包:
python-psycopg2
下列【新】軟件包將被安裝:
pgbouncer postgresql-client-common postgresql-common
升級了 0 個軟件包,新安裝了 3 個軟件包,要卸載 0 個軟件包,有 1 個軟件包未被升級。
須要下載 400 kB 的軟件包。
解壓縮後會消耗掉 1,234 kB 的額外空間。
您但願繼續執行嗎? [Y/n] y
獲取:1 http://mirrors.163.com/debian/ jessie/main postgresql-client-common all 165+deb8u3 [73.8 kB]
獲取:2 http://mirrors.163.com/debian/ jessie/main postgresql-common all 165+deb8u3 [203 kB]
獲取:3 http://mirrors.163.com/debian/ jessie/main pgbouncer amd64 1.5.4-6+deb8u1 [123 kB]
下載 400 kB,耗時 5秒 (73.5 kB/s)
正在預設定軟件包 ...
正在選中未選擇的軟件包 postgresql-client-common。
(正在讀取數據庫 ... 系統當前共安裝有 141812 個文件和目錄。)
正準備解包 .../postgresql-client-common_165+deb8u3_all.deb ...
正在解包 postgresql-client-common (165+deb8u3) ...
正在選中未選擇的軟件包 postgresql-common。
正準備解包 .../postgresql-common_165+deb8u3_all.deb ...
正在添加 postgresql-common 致使 /usr/bin/pg_config 轉移到 /usr/bin/pg_config.libpq-dev
正在解包 postgresql-common (165+deb8u3) ...
正在選中未選擇的軟件包 pgbouncer。
正準備解包 .../pgbouncer_1.5.4-6+deb8u1_amd64.deb ...
正在解包 pgbouncer (1.5.4-6+deb8u1) ...
正在處理用於 man-db (2.7.0.2-5) 的觸發器 ...
正在處理用於 systemd (215-17+deb8u7) 的觸發器 ...
正在設置 postgresql-client-common (165+deb8u3) ...
正在設置 postgresql-common (165+deb8u3) ...
正在將用戶「postgres」加入到「ssl-cert」組中
Creating config file /etc/postgresql-common/createcluster.conf with new version
Creating config file /etc/logrotate.d/postgresql-common with new version
Building PostgreSQL dictionaries from installed myspell/hunspell packages...
en_us
Removing obsolete dictionary files:
正在設置 pgbouncer (1.5.4-6+deb8u1) ...
正在處理用於 systemd (215-17+deb8u7) 的觸發器 ...
驗證安裝web
# dpkg -l pgbouncer
指望狀態=未知(u)/安裝(i)/刪除(r)/清除(p)/保持(h)
| 狀態=未安裝(n)/已安裝(i)/僅存配置(c)/僅解壓縮(U)/配置失敗(F)/不徹底安裝(H)/觸發器等待(W)/觸發器未決(T)
|/ 錯誤?=(無)/須重裝(R) (狀態,錯誤:大寫=故障)
||/ 名稱 版本 體系結構: 描述 +++-=========================================-=========================-=========================-=======================================================================================
ii pgbouncer 1.5.4-6+deb8u1 amd64 lightweight connection pooler for PostgreSQL
# dpkg -l libevent-2.0-5
指望狀態=未知(u)/安裝(i)/刪除(r)/清除(p)/保持(h)
| 狀態=未安裝(n)/已安裝(i)/僅存配置(c)/僅解壓縮(U)/配置失敗(F)/不徹底安裝(H)/觸發器等待(W)/觸發器未決(T)
|/ 錯誤?=(無)/須重裝(R) (狀態,錯誤:大寫=故障)
||/ 名稱 版本 體系結構: 描述 +++-========================================-=========================-=========================-======================================================================================
ii libevent-2.0-5:amd64 2.0.21-stable-2+deb8u1 amd64 Asynchronous event notification library
# dpkg -l libevent-dev
指望狀態=未知(u)/安裝(i)/刪除(r)/清除(p)/保持(h)
| 狀態=未安裝(n)/已安裝(i)/僅存配置(c)/僅解壓縮(U)/配置失敗(F)/不徹底安裝(H)/觸發器等待(W)/觸發器未決(T)
|/ 錯誤?=(無)/須重裝(R) (狀態,錯誤:大寫=故障)
||/ 名稱 版本 體系結構: 描述 +++-========================================-=========================-=========================-======================================================================================
ii libevent-dev 2.0.21-stable-2+deb8u1 amd64 Asynchronous event notification library (development files)
pgbouncer相關的路徑、文件sql
# dpkg -L pgbouncer
/.
/etc
/etc/init.d
/etc/init.d/pgbouncer
/etc/pgbouncer
/etc/pgbouncer/pgbouncer.ini
/etc/pgbouncer/userlist.txt
/etc/default
/etc/default/pgbouncer
/usr
/usr/sbin
/usr/sbin/pgbouncer
/usr/share
/usr/share/man
/usr/share/man/man5
/usr/share/man/man5/pgbouncer.5.gz
/usr/share/man/man1
/usr/share/man/man1/pgbouncer.1.gz
/usr/share/doc
/usr/share/doc/pgbouncer
/usr/share/doc/pgbouncer/NEWS.gz
/usr/share/doc/pgbouncer/README.Debian
/usr/share/doc/pgbouncer/changelog.Debian.gz
/usr/share/doc/pgbouncer/AUTHORS
/usr/share/doc/pgbouncer/faq.html
/usr/share/doc/pgbouncer/examples
/usr/share/doc/pgbouncer/examples/userlist.txt
/usr/share/doc/pgbouncer/examples/pgbouncer.ini.gz
/usr/share/doc/pgbouncer/todo.html
/usr/share/doc/pgbouncer/copyright
/usr/share/doc/pgbouncer/config.html
/usr/share/doc/pgbouncer/README
/usr/share/doc/pgbouncer/usage.html
/usr/share/doc/pgbouncer/README.html
/usr/share/pgbouncer
/usr/share/pgbouncer/mkauth.py
重點關注的幾個路徑、文件數據庫
/etc/init.d/pgbouncer
/etc/init.d/$NAME {start|stop|reload|force-reload|restart|status}
/etc/pgbouncer/
/etc/pgbouncer/pgbouncer.ini
/etc/pgbouncer/userlist.txt
/etc/default/pgbouncer
/usr/sbin/pgbouncer
/var/run/postgresql
/usr/share/pgbouncer
/usr/share/doc/pgbouncer
兩個重要的文件
/etc/pgbouncer/pgbouncer.ini
/etc/pgbouncer/userlist.txt
配置pgbouncer的cfg文件 pgbouncer.iniruby
# vi pgbouncer.ini
[databases]
trade_peiybdb = host=192.168.56.101 port=5432 user=usr_peiyb password=usr_peiyb dbname=peiybdb pool_size=50 client_encoding=UNICODE datestyle=ISO connect_query='SELECT 1'
[pgbouncer]
logfile = /var/log/postgresql/pgbouncer.log
pidfile = /var/run/postgresql/pgbouncer.pid
listen_addr = 192.168.56.220
listen_port = 6432
#unix_socket_dir = /var/run/postgresql
auth_type = md5
auth_file = /etc/pgbouncer/userlist.txt
pool_mode = session
admin_users = root
stats_users = stat_collector
server_reset_query = DISCARD ALL
max_client_conn = 100
default_pool_size = 20
配置用戶密碼的文件 userlist.txt 服務器
# vi userlist.txt
"root" "root"
"usr_abc" "usr_abc"
啓動pgbouncer
# pgbouncer --help
Usage: pgbouncer [OPTION]... config.ini
-d, --daemon Run in background (as a daemon) -R, --restart Do a online restart -q, --quiet Run quietly -v, --verbose Increase verbosity -u, --user=<username> Assume identity of <username> -V, --version Show version -h, --help Show this help screen and exit
# service pgbouncer start
或者
$ /usr/sbin/pgbouncer -d /etc/pgbouncer/pgbouncer.ini
2018-01-22 21:58:26.622 4715 LOG File descriptor limit: 65536 (H:65536), max_client_conn: 100, max fds possible: 160
查看進程
$ ps -fu postgres
UID PID PPID C STIME TTY TIME CMD
postgres 4712 4711 0 21:58 pts/1 00:00:00 -su
postgres 4717 1 0 21:58 ? 00:00:00 /usr/sbin/pgbouncer -d /etc/pgbouncer/pgbouncer.ini
postgres 4720 4712 0 22:00 pts/1 00:00:00 ps -fu postgres
查看端口
# netstat -antp|grep -i 6432
tcp 0 0 192.168.56.220:6432 0.0.0.0:* LISTEN 6158/pgbouncer
鏈接pgbouncer
$ psql -h 192.168.56.220 -p 6432 -U usr_abc trade_peiybdb
用戶 usr_abc 的口令:
psql (9.4.15, 服務器 9.1.24)
輸入 "help" 來獲取幫助信息.
trade_peiybdb=>
查看6432端口
# netstat -antp|grep -i 6432
tcp 0 0 192.168.56.220:6432 0.0.0.0:* LISTEN 6158/pgbouncer
tcp 0 0 192.168.56.220:58877 192.168.56.220:6432 ESTABLISHED 6163/psql
tcp 0 0 192.168.56.220:58876 192.168.56.220:6432 TIME_WAIT -
tcp 0 0 192.168.56.220:6432 192.168.56.220:58877 ESTABLISHED 6158/pgbouncer
上面輸出留意 6163/psql 這個進程
# netstat -antp|grep -i "192.168.56"
tcp 0 0 192.168.56.220:6432 0.0.0.0:* LISTEN 6158/pgbouncer
tcp 0 0 192.168.56.220:22 192.168.56.1:57230 ESTABLISHED 4051/sshd: peiyb [p
tcp 0 0 192.168.56.220:22 192.168.56.1:49987 ESTABLISHED 6085/sshd: peiyb [p
tcp 0 52 192.168.56.220:22 192.168.56.1:63625 ESTABLISHED 968/sshd: peiyb [pr
tcp 0 0 192.168.56.220:58877 192.168.56.220:6432 ESTABLISHED 6163/psql
tcp 0 0 192.168.56.220:6432 192.168.56.220:58877 ESTABLISHED 6158/pgbouncer
tcp 0 0 192.168.56.220:38510 192.168.56.101:5432 ESTABLISHED 6158/pgbouncer
上面輸出留意 6158/pgbouncer 這個進程
# netstat -antp|grep -i 6158 |grep -i "192.168.56.101"
tcp 0 0 192.168.56.220:38510 192.168.56.101:5432 ESTABLISHED 6158/pgbouncer
能夠看到 本地 6158/pgbouncer 進程使用 38510 端口 和 遠程主機 192.168.56.10 的5432 端口已經創建了鏈接。
在本地開個 dbeaver 鏈接看看,結果提示錯誤
org.postgresql.util.PSQLException: ERROR: Unsupported startup parameter: extra_float_digits
這個錯誤能夠問下baidu,不少人都碰到了。這裏列出了一個參考過的url,就是修改一個配置
ignore_startup_parameters = extra_float_digits
重啓 pgbouncer 服務 或者 reload 一下
當時參考的文章
https://my.oschina.net/Kenyon/blog/95550
https://www.talendforge.org/forum/viewtopic.php?pid=152966
http://blog.163.com/digoal@126/blog/static/1638770402010539112355/
查看6432端口
# netstat -antp|grep -i 6432
tcp 0 0 192.168.56.220:6432 0.0.0.0:* LISTEN 6442/pgbouncer
tcp 0 0 192.168.56.220:6432 192.168.56.220:58885 ESTABLISHED 6442/pgbouncer
tcp 0 0 192.168.56.220:58885 192.168.56.220:6432 ESTABLISHED 6466/psql
tcp 0 0 192.168.56.220:6432 192.168.56.1:57514 ESTABLISHED 6442/pgbouncer
tcp 0 0 192.168.56.220:6432 192.168.56.1:57513 ESTABLISHED 6442/pgbouncer
tcp 0 0 192.168.56.220:58884 192.168.56.220:6432 TIME_WAIT -
# netstat -antp|grep -i "192.168.56"
tcp 0 0 192.168.56.220:6432 0.0.0.0:* LISTEN 6442/pgbouncer
tcp 0 0 192.168.56.220:38515 192.168.56.101:5432 ESTABLISHED 6442/pgbouncer
tcp 0 0 192.168.56.220:38514 192.168.56.101:5432 ESTABLISHED 6442/pgbouncer
tcp 0 0 192.168.56.220:6432 192.168.56.220:58885 ESTABLISHED 6442/pgbouncer
tcp 0 0 192.168.56.220:58885 192.168.56.220:6432 ESTABLISHED 6466/psql
tcp 0 0 192.168.56.220:6432 192.168.56.1:57514 ESTABLISHED 6442/pgbouncer
tcp 0 0 192.168.56.220:6432 192.168.56.1:57513 ESTABLISHED 6442/pgbouncer
上面觀察最後兩行 本地臺式機 192.168.56.1 到 192.168.56.220 的 dbeaver 建立的鏈接
上面觀察第二、3兩行,是對應鏈接到 192.168.56.101 的 5432 端口的信息
能夠在 pgbouncer 的管理界面求證
# psql -h 192.168.56.220 -p 6432 -U root pgbouncer
用戶 root 的口令:
psql (9.4.15, 服務器 1.5.4/bouncer)
輸入 "help" 來獲取幫助信息.
pgbouncer=# show clients;
type | user | database | state | addr | port | local_addr | local_port | connect_time | request_time | ptr | link ------+---------+---------------+--------+----------------+-------+----------------+------------+---------------------+---------------------+-----------+-----------
C | root | pgbouncer | active | 192.168.56.220 | 58887 | 192.168.56.220 | 6432 | 2018-01-23 02:54:36 | 2018-01-23 02:55:20 | 0x17f9e98 |
C | usr_abc | trade_peiybdb | active | 192.168.56.1 | 57513 | 192.168.56.220 | 6432 | 2018-01-23 02:45:46 | 2018-01-23 02:46:21 | 0x17f9bc8 | 0x1817410
C | usr_abc | trade_peiybdb | active | 192.168.56.1 | 57514 | 192.168.56.220 | 6432 | 2018-01-23 02:45:46 | 2018-01-23 02:46:16 | 0x17f9a60 | 0x1817578
C | usr_abc | trade_peiybdb | active | 192.168.56.220 | 58885 | 192.168.56.220 | 6432 | 2018-01-23 02:47:56 | 2018-01-23 02:47:56 | 0x17f9d30 |
(4 行記錄)
上面觀察第二、3兩行,addr、port、link列
pgbouncer=# show servers;
type | user | database | state | addr | port | local_addr | local_port | connect_time | request_time | ptr | link ------+-----------+---------------+--------+----------------+------+----------------+------------+---------------------+---------------------+-----------+-----------
S | usr_peiyb | trade_peiybdb | active | 192.168.56.101 | 5432 | 192.168.56.220 | 38514 | 2018-01-23 02:44:32 | 2018-01-23 02:46:21 | 0x1817410 | 0x17f9bc8
S | usr_peiyb | trade_peiybdb | active | 192.168.56.101 | 5432 | 192.168.56.220 | 38515 | 2018-01-23 02:44:32 | 2018-01-23 02:46:16 | 0x1817578 | 0x17f9a60
(2 行記錄)
上面觀察第一、2兩行,local_addr、local_port、ptr列
pgbouncer=# show sockets;
type | user | database | state | addr | port | local_addr | local_port | connect_time | request_time | ptr | link | recv_pos | pkt_pos | pkt_remain | send_pos | send_remain | pkt_avail | send_avail ------+-----------+---------------+-----------+----------------+-------+----------------+------------+---------------------+---------------------+-----------+-----------+----------+---------+------------+----------+-------------+-----------+------------
C | root | pgbouncer | cl_active | 192.168.56.220 | 58887 | 192.168.56.220 | 6432 | 2018-01-23 02:54:36 | 2018-01-23 03:02:37 | 0x17f9e98 | | 19 | 0 | 0 | 0 | 0 | 19 | 0
C | usr_abc | trade_peiybdb | cl_active | 192.168.56.1 | 57513 | 192.168.56.220 | 6432 | 2018-01-23 02:45:46 | 2018-01-23 02:46:21 | 0x17f9bc8 | 0x1817410 | 0 | 0 | 0 | 0 | 0 | 0 | 0
C | usr_abc | trade_peiybdb | cl_active | 192.168.56.1 | 57514 | 192.168.56.220 | 6432 | 2018-01-23 02:45:46 | 2018-01-23 02:46:16 | 0x17f9a60 | 0x1817578 | 0 | 0 | 0 | 0 | 0 | 0 | 0
C | usr_abc | trade_peiybdb | cl_active | 192.168.56.220 | 58885 | 192.168.56.220 | 6432 | 2018-01-23 02:47:56 | 2018-01-23 02:47:56 | 0x17f9d30 | | 0 | 0 | 0 | 0 | 0 | 0 | 0
S | usr_peiyb | trade_peiybdb | sv_active | 192.168.56.101 | 5432 | 192.168.56.220 | 38514 | 2018-01-23 02:44:32 | 2018-01-23 02:46:21 | 0x1817410 | 0x17f9bc8 | 0 | 0 | 0 | 0 | 0 | 0 | 0
S | usr_peiyb | trade_peiybdb | sv_active | 192.168.56.101 | 5432 | 192.168.56.220 | 38515 | 2018-01-23 02:44:32 | 2018-01-23 02:46:16 | 0x1817578 | 0x17f9a60 | 0 | 0 | 0 | 0 | 0 | 0 | 0
(6 行記錄)
上面觀察第一、2兩行
管理pgbouncer
pgbouncer其實有一個虛擬的db存在,名稱就是pgbouncer
當以admin_users 登記的那個用戶登陸pgbouncer 時,pgbouncer認爲用戶是要對pgbouncer自己進行管理,就不會把去請求真實的DB了。
# psql -h 192.168.56.220 -p 6432 -U root pgbouncer
用戶 root 的口令:
psql (9.4.15, 服務器 1.5.4/bouncer)
輸入 "help" 來獲取幫助信息.
pgbouncer=# show help;
NOTICE: Console usage
描述:
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=# show version;
NOTICE: pgbouncer version 1.5.4 (compiled by <root@lehmann> at 2015-05-23 23:31:01)
SHOW
pgbouncer=# show databases;
name | host | port | database | force_user | pool_size | reserve_pool ---------------+----------------+------+-----------+------------+-----------+--------------
pgbouncer | | 6432 | pgbouncer | pgbouncer | 2 | 0
trade_peiybdb | 192.168.56.101 | 5432 | peiybdb | usr_peiyb | 50 | 0
(2 行記錄)
pgbouncer=# show stats;
database | total_requests | total_received | total_sent | total_query_time | avg_req | avg_recv | avg_sent | avg_query ---------------+----------------+----------------+------------+------------------+---------+----------+----------+-----------
pgbouncer | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0
trade_peiybdb | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0
(2 行記錄)