Access Google Cloud SQL from python in local machine?

Question:

Update 2022

There is cloud sql proxy now:

https://cloud.google.com/sql/docs/mysql/sql-proxy#how-works

Old Question

I am currently using my local machine to manipulate data, google app engine to host my web service and google cloud sql to handle the database.
However, I need to upload my data to gae first, then run a backend job on gae to uploading all data to google cloud sql later. It is time wasting to upload data twice.
I would like to import/update some date from my local machine to google cloud sql directly.

Right now, google cloud sql only fully supported google app engine as its client.

Google Cloud SQL is currently only available to Google App Engine
applications. To develop Google App Engine applications, you need to
install the App Engine SDK, available in either Python or Java:

However, since google cloud sql provides a jar db driver to allow command line client / SQuirrel SQL Client access from local machine. It looks like I can write some java code to interact with the cloud sql via this jar in local machine. Or I could use tools like jpype to access google cloud sql via python.

Is that possible? Where should I start?
Or is there any alternative way that I can use python on local machine to access google cloud sql?

https://developers.google.com/cloud-sql/docs/commandline

http://jpype.sourceforge.net/

Asked By: lucemia

||

Answers:

This tool is new to me!

If this tool exists, why don’t you use it to upload your data to Cloud SQL?

At the time of this writting, there’s no official support to connect to Google Cloud SQL from the outside world. Google said is planning to allow access from anywhere in the near future.

In conclusion, try using this tool to upload your data. Using appengine as a middleware could be costly depending on the size of your data.

I found a way to connect google cloud sql from python via SDK

def connect_sql(instance, database = None):
    from google.storage.speckle.python.tool import google_sql    
    database = google_sql.DatabaseConfig(instance, database)

    sql_cmd_config = google_sql.config.SQLCmdConfig(None)
    sql_cmd_config.add('__googlesql__', instance, None, None, database,
                    google_sql.GoogleSqlDriver.NAME, None, None)
    sql_cmd = google_sql.GoogleSqlCmd(sql_cmd_config)
    sql_cmd.set_database(instance)

    sql_cmd.preloop()
    return sql_cmd._SQLCmd__db
Answered By: lucemia

Cloud SQL now supports the native MySQL wire protocol [1], so you can use standard connectors such as Connector/Python [2]

  1. https://developers.google.com/cloud-sql/docs/external#wireprotocol
  2. http://dev.mysql.com/downloads/connector/python/
Answered By: Joe Faith