2017-09-25 52 views
0

我正在尝试将MySQL数据库迁移到Django中。我按照教程创建了一个简单的数据库,但现在我试图将我的实际数据库迁移到项目中。我修改settings.py看起来像这样(更改数据库部分):将MySQL数据库迁移到Django时出错。意外的主机名

""" 
Django settings for tutorial project. 

Generated by 'django-admin startproject' using Django 1.11.5. 

For more information on this file, see 
https://docs.djangoproject.com/en/1.11/topics/settings/ 

For the full list of settings and their values, see 
https://docs.djangoproject.com/en/1.11/ref/settings/ 
""" 

import os 

# Build paths inside the project like this: os.path.join(BASE_DIR, ...) 
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 


# Quick-start development settings - unsuitable for production 
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/ 

# SECURITY WARNING: keep the secret key used in production secret! 
SECRET_KEY = '********' 

# SECURITY WARNING: don't run with debug turned on in production! 
DEBUG = True 

ALLOWED_HOSTS = [] 


# Application definition 

INSTALLED_APPS = [ 
    'django.contrib.admin', 
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
    'rest_framework', 
    'helloservice.apps.HelloServiceConfig', 
] 

REST_FRAMEWORK = { 
    'PAGE_SIZE': 10 
} 

MIDDLEWARE = [ 
    'django.middleware.security.SecurityMiddleware', 
    'django.contrib.sessions.middleware.SessionMiddleware', 
    'django.middleware.common.CommonMiddleware', 
    'django.middleware.csrf.CsrfViewMiddleware', 
    'django.contrib.auth.middleware.AuthenticationMiddleware', 
    'django.contrib.messages.middleware.MessageMiddleware', 
    'django.middleware.clickjacking.XFrameOptionsMiddleware', 
] 

ROOT_URLCONF = 'app.urls' 

TEMPLATES = [ 
    { 
     'BACKEND': 'django.template.backends.django.DjangoTemplates', 
     'DIRS': [], 
     'APP_DIRS': True, 
     'OPTIONS': { 
      'context_processors': [ 
       'django.template.context_processors.debug', 
       'django.template.context_processors.request', 
       'django.contrib.auth.context_processors.auth', 
       'django.contrib.messages.context_processors.messages', 
      ], 
     }, 
    }, 
] 

WSGI_APPLICATION = 'app.wsgi.application' 


# Database 
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases 

DATABASES = { 
    'default': { 
     'ENGINE': 'django.db.backends.mysql', 
     'NAME': 'pulse', 
     'USER':'root', 
     'PASSWORD':'zzzz', 
     'HOST':'yyyy-backend.cluster-cwtxulgbsb88.us-east-1.rds.amazonaws.com', 
     'PORT':'3306', 

    } 
} 


# Password validation 
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators 

AUTH_PASSWORD_VALIDATORS = [ 
    { 
     'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 
    }, 
    { 
     'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 
    }, 
    { 
     'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 
    }, 
    { 
     'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 
    }, 
] 


# Internationalization 
# https://docs.djangoproject.com/en/1.11/topics/i18n/ 

LANGUAGE_CODE = 'en-us' 

TIME_ZONE = 'UTC' 

USE_I18N = True 

USE_L10N = True 

USE_TZ = True 


# Static files (CSS, JavaScript, Images) 
# https://docs.djangoproject.com/en/1.11/howto/static-files/ 

STATIC_URL = '/static/' 

我的错误:

mysql_exceptions.OperationalError: (2005, "Unknown MySQL server host 'mysql://dev-backend.cluster-xxx.us-east-1.rds.amazonaws.com:3306' (74)") 

当它到底在越来越DEV-backend.cluster从数据库?

我migrate命令:

python manage.py makemigrations 
python manage.py migrate 
+0

现在您需要更改'SECRET_KEY'。 –

+0

哈哈哎呀....谢谢! – Rilcon42

回答

0

检查为DATABASES分配的下游设置。在Django项目的常见模式是这样的布局:

# In settings.py, declare all the base values 
DATABASES = { ... } 
INSTALLED_APPS = [ ... ] 

# Import other settings based on environment 
if os.environ.get("environ") != "production": 
    from .devel import * 

是好还是坏此图案或其变型是常见的。您可能在文件的稍后条件下有一个导入。

+0

我更新了我的代码以包含我的整个settings.py。还有其他文件我应该检查吗?我没有在settings.py中看到任何奇怪的东西,只要其他文件的引用是 – Rilcon42

+0

@ Rilcon42你可以从你运行Django的地方ping数据库吗?有没有“CNAME”或重定向设置? –

0

我也遇到了同样的问题。

if yyyy-backend.cluster-cwtxulgbsb88.us-east-1.rds.amazonaws.com is correct??
can name resolve?

你可能想使用它的 IP地址连接代替。

Because Usually the case when name resolving doesn't work on the host.