pgbouncer learning

pgbouncer learning

I have been using pgpool as the connection pool before. Recently I learned that there is a lighter tool pgbouncer. Try to build it and do simple learning.

installation

Install directly using yum, download the repo:

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

Run the yum installation command:

yum install pgbouncer -y

Or pull the installation from git:

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

Configuration

Build test users and libraries:

[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

To modify the pgbouncer.ini file, please refer to the official instructions: 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到数据库的连接

Description: pool_mode has three modes, the official description is as follows:

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.
查询完成时释放连接池。此模式下不允许跨越多个语句的事务

Need to be set according to business conditions.

In addition, after testing, if you use pgbouncer, postgresql's original pg_hba.conf will be completely invalid, because all ip is connected to the database through pgbouncer. Security issues need to be addressed from other aspects.

Generate a password file:

[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" ""

Change the permissions. Since the socket file location of my postgresql database is / var / run / postgresql, I need to release the permissions to let pgbouncer users write their own connection information:

[root@plat-ecloud01-mgmt-monitor04 ~]# chmod -R 777 /var/run/postgresql/
[root@plat-ecloud01-mgmt-monitor04 ~]# chown -R pgbouncer:pgbouncer /etc/pgbouncer/*

start up:

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

verification

Use pgbouncer to connect to the database:

[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)

In addition, pgbouncer provides a virtual database pgbouncer for management. The reason why it becomes a virtual database is because it can provide a database operation interface like PostgreSQL, but this database does not really exist. It is a command line interface virtualized by 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)
Published 136 original articles · Like 58 · Visits 360,000+

Guess you like

Origin blog.csdn.net/sunbocong/article/details/103610774