• pgbouncer 使用


    简介

    pgbouncer 是一个 PostgreSQL 连接池。任何目标应用程序都可以像连接 PostgreSQL 服务器一样连接到pgbouncer,并且 pgbouncer 将创建到实际服务器的连接,或者重用其现有的连接。pgbouncer 的目的是降低打开新连接到 PostgreSQL 的性能影响。

    连接池

    PostgreSQL允许我们通过参数max_connections来控制最大连接数 ,但是如果为前端应用的每次请求都分配一个新的DB连接,那么DB服务端可能在链接风暴到来时需要一次性提供大量的连接插槽,这会严重消耗服务端的内存,并且会导致性能急剧下降。

    在高并发请求的应用场景,为了保护DB,取而代之的我们可以利用连接池的连接复用特性,来避免DB被突然到来的连接洪峰淹没。这样,当应用程序一次性发送成千上万个查询的时候,如果超过了DB的处理能力,请求将在会连接池中排队,而不会导致后端PostgreSQL崩溃。

    与突然分配大量DB连接相比,这种在连接池中配置固定数量的连接处理来接收请求,查询的性能要好得多。另外,客户端的连接非常轻巧,除了文件描述符外基本没有其他消耗。但我们服务端连接却很重,需要适当配置。用较少的服务器连接来支持大量的客户端连接,是我们在PostgreSQL使用pgouncer的主要用

    作用

    能够缓存和PostgreSQL的连接,当有连接请求进来的时候,直接分配空闲进程,而不需要PostgreSQL fork出新进程来建立连接,以节省创建新进程,创建连接的资源消耗。

    能够有效提高连接的利用率,避免过多的无效连接,导致数据库消耗资源过大,CPU占用过高。

    对客户端连接进行限制,预防过多或恶意的连接请求。

    特点

    C语言编写,效率高,内存消耗低(默认为2k/连接),因为Bouncer不需要每次都接受完整的数据包

    可以把不同的数据库连接到一个机器上,而对客户端保持透明

    支持在线的重新配置而无须重启

    仅支持V3协议,因此后端版本须>=7.4

    使用libevent进行socket通信,通信效率高。

    下载路径

    http://www.pgbouncer.org/downloads/
    
    • 1

    安装

    依赖

    GNU Make 3.81+
    Libevent 2.0+
    pkg-config
    OpenSSL 1.0.1+ for TLS support
    (optional) c-ares as alternative to Libevent’s evdns
    (optional) PAM libraries
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 安装依赖
    yum install -y libevent libevent-devel 
    yum install -y openssl-devel
    
    • 1
    • 2

    编译安装

    /configure  --prefix=/usr/local/pgbouncer  
    * 如果 openssl 安装失败,可以不带 openssl 选项
    /configure  --prefix=/usr/local/pgbouncer --without-openssl
    make && make install
    
    • 1
    • 2
    • 3
    • 4

    环境变量配置

    echo 'export PATH=/usr/local/pgbouncer/bin:$PATH' >>/etc/profile
    source /etc/profile
    
    • 1
    • 2
    • 测试
    [root@localhost pgbouncer-1.17.0]# pgbouncer -V
    pgbouncer: /usr/local/lib/libssl.so.10: no version information available (required by pgbouncer)
    PgBouncer 1.17.0
    libevent 2.0.21-stable
    adns: c-ares 1.10.0
    tls: OpenSSL 1.0.2k-fips  26 Jan 2017
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 配置
     mkdir -p /etc/pgbouncer
     
    cat >> /etc/pgbouncer/userlist.txt <<"EOF"
    "postgres" :"passwd"
    EOF
     
    cat >> /etc/pgbouncer/pgbouncer.ini <<"EOF"
    [databases]
    *= host=207.207.35.100 port=5432 dbname=postgres user=postgres
    
    [pgbouncer]
    listen_port = 6432
    listen_addr = 207.207.35.100
    auth_type = md5
    auth_file = /etc/pgbouncer/userlist.txt
    logfile = /etc/pgbouncer/pgbouncer.log
    pidfile = /etc/pgbouncer/pgbouncer.pid
    admin_users = postgres
    stats_users = postgres
    server_reset_query = DISCARD ALL
    server_check_query = select 1
    server_check_delay = 30
    max_client_conn = 5000
    default_pool_size = 20
    reserve_pool_size = 5
    dns_max_ttl = 15
    pool_mode=transaction
    EOF
    
     chown postgres.postgres -R /etc/pgbouncer
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 启动
    su - postgres 
    pgbouncer -d /etc/pgbouncer/pgbouncer.ini
    
    • 1
    • 2
    • 测试远程连接
    [root@localhost ~]# /home/postgres/pgsql/bin/psql -U postgres -h 207.207.35.100 -p 6432 -d postgres -c "select version()"
    Password for user postgres:
                                       version
    ------------------------------------------------------------------------------------------------------------
     PostgreSQL 11.8 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 7.5.0, 64-bit
    (1 row)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    参考:
    https://www.modb.pro/db/104414
    https://www.cnblogs.com/haha029/p/15626042.html

  • 相关阅读:
    如何在快应用中实现滑动操作组件
    使用Maven 构建、开发和打包 JavaFX 项目
    组会记录2023/9/28
    0~n-1缺失的数字(注意尾巴也有可能)
    多功能便携式吸尘器设计
    uniapp 事件委托失败 获取不到dataset
    简单的反弹shell到全交互式shell
    服务网格Service Mesh和Istio
    探索UWB模块的多功能应用——UWB技术赋能智慧生活
    SpringBoot+Vue项目流浪狗领养管理系统的设计与实现
  • 原文地址:https://blog.csdn.net/yueludanfeng/article/details/125435085