pgbouncer学习

pgbouncer学习

之前一直使用pgpool做连接池,近期了解到还有个更轻型的工具pgbouncer,试着搭建下,做简单学习。

安装

直接使用yum安装,下载repo:

yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm -y

执行yum安装命令:

yum install pgbouncer -y

或者从git拉取安装:

$ git clone https://github.com/pgbouncer/pgbouncer.git
$ cd pgbouncer
$ git submodule init
$ git submodule update
$ ./autogen.sh
$ ./configure ...
$ make
$ make install

配置

建测试用户和库:

[root@plat-ecloud01-mgmt-monitor04 ~]# psql -U postgres -p 9999
psql (12.1)
Type "help" for help.

postgres=# CREATE USER test_user WITH PASSWORD 'oracle';
CREATE ROLE
postgres=# CREATE DATABASE test_db OWNER test_user;
CREATE DATABASE
postgres=# GRANT ALL PRIVILEGES ON DATABASE test_db to test_user;
GRANT

修改pgbouncer.ini文件,可参考官方说明:pgbouncer.ini

vim /etc/pgbouncer/pgbouncer.ini

[databases]
test_db = host=localhost port=9999 user=test_user password=oracle connect_query='SELECT 1'

[pgbouncer]
logfile = /var/log/pgbouncer/pgbouncer.log          # 日志文件位置
pidfile = /var/run/pgbouncer/pgbouncer.pid          # pid文件位置
listen_addr = *                                     # 监听的地址
listen_port = 6432                                  # 监听的端口
unix_socket_dir = /var/run/postgresql               # unix socket文件位置
auth_type = md5                                     # 认证方式
auth_file = /etc/pgbouncer/userlist.txt             # 认证文件
admin_users = postgres                              # 管理员用户名
stats_users = stats, postgres                       # 状态用户stats和postgres
pool_mode = transaction                             # 池的模式,默认session级别
server_reset_query = DISCARD ALL                    # 
max_client_conn = 1000                              # 最大连接用户数,客户端到pgbouncer的链接数量
default_pool_size = 20                              # 默认池大小,表示建立多少个pgbouncer到数据库的连接

说明:pool_mode有三个模式,官方说明如下:

pool_mode
Specifies when a server connection can be reused by other clients.
指定服务器连接何时可被其他客户端重用
session
Server is released back to pool after client disconnects. Default.
客户端断开时释放连接池
transaction
Server is released back to pool after transaction finishes.
事务完成时释放连接池
statement
Server is released back to pool after query finishes. Transactions spanning multiple statements are disallowed in this mode.
查询完成时释放连接池。此模式下不允许跨越多个语句的事务

需根据业务情况进行设定。

另外,经过测试,如果使用pgbouncer,postgresql原本的pg_hba.conf将完全无效,因为所有ip都是通过pgbouncer连入数据库。安全问题需要从其他方面进行处理。

生成密码文件:

[root@plat-ecloud01-mgmt-monitor04 ~]# /etc/pgbouncer/mkauth.py /etc/pgbouncer/userlist.txt "host=localhost user=postgres password=oracle"
[root@plat-ecloud01-mgmt-monitor04 ~]# cat /etc/pgbouncer/userlist.txt 
"postgres" "md53175bce1d3201d16594cebf9d7eb3f9d" ""
"test_user" "md5acd83f81d02d585fa2459ad8008a78e9" ""

改权限,由于我postgresql数据库设置的socket文件位置为/var/run/postgresql,需要放开权限,来让pgbouncer用户在里面写自己的连接信息:

扫描二维码关注公众号,回复: 10741724 查看本文章
[root@plat-ecloud01-mgmt-monitor04 ~]# chmod -R 777 /var/run/postgresql/
[root@plat-ecloud01-mgmt-monitor04 ~]# chown -R pgbouncer:pgbouncer /etc/pgbouncer/*

启动:

[pgbouncer@plat-ecloud01-mgmt-monitor04 ~]$ pgbouncer -d /etc/pgbouncer/pgbouncer.ini 
2019-12-19 14:24:03.674 CST [22563] LOG stale pidfile, removing

验证

使用pgbouncer连接到数据库:

[root@plat-ecloud01-mgmt-monitor04 ~]# psql -U test_user -p 6432 test_db
Password for user test_user: 
psql (12.1)
Type "help" for help.

test_db=> \l
                              List of databases
   Name    |   Owner   | Encoding | Collate | Ctype |    Access privileges    
-----------+-----------+----------+---------+-------+-------------------------
 postgres  | postgres  | UTF8     | C       | C     | 
 template0 | postgres  | UTF8     | C       | C     | =c/postgres            +
           |           |          |         |       | postgres=CTc/postgres
 template1 | postgres  | UTF8     | C       | C     | =c/postgres            +
           |           |          |         |       | postgres=CTc/postgres
 test      | postgres  | UTF8     | C       | C     | 
 test_db   | test_user | UTF8     | C       | C     | =Tc/test_user          +
           |           |          |         |       | test_user=CTc/test_user
(5 rows)

另外pgbouncer对外提供了一个虚拟数据库pgbouncer用来管理,之所以成为虚拟数据库,是因为它可以提供像PostgreSQL那样的数据库操作界面,但是这个数据库却并不是真实存在的。而是pgbouncer虚拟出来的一个命令行界面。

[pgbouncer@plat-ecloud01-mgmt-monitor04 ~]$ psql -p 6432 pgbouncer
psql (12.1, server 1.12.0/bouncer)
Type "help" for help.

pgbouncer=# show help;
NOTICE:  Console usage
DETAIL:  
        SHOW HELP|CONFIG|DATABASES|POOLS|CLIENTS|SERVERS|USERS|VERSION
        SHOW FDS|SOCKETS|ACTIVE_SOCKETS|LISTS|MEM
        SHOW DNS_HOSTS|DNS_ZONES
        SHOW STATS|STATS_TOTALS|STATS_AVERAGES|TOTALS
        SET key = arg
        RELOAD
        PAUSE [<db>]
        RESUME [<db>]
        DISABLE <db>
        ENABLE <db>
        RECONNECT [<db>]
        KILL <db>
        SUSPEND
        SHUTDOWN

SHOW
pgbouncer=# show clients;
 type |   user    | database  | state  | addr | port | local_addr | local_port |      connect_time       |      request_time       | wait | wait_us 
| close_needed |   ptr    | link | remote_pid | tls 
------+-----------+-----------+--------+------+------+------------+------------+-------------------------+-------------------------+------+---------
+--------------+----------+------+------------+-----
 C    | pgbouncer | pgbouncer | active | unix | 6432 | unix       |       6432 | 2019-12-19 14:41:32 CST | 2019-12-19 14:41:40 CST |    5 |  483185 
|            0 | 0x9ec340 |      |      23567 | 
(1 row)

pgbouncer=# show pools;
 database  |   user    | cl_active | cl_waiting | sv_active | sv_idle | sv_used | sv_tested | sv_login | maxwait | maxwait_us | pool_mode 
-----------+-----------+-----------+------------+-----------+---------+---------+-----------+----------+---------+------------+-----------
 pgbouncer | pgbouncer |         1 |          0 |         0 |       0 |       0 |         0 |        0 |       0 |          0 | statement
(1 row)
发布了136 篇原创文章 · 获赞 58 · 访问量 36万+

猜你喜欢

转载自blog.csdn.net/sunbocong/article/details/103610774