更新:
我重新派生了GoogleCloudPlatform示例项目,然后重试。突然间,它开始工作了。
我的问题有一半与我的目标项目使用Flask SQLAlchemy有关。为此,我需要使用MySQLdb
方言,如下所示:https://stackoverflow.com/a/10900826/4455571
我仍然不确定为什么我第一次不能让GoogleCloudPlatform示例工作,以及为什么它在我重新分叉后突然开始工作。
原始帖子:
我正在尝试使用以下指南将运行在Cloud Run上的容器连接到Cloud SQL:https://cloud.google.com/sql/docs/mysql/connect-run?hl=en_US
我已经确保做到以下几点:
- 为我的项目启用API
- 将"云SQL客户端"角色添加到我的服务帐户REDACTED-compute@developer.gserviceaccount.com
但是,连接失败并出现以下错误:
文件"/usr/local/lib/python3.8/site packages/pymysql/connections.py",第630行,在连接中引发exc-sqlalchemy.exc.OperationalError:(pymysql.er.OperationalError((2003,"无法连接到MySQL服务器在"localhost"上([Erno 2]没有这样的文件或目录("(
我使用GitHub中的示例代码进行了第二次尝试:https://github.com/GoogleCloudPlatform/python-docs-samples/tree/master/cloud-sql/mysql/sqlalchemy
以下是我遵循的步骤:
- 镜像存储库并将其设为私有存储库
- 编辑了
app.yaml
以添加类似于以下内容的凭据(我没有用引号括起值-应该吗?(:
env_variables:
CLOUD_SQL_CONNECTION_NAME: my-project-270323:us-central1:database
DB_USER: root
DB_PASS: areallygreatpassword
DB_NAME: database
- 将回购连接到云构建并触发新的构建
- 在Cloud Run上的新服务中部署了我的容器,确保在Connections>Cloud SQL Connections下按数据库进行选择
我得到了同样的错误。我做错了什么?
编辑
以下是完整的错误转储:
Traceback (most recent call last):()
File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 2446, in wsgi_app response = self.full_dispatch_request()()
File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 1944, in full_dispatch_request self.try_trigger_before_first_request_functions()()
File "/usr/local/lib/python3.8/site-packages/flask/app.py", line 1992, in try_trigger_before_first_request_functions func()()
File "/app/main.py", line 81, in create_tables with db.connect() as conn:()
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 2209, in connect return self._connection_cls(self, **kwargs)()
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 103, in __init__ else engine.raw_connection()()
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 2306, in raw_connection return self._wrap_pool_connect(()
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 2279, in _wrap_pool_connect Connection._handle_dbapi_exception_noconnection(()
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1547, in _handle_dbapi_exception_noconnection util.raise_from_cause(sqlalchemy_exception, exc_info)()
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/util/compat.py", line 398, in raise_from_cause reraise(type(exception), exception, tb=exc_tb, cause=cause)()
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/util/compat.py", line 152, in reraise raise value.with_traceback(tb)()
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 2276, in _wrap_pool_connect return fn()()
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 303, in unique_connection return _ConnectionFairy._checkout(self)()
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 773, in _checkout fairy = _ConnectionRecord.checkout(pool)()
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 492, in checkout rec = pool._do_get()()
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/pool/impl.py", line 139, in _do_get self._dec_overflow()()
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/util/langhelpers.py", line 68, in __exit__ compat.reraise(exc_type, exc_value, exc_tb)()
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/util/compat.py", line 153, in reraise raise value()
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/pool/impl.py", line 136, in _do_get return self._create_connection()()
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 308, in _create_connection return _ConnectionRecord(self)()
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 437, in __init__ self.__connect(first_connect_check=True)()
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 652, in __connect connection = pool._invoke_creator(self)()
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/strategies.py", line 114, in connect return dialect.connect(*cargs, **cparams)()
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/default.py", line 489, in connect return self.dbapi.connect(*cargs, **cparams)()
File "/usr/local/lib/python3.8/site-packages/pymysql/__init__.py", line 94, in Connect return Connection(*args, **kwargs)()
File "/usr/local/lib/python3.8/site-packages/pymysql/connections.py", line 325, in __init__ self.connect()()
File "/usr/local/lib/python3.8/site-packages/pymysql/connections.py", line 630, in connect raise exc sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2003, "Can't connect to MySQL server on 'localhost' ([Errno 2] No such()
File or directory)")
此外,我是通过web控制台而不是CLI来完成此操作的。以下是我传递给谷歌提供的示例项目的设置:
drive.google.com/open?id=17nl_rQVTU2ZirCe64bjfe90zGweg3a6drive.google.com/open?id=1_Riy1HNSPvZZUGl4tJ0Z8puYuuMbUMPH
这个问题包括一些广泛的研究,做得很好。直接解决几个问题:
app.yaml
文件特定于应用程序引擎,是将环境变量配置传递给应用程序的方法。在Cloud Run中,在部署时使用--update-env-vars标志- 您的服务部署操作中也可能缺少一个标志:
--add-cloudsql-instances INSTANCE-CONNECTION-NAME
这些步骤加在一起:
gcloud run deploy SERVICE --image gcr.io/PROJECT/SERVICE
--add-cloudsql-instances my-project-270323:us-central1:database
--update-env-vars CLOUD_SQL_CONNECTION_NAME=my-project-270323:us-central1:database
--update-env-vars DB_USER=root
--update-env-vars DB_PASS=areallygreatpassword
--update-env-vars DB_NAME=database
请注意,INSTANCE_CONNECTION_NAME中的database
是Cloud SQL实例,而作为DB_NAME的database
是在该实例中创建的数据库。
配置值需要用作创建数据库连接的一部分。
您可以在Github上的完整示例代码中看到更多这些环境变量之间的连接以及创建连接对象的信息。