App Engine+Postgresql 出现 "Connection refused" 错误

App engine+Postgresql getting "Connection refused" error

我是一个新手,试图在标准 App Engine 上部署玩具 django 应用程序,但出现以下错误。

运行 本地应用程序

当我使用 127.0.0.1 或 Public IP 作为 'HOST' 地址时,我的应用程序 在本地使用云 SQL 正常运行。但是,如果我使用这样的 GCP 连接名称,我会收到此错误:

'HOST':  '/cloudsql/asim800:us-central1:django-mysql-1'

OperationalError: (2002, "Can't connect to local MySQL server through socket '/cloudsql/asim800:us-central1:django-mysql-1'

运行 GAE 上的应用

  1. 使用 HOST='/cloudsql/asim800:us-central1:django-mysql-1',我得到这个错误 2003,“无法连接到 'localhost' 上的 MySQL 服务器([Errno 111] 连接被拒绝)” - 不确定为什么我会收到 'localhost' 错误,但似乎有什么问题将 Google 连接名称转换为主机和端口信息时出错。

  2. 对 GAE 上的 mysql 实例使用 public IP 导致超时。

我的settings.py看起来像这样

    DATABASES = {
        'default': {
                'ENGINE': 'django.db.backends.mysql',
                'HOST': '/cloudsql/asim800:us-central1:django-mysql-1', 
                'NAME': 'myproject',
                'USER': 'pblogsu',
                'PASSWORD': '****',
        }
    }

我可以在以下日志中正确看到它们:

{'database': 'myproject', 'host': '/cloudsql/asim800:us-central1:django-app1', 'password': 'pblogsu', 'user': 'hidden'}

我已启用 Google 云 SQL API。不确定如何调试 Google 连接名称映射到 host/port 地址。 我曾在此处 (https://github.com/GoogleCloudPlatform/python-docs-samples/issues/870) 看到类似的问题报告,但 none 的建议对我有所帮助。 任何帮助将不胜感激。

PS:我已经对我的问题进行了大量编辑。早些时候我用过 postgesql,结果非常相似。

检查 PostgreSQL 服务器上的参数 unix_socket_directoriesport。为了您的连接尝试工作

  • 服务器必须 运行 与客户端

    在同一台机器上
  • cloudsql/asim800:us-central1:django-app1 必须在 unix_socket_directories

  • port 必须是 5432

我终于弄清楚需要在 IAM & Admin> IAM 中添加其中一个服务帐户(与 GAE 关联),然后赋予 Cloud sql.Client 角色。

事后看来,这听起来像是一个简单的解决方案,但需要深入挖掘才能解决问题。我怀疑可能存在权限问题,但我不确定需要添加哪个帐户。在日志中看到这个错误后,我得到了第一条线索:

"CloudSQL warning: your action is needed to update your application and avoid potential disruptions. Please see https://cloud.google.com/sql/docs/mysql/connect-overview for additional details: ensure that the account has access to "asim800:us-central1:django-mysql-1" (and make sure there's no typo in that name). Error during createEphemeral for asim800:us-central1:django-mysql-1: googleapi: Error 403: The client is not authorized to make this request., notAuthorized"

感谢您的帮助。如果有人 运行 进入这个问题,我很乐意添加任何细节。