Вопрос

I'm trying to set up a django project on Google app engine but i can't seem to connect from my computer to the database to do things like syncdb and so on.

this is my configuration (Altered)

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'HOST': '/cloudsql/something:else',
        'NAME': 'db_name',
        'USER': 'db_user',
    }
}

now the connection to the database works on the deployed app, but not when I'm running it from my end, the error I get is

Can't connect to local MySQL server through socket '/cloudsql/something:else'

I'm running osx, I installed google cloud sdk as well app engine SDK obviously, I'm not sure what I'm missing.

Thanks

Это было полезно?

Решение

It can't connect to it because your local environment won't recognize host /cloudsql/something:else, this is only be recognized on production environment.

You can follow these approach to make it working in dev and production environment.

Using the same production Cloud SQL instance in both production and local

First, you have to go on the Google Cloud Console to give access to your current ip.

Give access to your current ip

  1. Go to Cloud SQL console, click on your instance name.
  2. Click "edit" button
  3. in Assign IP Address, select "Assign IP Address"
  4. in below "Authorized IP Address", add an IP address on your development computer.

Database setting

And then use the setting as following:

#!/usr/bin/env python
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'USER': 'root',
        'PASSWORD': '<your-database-password>',
        'HOST': '<your-database-ip>',
        'NAME': '<your-database-name>',
    }
}

Use separate different databases for production and dev server

You can separate settings for production and local development server:

if os.getenv("SERVER_SOFTWARE", "").startswith("Google App Engine"):
    # production
    DATABASES = {
        # production database settings
    }
else:
    # local dev server
    DATABASES = {
        # local database settings
    }
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top