Linux 下安装pgbouncer

系统环境准备

1 创建用户:
sudo groupadd postgres
sudo useradd -g postgres postgres

2 创建目录并赋权
sudo mkdir -p /opt/pgbouncer
sudo mkdir -p /export/pgbouncer_data
sudo chown -R postgres: /export/pgbouncer

3 安装依赖包
sudo apt-get install openssl
sudo apt-get install libssl-dev

4 安装libevent
wget http://www.monkey.org/~provos/libevent-2.0.22.tar.gz
tar -zxvf libevent-2.0.22.tar.gz
cd libevent-2.0.22
sudo ./configure --prefix=/usr
make
make install

安装pgbouncer

官网:https://pgbouncer.github.io/

1 先去官网下载pgbouncer源码安装包,下载地址:https://pgbouncer.github.io/downloads/
2 解压安装包:tar -zxvf pgbouncer-1.7.2.tar.gz
3 源码安装:
$ ./configure --prefix=/opt/pgbouncer --with-libevent=libevent-prefix
$ make
$ make install

配置文件

所有的配置文件都在/export/pgbouncer_data下

1 配置pgbouncer.ini文件
[databases]
* = host=along port=5432 user=along password=along pool_size=20

[pgbouncer]
listen_port = 6432
listen_addr = 127.0.0.1
;unix_socket_dir = ''
;user = postgres
auth_type = md5
auth_file = /export/pgbouncer_data/users.txt
logfile = /export/pgbouncer_data/pgbouncer.log
pidfile = /export/pgbouncer_data/pgbouncer.pid
admin_users = postgres
stats_users = mon
pool_mode = session
client_idle_timeout=10
server_idle_timeout=1800
idle_transaction_timeout=10
client_login_timeout=10
server_reset_query = DEALLOCATE ALL;
server_check_query = select 1
server_check_delay = 10
reserve_pool_size = 5
reserve_pool_timeout = 5
max_client_conn = 50
default_pool_size = 20
log_connections = 0
log_disconnections = 0
log_pooler_errors = 1
ignore_startup_parameters = extra_float_digits

2 配置users.txt文件
-- ”用户名“ ”密码“
"along" "along"
“postgres” "postgres"

 3 配置ip_limit文件
192.0.0.0/8

4 配置环境变量
export PATH=/opt/pg10/bin:/opt/pgbouncer/bin:$PATH
source .bashrc

启动pgbouncer

postgres@along:/export/pgbouncer_data$ pgbouncer -d /export/pgbouncer_data/pgbouncer.ini 
2017-07-20 21:10:55.952 11381 LOG File descriptor limit: 1024 (H:65536), max_client_conn: 50, max fds possible: 60
2017-07-20 21:10:55.953 11381 FATAL @src/main.c:893 in function main(): unix socket is in use, cannot continue
原因:
在/tmp目录下已经有端口5432的socket
postgres@along:/export/pgbouncer_data$ ll /tmp |grep -i .s.PG
srwxrwxrwx  1 postgres postgres      0 7月  20 21:14 .s.PGSQL.5432=
-rw-------  1 postgres postgres     47 7月  20 21:14 .s.PGSQL.5432.lock

解决方法:
修改pgbouncer.ini 配置文件下的listen_port = 5432 为 6432

postgres@along:/export/pgbouncer_data$ pgbouncer -d /export/pgbouncer_data/pgbouncer.ini 
2017-07-20 21:20:56.546 11637 LOG File descriptor limit: 1024 (H:65536), max_client_conn: 50, max fds possible: 60
postgres@along:/export/pgbouncer_data$ ll /tmp |grep -i .s.PG
srwxrwxrwx  1 postgres postgres      0 7月  20 21:14 .s.PGSQL.5432=
-rw-------  1 postgres postgres     47 7月  20 21:14 .s.PGSQL.5432.lock
srwxrwxrwx  1 postgres postgres      0 7月  20 21:20 .s.PGSQL.6432=

pgbouncer启动成功

登录pgbouncer控制台

postgres@along:/export/pgbouncer_data$ psql -h 127.0.0.1 -Upostgres -p 6432 pgbouncer
Password for user postgres: 
psql (10beta1, server 1.7.2/bouncer)
Type "help" for help.

pgbouncer=#
上一篇:Linux源码安装PostGIS


下一篇:Greenplum 在Linux环境安装