问题描述
我正在尝试为我的 APPEngine 帐户使用云 SQL/mysql 实例.该应用程序是一个 python django-2.1.5 appengine 应用程序.我在谷歌云中创建了一个 MYSQL 实例.
I am trying to use cloud SQL / mysql instance for my APPEngine account. The app is an python django-2.1.5 appengine app. I have created an instance of MYSQL in the google cloud.
我在从 SQL 实例详细信息复制的 app.yaml 文件中添加了以下内容:
I have added the following in my app.yaml file copied from the SQL instance details:
beta_settings:
cloud_sql_instances: <INSTANCE_CONNECTION_NAME>=tcp:<TCP_PORT>
我已为我的 appengine 项目 xxx-app 的所有者 xxx-
[email protected]
授予了Cloud SQL Client
权限.我创建了一个特定于应用程序XYZ
的数据库用户帐户,它可以连接到所有主机(* 选项)I have given rights for my appengine project xxx-app's owner xxx-
[email protected]
theCloud SQL Client
rights. I have created a DB user account specific for the appXYZ
which can connect to all hosts (* option)我在 settings.py 中的连接详细信息如下:
My connection details in settings.py are the following:
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'my-db', 'USER': 'appengine', 'PASSWORD': 'xxx', 'HOST': '111.111.11.11', # used actual ip 'PORT': '3306' } }
- 我也按照 https://github.com/GoogleCloudPlatform/appengine-django-skeleton/blob/master/mysite/settings.py:
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'HOST': '/cloudsql/<your-project-id>:<your-cloud-sql-instance>', 'NAME': '<your-database-name>', 'USER': 'root', } }
- 我也无法从本地连接.但是,如果我对本地 IP 执行
Add Network
然后尝试连接本地连接.使用 CIDR 表示法添加本地 IP 地址的网络后,该应用在本地运行良好. - I cannot connect from the local as well. However, if I do a
Add Network
of my local IP and then try to connect the local connects. THe app runs fine locally after adding the network of local IP Address using CIDR notation. - 如果不添加 AppEngine 分配的 IP 地址,我将无法连接到 Cloud sql.它给了我一个错误:
我的问题:
OperationalError: (2003, "Can't connect to MySQL server on '0.0.0.0' ([Errno 111] Connection refused)")
- 在哪里可以找到应用引擎分配的 IP 地址.即使是暂时的,我也不介意.我知道如果我需要静态 IP 地址,我将不得不创建一个计算 VM 实例.
cloud_sql_instances:
在/cloudsql/
cloud_sql_instances:<INSTANCE_CONNECTION_NAME>=tcp:<TCP_PORT>
提供本地 tcp 端口(127.0.0.1:<TCP_PORT>
).cloud_sql_instances: <INSTANCE_CONNECTION_NAME>
provides a Unix socket at/cloudsql/<INSTANCE_CONNECTION_NAME>
cloud_sql_instances: <INSTANCE_CONNECTION_NAME>=tcp:<TCP_PORT>
provides a local tcp port (127.0.0.1:<TCP_PORT>
).
推荐答案
App Engine 对特定实例的 IP 地址没有任何保证,并且可能随时更改.由于它是一个无服务器平台,它抽象了基础设施,让您可以专注于您的应用程序.
App Engine doesn't have any guarantees regarding IP address of a particular instance, and may change at any time. Since it is a Serverless platform, it abstracts away the infrastructure to allow you to focus on your app.
使用 App Engine Flex 时有两个选项:Unix 域套接字和 TCP 端口.哪一种 App Engine 为您提供取决于您在 app.yaml 中指定它的方式:
There are two options when using App Engine Flex: Unix domain socket and TCP port. Which one App Engine provides for you depends on how you specify it in your app.yaml:
您可以在 从 App Engine 连接 页面.
这篇关于无法在 Django 2.x.x Python AppEngine 中连接到 MySQL 服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!