How to connect from a pool of Google Compute Engine instances to Cloud SQL DB in the same project?
Asked Answered
G

1

1

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.

Gardant answered 21/9, 2016 at 18:48 Comment(2)
I think you may need to go with the Cloud SQL Proxy option, and use startup script to do the installation. cloud.google.com/compute/docs/startupscriptSlumberous
Yes, in-fact, just 2 minutes back, I successfully started connecting via the Cloud SQL Proxy option. Had to sleep in between, when it became clear last night that "cloud sql proxy" was the only option left.Gardant
G
3

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.

Gardant answered 22/9, 2016 at 6:20 Comment(1)
Updated the blogpost to include an alternative to using Google Cloud Sql Proxy for Java apps that have to connect from GCE to Cloud SQL databases: the use of "cloud-sql-mysql-socket-factory" library.Gardant

© 2022 - 2024 — McMap. All rights reserved.