【问题标题】:Google App Engine and Cloud SQL: Lost connection to MySQL server at 'reading initial communication packet' SQL 2nd GenGoogle App Engine 和 Cloud SQL:在“读取初始通信数据包”SQL 第 2 代时失去与 MySQL 服务器的连接
【发布时间】:2016-10-20 03:29:21
【问题描述】:

我收到了与本主题中其他帖子类似的错误。 我尝试从第一代 SQL 服务器切换到第二代 SQL 服务器(都在 us-central1 上),但它仍然不起作用。

我从项目顶部的 url 复制了我的 CLOUDSQL_PROJECT。
我从 SQL 页面的属性部分复制了我的 CLOUDSQL_INSTANCE。

在我的 main.py 中,我正在尝试运行 Google 示例代码,但它不起作用(当然,在本地它可以):

if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine/'):
        db = MySQLdb.connect(
            unix_socket='/cloudsql/{}:{}'.format(
                CLOUDSQL_PROJECT,
                CLOUDSQL_INSTANCE),
            user=user,passwd=password)
    # When running locally, you can either connect to a local running
    # MySQL instance, or connect to your Cloud SQL instance over TCP.
    else:
        db = MySQLdb.connect(host=host,user=user,passwd=password)
cursor = db.cursor()
cursor.execute('SHOW VARIABLES')

for r in cursor.fetchall():
    self.response.write('{}\n'.format(r))

【问题讨论】:

  • 您只能写入响应一次,除其他任何内容外。
  • 以某种方式修复!我尝试操作 CLOUDSQL_INSTANCE 并且它起作用了。我刚刚从中删除了 project-id 部分,它可以正常工作,例如,如果项目 id 是 hello-word-123 并且实例名称(来自 SQL 属性页面)是 hello-world-123:us-central1:sqlsomething3那么你需要把它改成 us-central1:sqlsomething3

标签: python python-2.7 google-app-engine google-cloud-sql


【解决方案1】:

文档有些过时。您应该能够始终使用 SQL 属性页面中的“实例连接名称”属性来构造 unix 套接字路径;只需将该值附加在“/cloudsql/”前缀之后。

对于第二代,连接格式为项目:区域:名称。在您的示例中,它映射到“hello-world-123:us-central1:sqlsomething3”,unix 套接字路径是“/cloudsql/hello-world-123:us-central1:sqlsomething3”。

【讨论】:

  • 为了补充 Tony 的回答,有关 App Engine 连接性的相关文档的标题为 Connecting from Google App Engine
  • 很好的答案。不得不搜索这个。
猜你喜欢
  • 2014-09-28
  • 2012-06-25
  • 1970-01-01
  • 2014-01-15
  • 2011-10-09
  • 2011-11-23
  • 2015-06-29
  • 2011-07-07
  • 2011-08-10
相关资源
最近更新 更多