How to connect from a pool of Google Compute Engin

2019-05-31 19:48发布

问题:

If I have a pool of Compute Engine instances (behind a load-balancer with a static IP), and I want to connect from Java apps on these instances to a Cloud Sql database in the same project, what's the way to about it?

1) These instances in the load balancer don't have static external IPs, so I can't authorize access to Cloud SQL by IP addresses.

2) "com.mysql.jdbc.GoogleDriver" is not available on Compute Engine, as it is on App Engine.

3) If I try to use the stock MySql JDBC driver, it doesn't understand a connection string like:

"jdbc:google:mysql://__instance_name__/__db_name__>?user=__user__&password=__password__";

and fails saying "No suitable driver found for jdbc:google:mysql://__instance_name__/__db_name__>?user=__user__&password=__password__"

I can't believe I can't easily connect from Compute Engine instances to a Cloud SQL database of the same Google Cloud project.

Is my only option to use "Connecting using the Cloud SQL Proxy" approach as described on https://cloud.google.com/sql/docs/compute-engine-access? That seems significant amount of work to be done automatically on instances that will come to the pool and go away at will!

Could someone please throw some light on it?

Thanks.

回答1:

Finally able to connect from dynamic-IP GCE instances to Google Cloud SQL database through the Google Cloud Sql proxy. Since there were variants (same-project-app-to-db, different-project-app-to-db) and I also wanted to keep some App-engine-to-Cloud-SQL connection information nearby for anyone who may need it, made a little write-up about it and put it at https://roshandawrani.wordpress.com/2016/09/22/connecting-from-gce-gae-java-to-google-cloud-sql/. Hope it's ok to leave the link here to the detailed information instead of reproducing everything here again. Cheers.