从 Python 查询云 SQL 数据库

Query Cloud SQL Database from Python

我想从本地 Python 应用程序将数据插入 CloudSQL MySQL 数据库,这可能吗?如果可以,怎么做?

我试过 运行 https://cloud.google.com/appengine/docs/python/cloud-sql/#Python_complete_python_example

底部的示例
db = MySQLdb.connect(unix_socket='/cloudsql/PROJECT-ID:INSTANCE-NAME', user='phil')

cursor = db.cursor()
cursor.execute('SHOW VARIABLES')
for r in cursor.fetchall():
  webapp2.RequestHandler.response.write('%s\n' % str(r))

db.close()

但是我收到错误:

`Can't connect to local MySQL server through socket`

当然可以,但这是专门从 App Engine 使用它的文档。相反,您应该使用 connecting from an external application 的文档 - 您需要配置访问权限,然后将 mysqldb 设置为通过 IP 而不是本地套接字进行连接。