Google App Engine 和 Cloud SQL:与位于 'reading initial communication packet' SQL 第二代的 MySQL 服务器失去连接

Google App Engine and Cloud SQL: Lost connection to MySQL server at 'reading initial communication packet' SQL 2nd Gen

我收到了与该主题中其他帖子类似的错误。 我尝试从第一代切换到第二代 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))

文档有点过时了。您应该能够始终使用 SQL 属性页面中的 "Instance connection name" 属性 来构造 unix 套接字路径;只需在“/cloudsql/”前缀后附加该值即可。

二代连接格式为project:region:name。在您的示例中,它映射到 "hello-world-123:us-central1:sqlsomething3",并且 unix 套接字路径是“/cloudsql/hello-world-123:us-central1:sqlsomething3”。