簡體   English   中英

將MySQL數據庫遷移到Django時出錯。 意外的主機名

[英]Error migrating MySQL database into Django. Unexpected host name

我正在嘗試將MySQL數據庫遷移到Django。 當我按照教程學習時,我創建了一個簡單的數據庫,但是現在我試圖將實際的數據庫遷移到項目中。 我修改了settings.py使其看起來像這樣(更改了數據庫部分):

"""
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作為數據庫呢?

我的遷移命令:

python manage.py makemigrations
python manage.py migrate

檢查該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 *

無論好壞,這種模式或其變化都是司空見慣的。 您可能在文件后面的條件下進行導入。

我也遇到了同樣的問題。

如果yyyy-backend.cluster-cwtxulgbsb88.us-east-1.rds.amazonaws.com是正確的?
名稱可以解析嗎?

您可能想使用其IP地址進行連接。

因為通常在主機上無法解析名稱的情況。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM