1

I'm trying to connect to Google Cloud SQL for MySql through Google Cloud Run. I've been through many posts at SO and also the Google documentation. But doesn't seem to get it working for myself. I admit that I have never containerized any application and I am predominantly a data person.

Note: I already have Cloud SQL instance created which I can connect through Cloud shell.

1) My code is based on Google documentation at

https://cloud.google.com/sql/docs/mysql/connect-run

and the github code at

https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/cloud-sql/mysql/sqlalchemy/main.py

2) Build the image

gcloud builds submit --tag gcr.io/my-project/my-test-image

3) Deploy the image

gcloud run deploy --image gcr.io/my-project/my-test-image

4) When I try to invoke the service in Cloud Shell using the following, I get output as "Service Unavailable"

curl -H "Authorization: Bearer $(gcloud auth print-identity-token)" https://my-service.run.app

Can anyone please help me to resolve this?

Changes - I had not provided the mysql instance name. I corrected it and now I'm getting error as below -

[INFO] Listening at: http://0.0.0.0:8080 (1)"

"Traceback (most recent call last): File "/usr/local/lib/python3.8/site-packages/pymysql/connections.py", line 571, in connect sock.connect(self.unix_socket) FileNotFoundError: [Errno 2] No such file or directory"

I don't understand why it's listening at 0.0.0.0:8080. I removed the host and port from my code and I still get this "info" and the error I get is that "FileNotFoundError". I'm not sure which file it needs. Does it need some kind of connections file? If yes what's the format of the file?

sopana
  • 365
  • 1
  • 5
  • 15
  • I don't find the host and port in your code. And what is your problem? The listening on 0.0.0.0? – guillaume blaquiere Mar 03 '20 at 10:32
  • 1
    Please make sure that you are correctly providing all the required parameters mentioned in the sample code. My guess is that maybe you're not correctly providing the CLOUD_SQL_CONNECTION_NAME at the last part of the sqlalchemy.create_engine. – Waelmas Mar 03 '20 at 11:06
  • Hi Waelmas, Yeah I corrected that - cloud_sql_connection_name = os.environ.get("CLOUD_SQL_CONNECTION_NAME", "mysql-instance-name") but I'm still getting error as - "Traceback (most recent call last): File "/usr/local/lib/python3.8/site-packages/pymysql/connections.py", line 571, in connect sock.connect(self.unix_socket) FileNotFoundError: [Errno 2] No such file or directory" – sopana Mar 03 '20 at 11:37
  • Hi Guillaume, I had added the public IP of my MySql instance too in the code but that also didn't work. Even then I was getting error as - [INFO] Listening at: http://0.0.0.0:8080 (1)" . So I was not sure why it's showing 0.0.0.0 instead of the Public IP I supplied in the code. – sopana Mar 03 '20 at 11:44
  • 2
    I think, what waelmas@ tried to point is that you are assinging the instance name to the connection name i.e. if you have a instance called cloudsqlinstance in a project called mylittleproject in the zone us-central 1 yout instance connection name will be mylittleproject:us-central1:cloudsqlinstance and not just "cloudsqlinstance" (you can find the connection name in the console). Also an importan thing here is to provide cloudsql.instances.connect rights to the service account in the IAM – Chris32 Mar 03 '20 at 11:45
  • You are missing the deploy command option `--add-cloudsql-instance INSTANCE-CONNECTION-NAME`. https://cloud.google.com/sql/docs/mysql/connect-run Your container is using Unix sockets which means you need to configure the Cloud SQL Proxy sidecar otherwise configure your connection for IP addressing. – John Hanley Mar 03 '20 at 17:22
  • Thank you Chris32 & Waelmas! I corrected the cloud sql instance name and it worked! – sopana Mar 04 '20 at 10:26

1 Answers1

0

I wrote a tutorial on how to Connect Cloud SQL(MySql) from Cloud Run (Java), but the flow is the same. link

Please make sure you understand the point 13, where you have to pass the instance connection name with the flag --add-cloudsql-instances, this will start the cloud sql proxi for you.

13.Configure the service for use with Cloud Run

   gcloud run services update run-mysql --add-cloudsql-instances run-to-sql:europe-west2:database-external --set-env-vars CLOUD_SQL_CONNECTION_NAME=run-to-sql:europe-west2:database-external  DB_USER=user_name,DB_PASS=user_password,DB_NAME=user_database

SO link

marian.vladoi
  • 7,663
  • 1
  • 15
  • 29