• 如何在一台服务器同一个端口运行多个pgbouncer


    PGbouncer是Postgresql数据库最常用的一款连接池软件,但是它是单进程的,所以只能占用一颗CPU资源,会造成CPU资源的浪费。PGbouncer有方法在同一台服务器的同一个端口运行多个进程实例,可以让资源得到充分利用。
    先看下一个pgbouncer的作用,如下图:
    在这里插入图片描述
    通过pgbouncer连接池,最终到达数据库的连接会少很多,这样就不怕连接风暴打爆数据库了。

    如下图,可以配置多个pgbouncer在同一台机器的同一个端口,充分利用系统资源

    在这里插入图片描述

    如何配置?
    修改pgbouncer.ini配置文件加入so_reuseport = 1
    使用yum安装pgbouncer后,复制

    cp pgbouncer.service pgbouncer@.service
    systemctl daemon-reload
    systemctl start pgbouncer@1
    systemctl start pgbouncer@2
    
    • 1
    • 2
    • 3
    • 4

    如下,pgbouncer@.service内容

    cat pgbouncer@.service
    [Unit]
    Description=A lightweight connection pooler for PostgreSQL (%i)
    #After=syslog.target
    After=network.target
    
    
    [Service]
    #RemainAfterExit=yes
    
    User=pgbouncer
    Group=pgbouncer
    
    # Path to the init file
    Environment=BOUNCERCONF=/etc/pgbouncer/pgbouncer.ini
    
    #PIDFile=/var/run/pgbouncer/pgbouncer.pid
    
    # Where to send early-startup messages from the server 
    # This is normally controlled by the global default set by systemd
    # StandardOutput=syslog
    
    ExecStart=/usr/bin/pgbouncer ${BOUNCERCONF}
    ExecReload=/usr/bin/kill -HUP $MAINPID
    KillSignal=SIGINT
    
    [Install]
    WantedBy=multi-user.target
    
    • 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

    另外由于
    https://github.com/systemd/systemd/commit/54255c64e6d223deb7d3863e426e78c443fda37c的原因,systemd中的ReusePort选项在222之前的版本中不能用于此目的。RHEL7无法实现该功能,需要RHEL8。

    我的测试机版本信息如下,无法实现该功能

    #操作系统版本
    lsb_release -a
    LSB Version:    :core-4.1-amd64:core-4.1-noarch
    Distributor ID: CentOS
    Description:    CentOS Linux release 7.9.2009 (Core)
    Release:        7.9.2009
    Codename:       Core
    
    #systemctl版本
    systemctl  --version
    systemd 219
    +PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    另外,有一个多线程的连接池可以替换需要多个pgbouncer的情况,可以参考https://github.com/yandex/odyssey测试一下。

    参考:
    https://www.2ndquadrant.com/en/blog/running-multiple-pgbouncer-instances-with-systemd/
    http://www.pgbouncer.org/config.html
    https://github.com/yandex/odyssey

  • 相关阅读:
    软考通过率低吗?怎么备考?
    `算法题解` `AcWing` 201. 可见的点
    [Linux系统编程]_进程的通信(三)
    前端css实现水平居中、垂直居中、水平垂直居中【木鱼精简】
    “蔚来杯“2022牛客暑期多校训练营8 D题: Poker Game: Decision
    牛客 HJ18 识别有效的IP地址和掩码并进行分类统计
    Mysql高级——索引
    博客项目(前台功能实现)
    LC1713. 得到子序列的最少操作次数(java - 动态规划)
    有关于UDP
  • 原文地址:https://blog.csdn.net/dazuiba008/article/details/128119287