我遇到了与该主题中的其他帖子类似的错误。 我尝试从第一代 SQL Server 切换到第二代 SQL Server(均在 us-central1 上),但仍然无法正常工作。
我从项目顶部的网址复制了我的 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 属性页中的“实例连接名称”属性来构造 unix 套接字路径;只需将该值附加在“/cloudsql/”前缀之后即可。
对于第二代,连接格式为项目:区域:名称。在您的示例中,它映射到“hello-world-123:us-central1:sqlsomething3”,unix套接字路径为“/cloudsql/hello-world-123:us-central1:sqlsomething3”。
关于python - Google App Engine 和 Cloud SQL : Lost connection to MySQL server at 'reading initial communication packet' SQL 2nd Gen,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37896030/