getting-started-python icon indicating copy to clipboard operation
getting-started-python copied to clipboard

Can't connect to MySQL server on 'localhost'

Open ajpharrington opened this issue 9 years ago • 9 comments

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"

ajpharrington avatar Apr 20 '17 14:04 ajpharrington

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

ajpharrington avatar Apr 20 '17 15:04 ajpharrington

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

theacodes avatar Apr 20 '17 16:04 theacodes

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 avatar Apr 21 '17 07:04 ryanmats

@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 avatar Apr 21 '17 15:04 ajpharrington

@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!

ryanmats avatar Apr 21 '17 16:04 ryanmats

@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 avatar Apr 24 '17 21:04 ryanmats

@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?

mikeklinker129 avatar Jun 28 '17 02:06 mikeklinker129

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

mikeklinker129 avatar Jun 28 '17 14:06 mikeklinker129

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

larsblumberg avatar Sep 28 '17 11:09 larsblumberg