You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker容器中Pgbouncer请求分配异常问题排查求助

数据库请求无法通过HAProxy转发至Pgbouncer排查方案

问题现象

  • 容器集群整体运行正常,但所有数据库请求均无法分发到Pgbouncer
  • 执行docker logs <pgbouncer-container-id>查看日志,无任何请求统计数据,即使已发起多次数据库请求
  • 排查HAProxy与docker-compose配置后未定位路由问题
  • 通过telnet连接HAProxy的数据库转发端口后,HAProxy日志出现请求转发记录,但Pgbouncer仍无有效请求统计

附相关配置与日志

HAProxy配置文件示例

frontend db_frontend
    bind *:5432
    mode tcp
    default_backend pgbouncer_backend

backend pgbouncer_backend
    mode tcp
    balance roundrobin
    server pgbouncer1 pgbouncer:6432 check inter 5s rise 2 fall 3

docker-compose配置文件示例

version: '3.8'
services:
  haproxy:
    image: haproxy:latest
    ports:
      - "5432:5432"
    volumes:
      - ./haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg
    depends_on:
      - pgbouncer

  pgbouncer:
    image: edoburu/pgbouncer:latest
    environment:
      - DB_HOST=postgres
      - DB_PORT=5432
      - DB_USER=admin
      - DB_PASSWORD=secret
      - POOL_MODE=transaction
    ports:
      - "6432:6432"
    depends_on:
      - postgres

  postgres:
    image: postgres:15-alpine
    environment:
      - POSTGRES_USER=admin
      - POSTGRES_PASSWORD=secret
      - POSTGRES_DB=appdb

日志信息

HAProxy日志(telnet连接后)

Feb 10 12:34:56 haproxy[123]: 192.168.1.100:51234 [10/Feb/2024:12:34:56.789] db_frontend pgbouncer_backend/pgbouncer1 0/0/1/1/2 200 123 - - ---- 1/1/0/0/0 0/0 "POSTGRES"

Pgbouncer日志(无请求统计)

2024-02-10 12:30:00.123 [1] LOG listening on 0.0.0.0:6432
2024-02-10 12:30:00.124 [1] LOG listening on [::]:6432
2024-02-10 12:30:00.125 [1] LOG process up: PgBouncer 1.21.0, libevent 2.1.12-stable (epoll), adns: c-ares 1.18.1, tls: OpenSSL 1.1.1w  11 Sep 2023

排查步骤

  1. 验证Pgbouncer端口可达性
    在HAProxy容器内执行telnet pgbouncer 6432,确认能正常建立连接。若连接失败,检查docker-compose网络配置,确保HAProxy与Pgbouncer处于同一网络(默认docker-compose会创建专属网络,自定义网络需确认服务已加入)。

  2. 检查Pgbouncer用户认证配置
    Pgbouncer默认需用户认证,若客户端请求未携带正确的用户名/密码,会被静默拒绝且无日志记录。检查userlist.txt配置,确保包含客户端使用的数据库用户,且密码与后端PostgreSQL匹配:

    "admin" "secret"
    
  3. 确认HAProxy TCP转发配置

    • 确保frontend与backend均设置为mode tcp(PostgreSQL为TCP协议,不可用http模式)
    • 核对backend的server地址:docker-compose中服务名pgbouncer可直接解析,端口需对应Pgbouncer监听端口(默认6432)
    • 添加option tcplog到frontend配置,查看完整TCP请求细节,确认请求是否完整转发至Pgbouncer
  4. 检查Pgbouncer连接池配置
    查看pgbouncer.ini,确认listen_addr设置为0.0.0.0(允许所有IP连接),auth_type配置正确(测试阶段可临时设为trust排除认证问题)。

  5. 直接测试Pgbouncer到PostgreSQL的连通性
    在Pgbouncer容器内执行psql -h localhost -p 6432 -U admin appdb,确认能正常连接后端PostgreSQL。若连接失败,优先解决Pgbouncer与PostgreSQL的连通问题。

内容的提问来源于stack exchange,提问作者eurus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 16:07:20