pgbouncer是postgresql数据库的一种轻量级数据库连接池。
一、什么是数据库连接池?
数据库连接池负责连接,管理,释放数据库连接,允许连接复用。
二、为什么要有数据库连接池?
如果没有连接池,每条sql过来建立一个连接,数据库就要fork一个子进程,子进程会复制父进程的所有信息,不仅浪费内存而且消耗很长时间(建立数据库连接时的网络响应时间)。
三、pgbouncer安装
-
1.yum install pgbouncer
-
2.配置pgbouncer.ini文件
[databases]
mydb= host=127.0.0.1 port=1921 user=xtt password=xtt@123
postgres= host=127.0.0.1 port=1921 user=postgres password=postgres@123
[pgbouncer]
logfile = /var/log/pgbouncer/pgbouncer.log
pidfile = /var/run/pgbouncer/pgbouncer.pid
listen_addr = *
listen_port = 6432
auth_type = md5
auth_file = /etc/pgbouncer/userlist.txt
admin_users = postgres,xtt
pool_mode = session
server_reset_query = DISCARD ALL
ignore_startup_parameters = extra_float_digits
max_client_conn = 1000
default_pool_size = 200 -
3.在pgbouncer.ini同级目录下增加userlist.txt 文件
-如果忘记用户密码 没关系 可以更改
切换到psql登录数据库,alter user postgres with password ‘postgresql@123’
select * from pg_shadow;把加密后的密码粘贴到userlist.txt文件中即可:
“postgres” “md5ce1f9faede3b822ed5982adb7efb1d7d”
“xtt” “md5eefd53021cafbf6405c4e1e4631748a6”
- 4.好上述两个文件之后,可以进行启动pgbouncer
[pgbouncer@192 ~]$ /usr/bin/pgbouncer -d /etc/pgbouncer/pgbouncer.ini
[pgbouncer@192 ~]$ ps -ef|grep pgbouncer
root 7423 7399 0 13:36 pts/0 00:00:00 su - pgbouncer
root 8608 8115 0 14:54 pts/1 00:00:00 su - pgbouncer
pgbounc+ 8675 1 0 14:55 ? 00:00:00 /usr/bin/pgbouncer -d /etc/pgbouncer/pgbouncer.ini
pgbounc+ 8689 8610 0 14:55 pts/1 00:00:00 grep --color=auto pgbouncer
[pgbouncer@192 ~]$ netstat -anlpt |grep 6432
tcp 0 0 0.0.0.0:6432 0.0.0.0:* LISTEN 8675/pgbouncer
tcp6 0 0 :::6432 ::: LISTEN 8675/pgbouncer
pgbouncer可以启动
- 5.登录pgbouncer :
[pgbouncer@192 root]$ psql -Upostgres -dpgbouncer -p 6432 -h 127.0.0.1
用户 postgres 的口令:
psql (9.2.24, 服务器 1.10.0/bouncer)
警告:psql 版本9.2, 服务器版本1.10.
一些psql功能可能无法工作.
输入 “help” 来获取帮助信息
如果要连接其他数据库 改变数据库名,数据库的端口号 和IP就行。
[pgbouncer@192 root]$ psql -U postgres -d test -p 1921 -h 127.0.0.1
- 6.管理
pgbouncer=# show clients;
-[ RECORD 1 ]±-------------------
type | C
user | postgres
database | pgbouncer
state | active
addr | 127.0.0.1
port | 52634
local_addr | 127.0.0.1
local_port | 6432
connect_time | 2019-07-23 15:02:16
request_time | 2019-07-23 15:02:37
wait | 0
wait_us | 0
close_needed | 0
ptr | 0x1e24540
link |
remote_pid | 0
tls |