【问题标题】:App engine+Postgresql getting "Connection refused" error应用引擎+Postgresql 出现“连接被拒绝”错误
【发布时间】:2021-11-27 03:35:39
【问题描述】:

我是一个新手,试图在标准 App 引擎上部署一个玩具 django 应用程序,我收到以下错误。

在本地运行应用程序

当我使用 127.0.0.1 或公共 IP 作为“主机”地址时,我的应用在本地使用云 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 实例使用公网 IP 会导致超时。

我的 settings.py 看起来像这样

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

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

{'数据库':'我的项目', '主机': '/cloudsql/asim800:us-central1:django-app1', '密码': 'pblogsu', “用户”:“隐藏”}

我已启用 Google Cloud SQL API。不确定如何调试 Google 连接名称映射到主机/端口地址。 我在这里看到过类似的问题(https://github.com/GoogleCloudPlatform/python-docs-samples/issues/870),但没有任何建议对我有帮助。 任何帮助将不胜感激。

PS:我已经大量编辑了我的问题。之前我使用过 postgesql,结果非常相似。

【问题讨论】:

    标签: mysql django google-app-engine connection


    【解决方案1】:

    我终于发现需要在 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"
    

    感谢您的帮助。如果其他人遇到此问题,我会很乐意添加任何详细信息。

    【讨论】:

      【解决方案2】:

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

      • 服务器必须与客户端运行在同一台机器上

      • cloudsql/asim800:us-central1:django-app1 必须在 unix_socket_directories

      • port 必须是 5432

      【讨论】:

      • 由于我正在使用 django,我不确定我是否可以直接访问 unix_socket_directories。有趣的问题是如何确保服务器与应用引擎在同一台机器上运行,因为我正在使用 Google Cloud。我也曾在 GCP 上尝试过 MySQL,但遇到了类似的错误。谢谢
      • 然后使用 TCP 连接,而不是通过 Unix 套接字。
      • 没错,我希望通过使用 HOST: '/cloudsql/asim800:us-central1:django-app1' 来获得 TCP 连接。我也尝试了公共 IP 地址,但这会导致连接超时。我可以使用上面的主机从本地计算机连接到 mysql,但不确定当我的应用程序在 GCP 应用程序引擎上运行时如何使其工作。再次非常感谢。
      • 任何以斜线开头的内容都被解释为包含 Unix 套接字的目录。
      猜你喜欢
      • 2017-10-12
      • 1970-01-01
      • 1970-01-01
      • 2017-08-05
      • 1970-01-01
      • 1970-01-01
      • 2021-07-01
      • 2012-06-05
      • 1970-01-01
      相关资源
      最近更新 更多