【问题标题】:Why am I getting sqlalchemy.exc.OperationalError when trying to connect to Google Cloud MySQL db from a local google cloud function?为什么我在尝试从本地谷歌云功能连接到谷歌云 MySQL 数据库时收到 sqlalchemy.exc.OperationalError?
【发布时间】:2020-05-28 03:23:13
【问题描述】:

这是我的 GCP 云函数“应用”的要点:

ma​​in.py

import sqlalchemy
import logging
import os
from time import perf_counter

def main(data, context):
    log = logging.getLogger("course_gen")
    db = sqlalchemy.create_engine(
        sqlalchemy.engine.url.URL(
            drivername="mysql+pymysql",
            username=os.environ.get("DB_USER"),
            password=os.environ.get("DB_PASS"),
            host="**.***.**.***", # this is actually the public IP of my cloud mysql instance
            port=3306,
            database="table_name"
        ),
        pool_size=5,
        max_overflow=2,
        pool_timeout=30,
        pool_recycle=1800
    )
    with db.connect() as cursor:
        start_time = perf_counter()

if __name__ == '__main__':
    main('data', 'context')

这是我复制 IP 的 Cloud MySQL 实例的相应概述:

port kwarg 有点令人困惑,但根据我从 this 之类的帖子推断,它始终是 3306。

基本上,当我在本地运行我的云功能时,我希望它能够连接到我配置的实时 GCP MySQL 实例,但我得到的完整错误是:

sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2003, "Can't connect to MySQL server on (timed out)")

【问题讨论】:

    标签: python mysql google-cloud-platform google-cloud-functions


    【解决方案1】:

    所以我实际上是在做一些研究时发现了这一点 - 基本上我必须遵循这个指南:

    https://cloud.google.com/sql/docs/mysql/quickstart-proxy-test#windows-64-bit

    在我的个人机器上设置基本上是某种本地运行代理

    (看起来像这样)

    $ ./cloud_sql_proxy -instances=********:us-east1:********=tcp:3306
    2020/05/27 22:36:06 Listening on 127.0.0.1:3306 for ********:us-east1:********
    2020/05/27 22:36:06 Ready for new connections
    2020/05/27 22:37:05 New connection for "********:us-east1:********"
    2020/05/27 22:37:05 Client closed local connection on 127.0.0.1:3306
    

    并将主机设置为localhost127.0.0.1,通过代理的魔力最终到达真正的云 MySQL 实例。瞧,没有更多错误了。

    【讨论】:

      猜你喜欢
      • 2019-12-08
      • 2019-05-12
      • 2018-12-01
      • 2020-05-18
      • 2021-01-25
      • 2022-12-17
      • 2020-03-18
      • 2021-10-05
      • 2021-03-18
      相关资源
      最近更新 更多