【问题标题】:Unable to run Django with a MySQL backend无法使用 MySQL 后端运行 Django
【发布时间】:2013-04-04 01:41:01
【问题描述】:

我正在尝试使用 MySQL 后端设置 Django,以前我只使用过 sqlite,但我遇到了一个相当奇怪的错误。 Error was: No module named mysql.base

我正在运行带有 Nginx 和 uwsgi 的 Ubuntu,并且我安装了 python-mysqldb。在此之后,我开始收到 502,所以我希望这是相关的,因为我没有更改任何配置文件。

这是manage.py inspectdb之后的堆栈跟踪;

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 453, in execute_from_command_line
utility.execute()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 272, in fetch_command
klass = load_command_class(app_name, subcommand)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 77, in load_command_class
module = import_module('%s.management.commands.%s' % (app_name, name))
  File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/inspectdb.py", line 8, in <module>
from django.db import connections, DEFAULT_DB_ALIAS
  File "/usr/local/lib/python2.7/dist-packages/django/db/__init__.py", line 40, in <module>
backend = load_backend(connection.settings_dict['ENGINE'])
  File "/usr/local/lib/python2.7/dist-packages/django/db/__init__.py", line 34, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "/usr/local/lib/python2.7/dist-packages/django/db/utils.py", line 93, in __getitem__
backend = load_backend(db['ENGINE'])
  File "/usr/local/lib/python2.7/dist-packages/django/db/utils.py", line 45, in load_backend
raise ImproperlyConfigured(error_msg)
django.core.exceptions.ImproperlyConfigured: 'django.db.backends.mysql' isn't an available database backend.
Try using 'django.db.backends.XXX', where XXX is one of:
u'oracle', u'postgresql_psycopg2', u'sqlite3'
Error was: No module named mysql.base

数据库设置;

  DATABASES = {
      'default': {
            'ENGINE': 'django.db.backends.mysql',  # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
            'NAME': 'wdsdj',                      # Or path to database file if using sqlite3
            # The following settings are not used with sqlite3:
            'USER': 'mark',
            'PASSWORD': 'pass',
            'HOST': '',                      # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
            'PORT': '',                      # Set to empty string for default.
      }
  }

如果我改用 sqlite3,那么 dj 不会有任何问题;

# This is an auto-generated Django model module.
# You'll have to do the following manually to clean this up:
#     * Rearrange models' order
#     * Make sure each model has one field with primary_key=True
# Feel free to rename the models, but don't rename db_table values or field names.
#
# Also note: You'll have to insert the output of 'django-admin.py sqlcustom [appname]'
# into your database.
from __future__ import unicode_literals

from django.db import models

【问题讨论】:

  • 你安装了mysql-python绑定吗? sudo apt-get install python-mysqldb
  • 我们可以查看您的数据库设置文件吗?
  • 重复hereherehere
  • @danodonovan 我经历过这些,但他们没有解决这个问题。据我所知,我已经设置了我的设置。我会和他们一起更新。

标签: mysql django


【解决方案1】:

检查您的设置,确保 ENGINE 正确django.db.backends.mysql

然后确保你已经安装了 python-mysqldb...

sudo apt-get install python-mysqldb

但是,在您的错误堆栈中,它指出“isn't an available database backend”似乎您的 Django 安装可能有问题?

【讨论】:

  • 引擎是正确的(参见 OP),我已经有了 python-mysqldb。 (导入 MySQLdb 在 python 中工作)。我正在运行过去几天安装的 Django 1.5.1 final。
  • 我明白了,我只在这里看到过一次这个错误:github.com/gmcguire/django-db-pool/issues/5 必须配置错误
  • 谢谢,下班后我会去看看 :) 也许在这个早期阶段我应该尝试一下 PostgreSQL,我之前从未使用过它,所以打算使用 MySQL。
  • 我切换到 PostgreSQL,它工作得很好。比 MySQL 简单得多。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-04-09
  • 2015-03-06
  • 1970-01-01
  • 2018-02-11
  • 2020-10-26
  • 1970-01-01
  • 2010-11-04
相关资源
最近更新 更多