postgresql 连接池pgbouncer的安装步骤详解

作者:袖梨 2022-06-29

说明
在安装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 []
RESUME []
KILL
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)

相关文章

精彩推荐