简体   繁体   中英

Query Cloud SQL Database from Python

I want to insert data into a CloudSQL MySQL database from a local Python application, is this possible, if so how?

I have tried running the examples at the bottom of 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()

However I get the error:

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

Of course it's possible, but that's the documentation for using it specifically from App Engine. Rather, you should use the docs for connecting from an external application - you'll need to configure access, then set mysqldb to connect via IP rather than a local socket.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM