【问题标题】:MySQL (MariaDB) backend for Django under Python3 on DebianDebian 上 Python3 下 Django 的 MySQL (MariaDB) 后端
【发布时间】:2017-10-28 10:11:50
【问题描述】:

我正在尝试在 Debian 机器上配置在 Python3 下运行的 Django,以使用 MariaDB 作为其后端。如果我按照教程更改我的mysite/settpings.py,即

DATABASES = {
    'default': {
        'ENGINE':   'django.db.backends.mysql',
        'NAME':     'foo',
        'USER':     'tim',
        'PASSWORD': 'swordfish',
        'HOST':     'localhost'
    }
}

我有很多悲伤,最终导致

  File "/usr/local/lib/python3.4/dist-packages/django/db/backends/mysql/base.py", line 30, in <module>
    'Did you install mysqlclient or MySQL-python?' % e
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named 'MySQLdb'.
Did you install mysqlclient or MySQL-python?

现在,很明显,MySQLdb 在 Python3 上不起作用,虽然我已经安装了 mysql 连接器包,但似乎不起作用。

如果我尝试使用 MySQL 的连接器 recommended

DATABASES = {
    'default': {
        'ENGINE':   'mysql.connector.django',
        'NAME':     'foo',
        'USER':     'tim',
        'PASSWORD': 'swordfish',
        'HOST':     'localhost'

然后我感到非常悲伤

django.core.exceptions.ImproperlyConfigured: 'mysql.connector.django' isn't an available database backend.
Try using 'django.db.backends.XXX', where XXX is one of:
    'mysql', 'oracle', 'postgresql', 'sqlite3'
Error was: cannot import name 'BaseDatabaseFeatures'

有什么建议吗?

更新

根据 Matt Seymour 的建议,如果我尝试安装 mysqlclient 的 pip(实际上是 pip3,因为我的系统 Python 仍然是 2.x),我会得到以下信息...

tim@merlin:~/mysite$ sudo pip3 install mysqlclient
Collecting mysqlclient
  Using cached mysqlclient-1.3.10.tar.gz
    Complete output from command python setup.py egg_info:
    /bin/sh: 1: mysql_config: not found
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-build-9uub69zo/mysqlclient/setup.py", line 17, in <module>
        metadata, options = get_config()
      File "/tmp/pip-build-9uub69zo/mysqlclient/setup_posix.py", line 44, in get_config
        libs = mysql_config("libs_r")
      File "/tmp/pip-build-9uub69zo/mysqlclient/setup_posix.py", line 26, in mysql_config
        raise EnvironmentError("%s not found" % (mysql_config.path,))
    OSError: mysql_config not found

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-9uub69zo/mysqlclient

我使用 MariaDB 而不是 MySQL 的事实在这里是否很重要?

【问题讨论】:

  • 你是如何安装 python-mysql 包的?我强烈建议在任何系统包上使用 virtualenv。激活你的环境然后pip install mysqlclient

标签: mysql django python-3.x


【解决方案1】:

好的,经过一番折腾,我得到了某种答案,尽管没有我想要的那么令人满意。

MySQLdb 库在 Python 3.x 下不受支持,因此您应该改用它的 mysqlclient 分支。但是,因为我使用的是 MySQL 的 MariaDB 分支,所以安装 mysqlclient 所需的一些 Debian 依赖项未得到满足。我不想恢复到 MySQL,因为我将 MariaDB 用于其他目的。

因此,由于我只需要一个相当健壮的多用户后端 RDBMS,我已经安装了 PostgreSQL 以与 Django 一起使用,它似乎工作得很好。

【讨论】:

    猜你喜欢
    • 2016-04-18
    • 2015-10-11
    • 2021-05-03
    • 1970-01-01
    • 1970-01-01
    • 2017-12-03
    • 2018-03-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多