无法对google cloud sql运行manage.py

时间:2021-03-22 00:59:24

I'm trying to run django commands

我正在尝试运行django命令

manage.py makemigrations
manage.py migrate

to setup MySQL databases on goodle cloud SQL but it only runs against my local development server.

在goodle云SQL上设置MySQL数据库,但它只针对我的本地开发服务器运行。

I've followed the code in the linked StackExchange thread.

我已经跟踪了链接的StackExchange线程中的代码。

Since this isn't working I've forced the database to cloud SQL by only defining that DATABASE within settings.py and I get the following:

由于这不起作用,我通过仅在settings.py中定义DATABASE来强制数据库云SQL,我得到以下内容:

Traceback (most recent call last):
  File "C:\FCA\manage.py", line 24, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Python27\lib\site-packages\django\core\management\base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Python27\lib\site-packages\django\core\management\base.py", line 398, in execute
    self.check()
  File "C:\Python27\lib\site-packages\django\core\management\base.py", line 426, in check
    include_deployment_checks=include_deployment_checks,
  File "C:\Python27\lib\site-packages\django\core\checks\registry.py", line 75, in run_checks
    new_errors = check(app_configs=app_configs)
  File "C:\Python27\lib\site-packages\django\core\checks\model_checks.py", line 28, in check_all_models
    errors.extend(model.check(**kwargs))
  File "C:\Python27\lib\site-packages\django\db\models\base.py", line 1170, in check
    errors.extend(cls._check_fields(**kwargs))
  File "C:\Python27\lib\site-packages\django\db\models\base.py", line 1247, in _check_fields
    errors.extend(field.check(**kwargs))
  File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py", line 925, in check
    errors = super(AutoField, self).check(**kwargs)
  File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py", line 208, in check
    errors.extend(self._check_backend_specific_checks(**kwargs))
  File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py", line 317, in _check_backend_specific_checks
    return connections[db].validation.check_field(self, **kwargs)
  File "C:\Python27\lib\site-packages\django\db\backends\mysql\validation.py", line 18, in check_field
    field_type = field.db_type(connection)
  File "C:\Python27\lib\site-packages\django\db\models\fields\__init__.py", line 625, in db_type
    return connection.data_types[self.get_internal_type()] % data
  File "C:\Python27\lib\site-packages\django\db\__init__.py", line 36, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "C:\Python27\lib\site-packages\django\utils\functional.py", line 33, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Python27\lib\site-packages\django\db\backends\mysql\base.py", line 184, in data_types
    if self.features.supports_microsecond_precision:
  File "C:\Python27\lib\site-packages\django\utils\functional.py", line 33, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Python27\lib\site-packages\django\db\backends\mysql\features.py", line 53, in supports_microsecond_precision
    return self.connection.mysql_version >= (5, 6, 4) and Database.version_info >= (1, 2, 5)
  File "C:\Python27\lib\site-packages\django\utils\functional.py", line 33, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Python27\lib\site-packages\django\db\backends\mysql\base.py", line 359, in mysql_version
    with self.temporary_connection():
  File "C:\Python27\lib\contextlib.py", line 17, in __enter__
    return self.gen.next()
  File "C:\Python27\lib\site-packages\django\db\backends\base\base.py", line 564, in temporary_connection
    cursor = self.cursor()
  File "C:\Python27\lib\site-packages\django\db\backends\base\base.py", line 231, in cursor
    cursor = self.make_debug_cursor(self._cursor())
  File "C:\Python27\lib\site-packages\django\db\backends\base\base.py", line 204, in _cursor
    self.ensure_connection()
  File "C:\Python27\lib\site-packages\django\db\backends\base\base.py", line 199, in ensure_connection
    self.connect()
  File "C:\Python27\lib\site-packages\django\db\utils.py", line 95, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "C:\Python27\lib\site-packages\django\db\backends\base\base.py", line 199, in ensure_connection
    self.connect()
  File "C:\Python27\lib\site-packages\django\db\backends\base\base.py", line 171, in connect
    self.connection = self.get_new_connection(conn_params)
  File "C:\Python27\lib\site-packages\django\db\backends\mysql\base.py", line 264, in get_new_connection
    conn = Database.connect(**conn_params)
  File "build\bdist.win-amd64\egg\MySQLdb\__init__.py", line 81, in Connect
  File "build\bdist.win-amd64\egg\MySQLdb\connections.py", line 193, in __init__
django.db.utils.OperationalError: (1045, "Access denied for user 'thwaites'@'localhost' (using password: YES)")

I have obtained an IPv4 address from Google and the user/password can access when I connect with MySQL Workbench

我从Google获得了一个IPv4地址,当我连接MySQL Workbench时,用户/密码可以访问

EDIT - below I attached settings.py

编辑 - 下面我附上了settings.py

# Copyright 2015 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Django settings for mysite project.

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

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

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

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os

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.8/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '-c&qt=71oi^e5s8(ene*$b89^#%*0xeve$x_trs91veok9#0h0'

# 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',
    'coursework',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
)

ROOT_URLCONF = 'mysite.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 = 'mysite.wsgi.application'


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

# [START db_setup]

# set this if you want to run django commands from development against CloudSQL eg to set up database etc
#make_cloudsql = 'prod'

import os
"""
if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine'):
    # Running on production App Engine, so use a Google Cloud SQL database.
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'HOST': '/cloudsql/quizalitious:myfcacoursedb',
            'NAME': 'mydb',
            'USER': 'root',
        }
    }
elif os.getenv('SETTINGS_MODE') == 'prod':
"""    # Running in development, but want to access the Google Cloud SQL instance in production.
DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'INSTANCE': '173.194.228.69',
            'NAME': 'mydb',
            'USER': 'thwaites',
            'PASSWORD' : 'secret',
        }
    }
"""else:
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': 'mydb',
            'USER': 'root',
            'PASSWORD': 'secret',
            'HOST': 'localhost',
            'PORT': '3306',
            'OPTIONS': {
                'autocommit': True,
                }
        }
    }
"""
# [END db_setup]

# Internationalization
# https://docs.djangoproject.com/en/1.8/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.8/howto/static-files/

STATIC_ROOT='static'
STATIC_URL = '/static/'

MEDIA_ROOT='media'
MEDIA_URL = '/media/'

1 个解决方案

#1


0  

I finally sorted this on a temporary basis by exporting my database structure from the development server and importing it into CloudSQL which is fine while the site isn't live but will become a problem if I want to update a live site and keep my data!

我最终通过从开发服务器导出我的数据库结构并将其导入CloudSQL来临时对其进行排序,这在网站不活动时很好但如果我想更新实时网站并保留我的数据将成为问题!

#1


0  

I finally sorted this on a temporary basis by exporting my database structure from the development server and importing it into CloudSQL which is fine while the site isn't live but will become a problem if I want to update a live site and keep my data!

我最终通过从开发服务器导出我的数据库结构并将其导入CloudSQL来临时对其进行排序,这在网站不活动时很好但如果我想更新实时网站并保留我的数据将成为问题!