无法使用 PyMySQL 连接到云 SQL
Can't connect to Cloud SQL using PyMySQL
我正在尝试从 Python 应用程序(使用 PyMySQL 0.7.9)运行 在 [=27= 之上连接到云 SQL ] 应用引擎。
我的连接字符串如下所示(凭据当然是假的):
pymysql.connect(unix_socket='/cloudsql/gae_project_name:cloudsql_instance_name',
user='user', password='', db='database_name')
我收到的错误信息是:
OperationalError: (2003, "Can't connect to MySQL server on 'localhost' ([Errno 97] Address family not supported by protocol)")
就像 PyMySQL 无法识别我正在尝试通过 Unix 套接字进行连接,而是尝试使用主机参数的默认值(我认为是 localhost
)
我可以使用相同的连接字符串连接到 MySQLdb。
那为什么不使用 MySQLdb 呢?
我在使用 PyMySQL 部署 Flask 应用程序时遇到了同样的问题,我尝试了很多修复都没有成功。我的解决方法是改用 MySQLDb 啊哈..!
显然,Google App Engine 标准环境目前不支持 PyMySQL,该环境仅运行 Python 2.7(截至 2018 年 6 月)。这是来自 GCP python 项目的维护者:
I can confirm that pymysql is not supported in the python27 runtime. However, for most use cases, it's possible to use pymysql locally and mysqldb in production by using a try: / except ImportError: to import one or the other conditionally. As they share the same interface, you can use import as to make the two different libraries share the same name for ease of use in your code.
查看此Github thread了解详情
我使用 Python3 Flex App Engine 和 PyMySQL 连接它。
这是我的 app.yaml 的样子:
runtime: python
env: flex
entrypoint: gunicorn -b :$PORT api:app --timeout 180
runtime_config:
python_version: 3
env_variables:
SQLALCHEMY_DATABASE_URI: >-
mysql+pymysql://user:password@/database?unix_socket=/cloudsql/project-name:us-central1:instance-name
beta_settings:
cloud_sql_instances: us-central1:instance-name
# This sample incurs costs to run on the App Engine flexible environment.
# The settings below are to reduce costs during testing and are not appropriate
# for production use. For more information, see:
# https://cloud.google.com/appengine/docs/flexible/python/configuring-your-app-with-app-yaml
manual_scaling:
instances: 1
resources:
cpu: 1
memory_gb: 0.5
disk_size_gb: 10
确保替换 env_variables 和 beta_settings 中的用户、密码、数据库和实例连接。
这是我的 python:
import pymysql.cursors
import pymysql
connection = pymysql.connect(unix_socket='/cloudsql/project-name:us-central1:instance-name',
user='user',
password='password',
db='database',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
我正在尝试从 Python 应用程序(使用 PyMySQL 0.7.9)运行 在 [=27= 之上连接到云 SQL ] 应用引擎。
我的连接字符串如下所示(凭据当然是假的):
pymysql.connect(unix_socket='/cloudsql/gae_project_name:cloudsql_instance_name',
user='user', password='', db='database_name')
我收到的错误信息是:
OperationalError: (2003, "Can't connect to MySQL server on 'localhost' ([Errno 97] Address family not supported by protocol)")
就像 PyMySQL 无法识别我正在尝试通过 Unix 套接字进行连接,而是尝试使用主机参数的默认值(我认为是 localhost
)
我可以使用相同的连接字符串连接到 MySQLdb。
那为什么不使用 MySQLdb 呢?
我在使用 PyMySQL 部署 Flask 应用程序时遇到了同样的问题,我尝试了很多修复都没有成功。我的解决方法是改用 MySQLDb 啊哈..!
显然,Google App Engine 标准环境目前不支持 PyMySQL,该环境仅运行 Python 2.7(截至 2018 年 6 月)。这是来自 GCP python 项目的维护者:
I can confirm that pymysql is not supported in the python27 runtime. However, for most use cases, it's possible to use pymysql locally and mysqldb in production by using a try: / except ImportError: to import one or the other conditionally. As they share the same interface, you can use import as to make the two different libraries share the same name for ease of use in your code.
查看此Github thread了解详情
我使用 Python3 Flex App Engine 和 PyMySQL 连接它。
这是我的 app.yaml 的样子:
runtime: python
env: flex
entrypoint: gunicorn -b :$PORT api:app --timeout 180
runtime_config:
python_version: 3
env_variables:
SQLALCHEMY_DATABASE_URI: >-
mysql+pymysql://user:password@/database?unix_socket=/cloudsql/project-name:us-central1:instance-name
beta_settings:
cloud_sql_instances: us-central1:instance-name
# This sample incurs costs to run on the App Engine flexible environment.
# The settings below are to reduce costs during testing and are not appropriate
# for production use. For more information, see:
# https://cloud.google.com/appengine/docs/flexible/python/configuring-your-app-with-app-yaml
manual_scaling:
instances: 1
resources:
cpu: 1
memory_gb: 0.5
disk_size_gb: 10
确保替换 env_variables 和 beta_settings 中的用户、密码、数据库和实例连接。
这是我的 python:
import pymysql.cursors
import pymysql
connection = pymysql.connect(unix_socket='/cloudsql/project-name:us-central1:instance-name',
user='user',
password='password',
db='database',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)