19

I'm attempting to use the cloud sql proxy to connect to 2 different cloud sql instances...

In the docs I found a line about Use -instances parameter. For multiple instances, use a comma-separated list. but not sure how to make that look. https://cloud.google.com/sql/docs/sql-proxy. I'm using Google Container engine, and with a single CloudSQL instance it works great:

- name: cloudsql-proxy
 image: b.gcr.io/cloudsql-docker/gce-proxy:1.05
 command: ["/cloud_sql_proxy", "--dir=/cloudsql",
 "-instances=starchup-147119:us-central1:first-db=tcp:3306",
 "-credential_file=/secrets/cloudsql/credentials.json"]
 volumeMounts:
 - name: cloudsql-oauth-credentials
 mountPath: /secrets/cloudsql
 readOnly: true
 - name: ssl-certs
 mountPath: /etc/ssl/certs

But for multiple I've tried the -instances section as such:

-instances=starchup-147119:us-central1:first-db,starchup-147119:us-central1:second-db=tcp:3306 
and 
-instances=starchup-147119:us-central1:first-db=tcp:3306,starchup-147119:us-central1:second-db=tcp:3306

but they all give various errors; ECONNREFUSED 127.0.0.1:3306, ER_DBACCESS_DENIED_ERROR, and ER_ACCESS_DENIED_ERROR

Any help is much appreciated!

asked Nov 24, 2016 at 19:12

2 Answers 2

35

You cannot have two databases hosted on the same TCP port. Instead, specify ports for each database in the comma-separated list:

-instances=project:region:db=tcp:3306,project:region:db-2=tcp:3307

I used 3306 and 3307 here, but you can use any ports you want! Make sure that the rest of your Container Engine config allows for communication between nodes on these ports (maybe that's true by default, I don't use GKE).

Most mysql drivers connect to port 3306 by default but have a way to specify another port. You'll have to arrange for your code to connect to the different port you choose for the second database.

answered Nov 25, 2016 at 17:01
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, I'm using this to connect a pod to 1) the master database for read/write traffic 2) the read-only replica for read-only traffic. Surprisingly this answer is the only place I could find for that information.
I'm getting "access denied" for making two connections to different databases on the same instance. Do you think using a different port will help? Not quite understanding the socket path concept yet.
better to use maxscale :)
6

You can create two instances on the same port but you have to define a different IP like this:

-instances=project:region:db=tcp:127.0.0.1:3306,project:region:db-2=tcp:127.0.0.2:3306

You can see an example on Github cloudsql-proxy repository.

answered May 27, 2021 at 14:43

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.