Getting-started-python: Can't connect to MySQL server on 'localhost'

Created on 20 Apr 2017  路  9Comments  路  Source: GoogleCloudPlatform/getting-started-python

I am having an issue with my flexible python app deployment connecting to the cloud sql instance. It is the same issue as posted here: https://github.com/GoogleCloudPlatform/getting-started-python/issues/92

Here is the error I receive:
sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2003, "Can't connect to MySQL server on 'localhost' ([Errno 2] No such file or directory)")
"

  • I have setup a MySQL Second Generation instance.
  • It is working fine when I run the application locally and connect through the proxy.
  • I have the following flag set in my service.yaml:
beta_settings:
cloud_sql_instances: "project:us-central1:dbinstance"
triage me

All 9 comments

I was able to find a solution, so I will post it here for others:

I added the following to my yaml file. I think the main difference from the config, is that 'localhost' is omitted.

env_variables:
  SQLALCHEMY_DATABASE_URI: >-
    mysql+pymysql://user:password@/database?unix_socket=/cloudsql/project:us-central1:instance

Interesting. @ryanmats can you see if you can reproduce? If so, we may need to update our connection strings.

So I just tested this quite a few times and have gotten the error sometimes and a functional website other times. The error seems to be independent of whether or not I delete 'localhost' from the SQLALCHEMY_DATABASE_URI. I do know that CloudSQL has been having some flaky problems lately.

@ajpharrington Can you please do me a favor and try again both with and without localhost, back-to-back?

@ryanmats I have tried again with and without 'localhost' in the connection string, and have found it does not make a difference. I even removed the env_variables from my yaml file, to check if the config file string works and it does. It is working as expected now, and I am not able to re-create the original error.

@ajpharrington Thanks for checking. Several other developers have noticed issues like this recently, and we are looking into what could be the problem.

In the near future, please let us know if any already deployed and functional apps suddenly run into this bug. Trying to figure out if this is a CloudSQL issue or an App Engine issue. Thanks!

@ajpharrington So we've determined that this is an issue with new App Engine deployments and CloudSQL (not existing deployments that have already worked) -> we've assigned this bug to someone on the App Engine Infrastructure team to fix.

If you deploy your app and it works, it should continue to work. If one of your new deployments isn't working, you should be able to try again later and it will work.

@ryanmats I just followed the tutorial and I am consistently getting this error. I have tried making multiple new projects, fiddling with settings, however I still get the error with this codebase, as well as my actual app I am trying to deploy (that uses cloudsql).

Do you have any solutions or updates on this?

@ajpharrington I tried your environment variable changes but they did not help.

I described a potential solution (enabling Cloud SQL Admin API) here: https://github.com/GoogleCloudPlatform/getting-started-python/issues/129#issuecomment-332803546

Was this page helpful?
0 / 5 - 0 ratings