【问题标题】:Can't get MySQL/MariaDB to work with Django 1.10.1 and Python 3.5 on Fedora 24?无法让 MySQL/MariaDB 在 Fedora 24 上与 Django 1.10.1 和 Python 3.5 一起使用?
【发布时间】:2016-09-16 13:51:54
【问题描述】:

除了上述之外,我还在使用 virtualenv 和 virtualenvwrapper。 MariaDB server 10.1.17 是通过dnf install安装的

我正在完成第一个 Django 教程。我的运行服务器命令:

(djTut3)$ python manage.py runserver

运行正常,但有关于迁移的警告。

在第 2 部分,这里:https://docs.djangoproject.com/en/1.10/intro/tutorial02/

我跑了:

(djTut3)$ python manage.py migrate

按照指示,出现以下错误:

ImportError: No module named 'MySQLdb'
During handling of the above exception, another exception occurred:
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named 'MySQLdb'

我做了很多搜索,并尝试了以下(除其他外),主要来自 SO:

===

$ sudo dnf install MySQL-python
<No error>

$ python3 manage.py migrate
<Same errors as before>

$ sudo dnf install mariadb-devel
Skipping packages with conflicts:
(add '--best --allowerasing' to command line to force their upgrade)

$ sudo dnf install mariadb-devel --best --allowerasing
<No error>

$ python3 manage.py migrate
<Same errors as before>

$ pip3 install mysql-python
ImportError: No module named 'ConfigParser'

$ pip3 install mysql-connector
<No error>

$ pip3 install mysql-client
_mysql.c:40:20: fatal error: Python.h: No such file or directory
#include "Python.h"
compilation terminated.
error: command 'gcc' failed with exit status 1

$sudo dnf install python-devel mysql-devel
<already installed>

===

现在还是打不开

(djTut3)$ python manage.py migrate

工作,并且

(djTut3)$ python manage.py runserver

给出与迁移命令相同的错误。

我的 settings.py 包括:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'djTut3',
        'USER': os.environ['DB_USERNAME'],
        'PASSWORD': os.environ['DB_PASSWORD'],
        'HOST': '127.0.0.1',
        'PORT': '',
    }
}

任何人都可以提供一些帮助吗?

非常感谢您的任何回复。

【问题讨论】:

    标签: mysql django python-3.x mariadb fedora


    【解决方案1】:

    这就是为什么你应该通过pip而不是你的操作系统包管理器来安装 Python 包的原因。 MySQLdb 不适用于 Python 3,正如您通过 pip3 尝试时的错误所见。

    您在安装 mysql-client 时遇到问题的原因可能是因为您需要 python-devel 包,或者任何 Fedora 等价包;那将是一个 dnf 安装。

    但是,既然您确实使用 mysql-connector 成功了,那么您可以使用它;您需要将 Django 配置为使用它,如 that library's documentation 所示,将 'django.db.backends.mysql' 替换为 'mysql.connector.django'

    【讨论】:

    • 我在设置文件中将 'django.db.backends.mysql' 替换为 'mysql.connector.django';现在python manage.py runserver 工作。但是尽管python manage.py migrate 运行,它会产生错误:`mysql.connector.errors.DatabaseError: 1265: Data truncated for column 'applied' at row 1
    • 在处理上述异常期间,发生了另一个异常:django.db.utils.DatabaseError: Data truncated for column 'applied' at row 1`设置更改,这没有帮助。你有什么进一步的想法吗?
    • 在重新运行 python manage.py migrate 我现在得到错误:mysql.connector.errors.ProgrammingError: 1050 (42S01): Table 'django_content_type' already exists During handling of the above exception, another exception occurred: django.db.utils.ProgrammingError: Table 'django_content_type' already exists
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-15
    • 1970-01-01
    • 2017-02-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-23
    相关资源
    最近更新 更多