【问题标题】:OperationalError: ERROR: pgbouncer cannot connect to serverOperationalError:错误:pgbouncer 无法连接到服务器
【发布时间】:2017-09-22 17:32:15
【问题描述】:

我正在尝试在 Django 安装上执行 python manage.py syncdb,但我不断收到 OperationalError: ERROR: pgbouncer cannot connect to server。 pgbouncer.log 包含如下行:

2017-09-19 19:44:15.107 1128 LOG C-0x8a9930: mydb/myuser@unix:6432 closing because: pgbouncer cannot connect to server (age=0)
2017-09-19 19:44:15.107 1128 WARNING C-0x8a9930: mydb/myuser@unix:6432 Pooler Error: pgbouncer cannot connect to server
2017-09-19 19:44:15.107 1128 LOG S-0x8c72e0: mydb/myuser@35.154.149.188:5432 new connection to server
2017-09-19 19:44:15.107 1128 LOG C-0x8a9930: mydb/myuser@unix:6432 login failed: db=mydb user=myuser
2017-09-19 19:44:30.108 1128 LOG S-0x8c72e0: mydb/myuser@35.154.149.188:5432 closing because: connect failed (age=15)

如果需要,ps -aef | grep pgbouncer 产生:

postgres  1128     1  0 18:38 ?        00:00:00 /usr/sbin/pgbouncer -d /etc/pgbouncer/pgbouncer.ini
myuser    1919  1533  0 19:45 pts/0    00:00:00 grep --color=auto pgbouncer

此外,grep port /etc/pgbouncer/pgbouncer.ini 会导致:

;;   dbname= host= port= user= password=
mydb = host=xx.xxx.xxx.xxx port=5432 dbname=mydb
;forcedb = host=127.0.0.1 port=300 user=baz password=foo client_encoding=UNICODE datestyle=ISO connect_query='SELECT 1'
listen_port = 6432

最后,settings.py 的相关部分包含:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'mydb',
        'USER': 'myuser',
        'PASSWORD': 'mypassword',
        'HOST': '/var/run/postgresql',
        'PORT': '6432',
    }

我在 postgresql.conf 中将 log_connections 转为 on,重新启动 PG 并再次尝试。以下是相关行:

2017-09-20 07:50:59 UTC LOG:  database system is ready to accept connections
2017-09-20 07:50:59 UTC LOG:  autovacuum launcher started
2017-09-20 07:51:00 UTC LOG:  connection received: host=[local]
2017-09-20 07:51:00 UTC LOG:  incomplete startup packet
2017-09-20 07:51:00 UTC LOG:  connection received: host=[local]
2017-09-20 07:51:00 UTC LOG:  connection authorized: user=postgres database=postgres
2017-09-20 07:51:01 UTC LOG:  connection received: host=[local]
2017-09-20 07:51:01 UTC LOG:  connection authorized: user=postgres database=postgres
2017-09-20 07:51:01 UTC LOG:  connection received: host=[local]
2017-09-20 07:51:01 UTC LOG:  connection authorized: user=postgres database=postgres

似乎连接正在通过,但userdatabase 的名称是postgres。这些凭据不是我在pgbouncer.ini 中提供的。

但是,在pgbouncer.ini 中描述的连接字符串中显式添加myuser 会导致:

2017-09-20 09:37:37 UTC FATAL:  Peer authentication failed for user "myuser"
2017-09-20 09:37:37 UTC DETAIL:  Connection matched pg_hba.conf line 90: "local   all             all                                     peer"

完全被难住了。

【问题讨论】:

  • PostgreSQL 日志对此有何评论? PostgreSQL 是否在目的地运行?
  • 您没有阅读我评论的第一部分。将log_connections 转为on 并检查PostgreSQL 日志。
  • settings.py 已将套接字目录定义为主机,但您在 /etc/pgbouncer/pgbouncer.ini 中有 localhost - 请在 settings.py 中将 /var/run/postgresql 更改为 127.0.0.1
  • @VaoTsun:啊,不,那没用。作为记录,我在 pgbouncer.ini 中设置了unix_socket_dir = /var/run/postgresql,所以套接字应该可以工作。当我执行sudo service pgbouncer start 时,可能pgbouncer.ini 没有正确加载?要么,要么这里有权限问题。你有什么感觉?
  • ps -aef | grep pgbouncer 显示正确的配置。你在auth_hba_file 有什么?..

标签: postgresql pgbouncer


【解决方案1】:

看来settings.py中的这一行出现了错误配置:

'PORT': '6432',

我评论了它并且 pgbouncer 开始工作。

虽然我不确定“为什么”。

也许这个端口发生了冲突;在我的例子中,Pgbouncer 和 PG 在一个服务器上共存。我过去在不同的虚拟机上设置它们没有任何障碍(并且无需评论'PORT': '6432',

【讨论】:

  • 这很奇怪。我怀疑通过评论 PORT 您直接连接到 postgresql
猜你喜欢
  • 2017-03-03
  • 2013-07-12
  • 2012-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-12
  • 1970-01-01
  • 2019-10-08
相关资源
最近更新 更多