Home > Software engineering >  App engine Postgresql getting "Connection refused" error
App engine Postgresql getting "Connection refused" error

Time:10-11

I am a newbie trying to deploy a toy django app on the standard App engine and I am getting the following errors.

Running App locally

My app runs properly locally with the cloud SQL when I use 127.0.0.1 or Public IP as 'HOST' address. However, I get a this error if I use GCP connection name like this:

'HOST':  '/cloudsql/asim800:us-central1:django-mysql-1'

OperationalError: (2002, "Can't connect to local MySQL server through socket '/cloudsql/asim800:us-central1:django-mysql-1'

Running App on GAE

  1. Using HOST='/cloudsql/asim800:us-central1:django-mysql-1', I get this error 2003, "Can't connect to MySQL server on 'localhost' ([Errno 111] Connection refused)" - not sure why I am getting 'localhost' in the error but there seems to be something wrong with translating Google connection name to host and port information.

  2. Using public IP for mysql instance on the GAE results in timeout.

My settings.py looks like this

    DATABASES = {
        'default': {
                'ENGINE': 'django.db.backends.mysql',
                'HOST': '/cloudsql/asim800:us-central1:django-mysql-1', 
                'NAME': 'myproject',
                'USER': 'pblogsu',
                'PASSWORD': '****',
        }
    }

I can see them correctly in the following log:

{'database': 'myproject', 'host': '/cloudsql/asim800:us-central1:django-app1', 'password': 'pblogsu', 'user': 'hidden'}

I have enabled Google Cloud SQL API. Not sure how to debug Google connection name mapping to host/port addresses. I have seen similar problem reported like here (https://github.com/GoogleCloudPlatform/python-docs-samples/issues/870) but none of the suggestions have helped me. Any help would be greatly appreciated.

PS: I have edited my question substantially. Earlier I had used postgesql with very similar results.

CodePudding user response:

Check the parameters unix_socket_directories and port on the PostgreSQL server. For your connection attempt to work

  • the server has to run on the same machine as the client

  • cloudsql/asim800:us-central1:django-app1 has to be in unix_socket_directories

  • port has to be 5432

CodePudding user response:

I was finally able to figure out that one of the service account (associated with GAE) needed to be added in the IAM & Admin> IAM and then given Cloud sql.Client role.

It sounds like an easy fix with the hindsight but it took a lot of digging in to resolve the issue. I had suspected that there may be a permission issue but I wasn't sure which account needs to be added. I got my first clue after I saw this error in the logs:

"CloudSQL warning: your action is needed to update your application and avoid potential disruptions. Please see https://cloud.google.com/sql/docs/mysql/connect-overview for additional details: ensure that the account has access to "asim800:us-central1:django-mysql-1" (and make sure there's no typo in that name). Error during createEphemeral for asim800:us-central1:django-mysql-1: googleapi: Error 403: The client is not authorized to make this request., notAuthorized"

Thanks for your help. I'll be happy add any details if someone else run into the issue.

  • Related