Score:0

GCloud SQL timed out on a Docker container

hu flag

I have a flask app in a Paperspace Deployment Docker container. It is connected to a Google Cloud SQL (MySQL).

Here are the variables in the YALM parameters:

env:
  - name: CLOUD_SQL_CONNECTION_NAME
    value: xx.xx.xx.xx
  - name: CLOUD_SQL_USERNAME
    value: my_user
  - name: CLOUD_SQL_PASSWORD
    value: xxxxx
  - name: CLOUD_SQL_DATABASE_NAME
    value: db_01

And the related code:

HOST =os.environ.get('CLOUD_SQL_CONNECTION_NAME') 
USER = os.environ.get('CLOUD_SQL_USERNAME')
PWD = os.environ.get('CLOUD_SQL_PASSWORD')
SQLDB = os.environ.get('CLOUD_SQL_DATABASE_NAME')
CHARSET = os.environ.get('CLOUD_SQL_CHARSET')

SQLALCHEMY_DATABASE_URI = ('mysql+pymysql://{user}:{password}@{connection_name}/{database}').format(user=USER, password=PWD,database=SQLDB, connection_name=HOST)

app.config['SQLALCHEMY_DATABASE_URI'] =  SQLALCHEMY_DATABASE_URI

The container is working well if I run it on the local PC, but not in a Paperspace cloud.

The GCloud SQL server never answers, even if I have the Paperspace service's IP address in the authorized IPs from GCould SQL.

The IP has been obtained thanks to a ping to the service endpoint URL.

Here is the error I get from the Paperspace Deployment server.

sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2003, "Can't connect to MySQL server on '[CLOUD_SQL_CONNECTION_NAME(IP)]' (timed out)")
(Background on this error at: https://sqlalche.me/e/14/e3q8)

Why the GCloud SQL server never answers? Is there any port or authorization that needs to be activated?

John Hanley avatar
cn flag
The error **(timed out)** often means a firewall issue. Using a ping to determine a services address will often result in the wrong address. I do not know your service (Paperspace) but you will need to either lookup the correct egress IP address or use the Cloud SQL Auth Proxy (program or Python library).
NicolasSens avatar
hu flag
Thanks a lot, I'll check this.
NicolasSens avatar
hu flag
@JohnHanley After further investigation, I've found out that Paperspace has the port 3306 closed, which block any request to the MySQL Database on GCloud. I've asked them to unlock port 3306. Not sure yet if they can. Do you know any trick to avoid this issue?
John Hanley avatar
cn flag
I have not used their service. However, I cannot image the reason why they would block you from connecting to a MySQL server. Which ports do they allow?
NicolasSens avatar
hu flag
I've tested several ports (5000, 8000, 3306, ...), and only the port 80 is working.
John Hanley avatar
cn flag
This does not make any sense. Get an official answer from Paperspace and post that in your question.
NicolasSens avatar
hu flag
Actually, I was authorizing a wrong IP address in GCloud. The command "curl api.ipify.org" gives the correct one and I was able to connect to the database.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.