說明
在安裝pgbouncer之前,已安裝了postgres
軟體下載:
wget https://github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz
wget http://pgfoundry.org/frs/download.php/3393/pgbouncer-1.5.4.tar.gz
編譯安裝libevent
tar zxf libevent-2.0.21-stable.tar.gz
cd libevent-2.0.21-stable
./configure --prefix=/usr/local/libevent
gmake
gmake install
載入libevent動態庫
cd /etc/ld.so.conf.d/
vim libevent2.0.21.conf
/usr/local/libevent/lib
ldconfig
配置運行環境變數
vim /etc/profile
PATH=/usr/local/pgbouncer/bin:$PATH
export PATH
執行生效
source /etc/profile
切換 到postgres使用者,配置運行環境
su - postgres
mkdir /data/pgbouncer/ -p
配置使用者密碼檔案:
cd /data/pgbouncer
vim user.txt
"admin" "111111"
"user" "111111"
"drfdai" "111111"
第一列是使用者名稱,第二列是密碼,串連 pgbouncer用的
配置pgbouncer參數
[databases]
write_db = host=192.168.1.240 port=5432 dbname=drfdai user=postgres password=postgres
read_db = host=192.168.1.241 port=5432 dbname=drfdai user=postgres password=postgres
[pgbouncer]
listen_port = 5433
listen_addr = *
auth_type = md5
auth_file = /data/pgbouncer/user.txt
logfile = /data/pgbouncer/pgbouncer.log
pidfile = /data/pgbouncer/pgbouncer.pid
admin_users = drfdai
pool_mode = session
max_client_conn = 6000
default_pool_size = 128
[databases]
配置後端PG資料庫頂
host= 資料庫IP
port= 資料庫連接埠
dbname= 資料庫名
user= 資料庫帳號
password= 資料庫密碼
[pgbouncer]
配置串連池參數
listen_port= 串連池連接埠,使用者串連時需要用這個連接埠
listen_addr= 允許串連的IP,*代表所有IP
auth_type=md5 用md5方式驗證帳號密碼
auth_file=儲存帳號密碼用的檔案,指的是串連池所用到的帳號密碼,使用者串連進來用的
admin_users= 管理串連池的使用者名稱
pool_mode= 指定池的模式,可以有session,transaction,statement三種模式
max_client_conn= 允許串連到pgbouncer上的最大用戶端數
default_pool_size= 串連池的預設在大小
如果有串連池後端用多個資料庫,則配置多條databases,我這裡配置了二個,一個是主(write_db),一個是從(read_db),讀寫分離用的
如果串連的後端資料庫不是本地的,需要在後端資料庫中開啟遠程允許串連池訪問的許可權,否則登陸失敗
pgbouncer服務管理
檢測設定檔
/usr/local/pgbouncer/bin/pgbouncer -v /data/pgbouncer/pgbouncer.ini
啟動:
/usr/local/pgbouncer/bin/pgbouncer -d /data/pgbouncer/pgbouncer.ini
重啟:
/usr/local/pgbouncer/bin/pgbouncer -R -d /data/pgbouncer/pgbouncer.ini
停止:
psql -p 5433 pgbouncer -h 127.0.0.1 -U drfdai -c 'shutdown'
載入配置:
psql -p 5433 pgbouncer -h 127.0.0.1 -U drfdai -c 'reload'
管理pgbouncer,
先登陸pgbouncer,然後運行管理命令
管理命令可以通過show help查看
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
串連測試
串連write_db資料庫
[postgres@drfdai ~]$ psql -h 127.0.0.1 -p 5433 -U drfdai write_db
Password for user drfdai:
psql (9.4.1)
Type "help" for help.
write_db=# \d
List of relations
Schema | Name | Type | Owner
--------+--------+-------+----------
public | table1 | table | postgres
(1 row)
write_db=#
串連串連池:
[postgres@drfdai ~]$ psql -h 127.0.0.1 -p 5433 -U drfdai pgbouncer Password for user drfdai:
psql (9.4.1, server 1.5.4/bouncer)
Type "help" for help.
pgbouncer=# show clients;
type | user | database | state | addr | port | local_addr | local_port | c
onnect_time | request_time | ptr | link
------+--------+-----------+--------+-----------+-------+------------+------------+-----
----------------+---------------------+-----------+------
C | drfdai | pgbouncer | active | 127.0.0.1 | 38453 | 127.0.0.1 | 5433 | 2016
-01-06 09:46:16 | 2016-01-06 09:46:41 | 0x11ac550 |
(1 row)