【问题标题】:Setting up Django with MySQL on macOS在 macOS 上使用 MySQL 设置 Django
【发布时间】:2018-12-13 01:32:39
【问题描述】:

我是 Django 新手,正在尝试在 macOS 上设置一个连接到 MySQL 的基本 Web 应用程序。但是,我似乎无法让 mysqlclient 在 macOS 上工作。

我正在运行一个虚拟环境和以下版本:

$ brew --version
Homebrew 1.8.5
$ pip --version
pip 18.1
$ python --version
Python 3.7.1
$ mysql --version
mysql  Ver 8.0.12 for osx10.14 on x86_64 (Homebrew)

当我尝试pip install mysqlclient (https://pypi.org/project/mysqlclient/) 时,我得到了

ld: library not found for -lssl
clang: error: linker command failed with exit code 1 (use -v to see invocation)
error: command 'gcc' failed with exit status 1

根据文档“您可能需要像这样安装 Python 和 MySQL 开发头文件和库:” $ brew install mysql-connector-c 这给出了另一个错误:

Error: Cannot install mysql-connector-c because conflicting formulae are installed.
  mysql: because both install MySQL client libraries

Please `brew unlink mysql` before continuing.
$ brew unlink mysql
$ brew install mysql-connector-c 
==> Pouring mysql-connector-c-6.1.11.mojave.bottle.tar.gz
????  /usr/local/Cellar/mysql-connector-c/6.1.11: 79 files, 15.3MB
brew link mysql
Error: Could not symlink bin/my_print_defaults
Target /usr/local/bin/my_print_defaults
is a symlink belonging to mysql-connector-c
$ brew link --overwrite mysql

现在我可以从控制台启动 mysql,但是当我更新 django,mysite settings.py 以连接到 MySQL 时,我得到了

ModuleNotFoundError: No module named 'MySQLdb'

这里是相关的settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', 
        'NAME': 'django',
        'USER': 'root',
        'PASSWORD': 'XXXXXX',
        'HOST': 'localhost', 
        'PORT': '3306',
    }
}

我还根据错误报告 https://bugs.mysql.com/bug.php?id=86971 检查了 mysql_config

Usage: /usr/local/bin/mysql_config [OPTIONS]
Compiler: Clang 10.0.0.10001145
Options:
        --cflags         [-I/usr/local/Cellar/mysql/8.0.12/include/mysql ]
        --cxxflags       [-I/usr/local/Cellar/mysql/8.0.12/include/mysql ]
        --include        [-I/usr/local/Cellar/mysql/8.0.12/include/mysql]
        --libs           [-L/usr/local/Cellar/mysql/8.0.12/lib -lmysqlclient -lssl -lcrypto]
        --libs_r         [-L/usr/local/Cellar/mysql/8.0.12/lib -lmysqlclient -lssl -lcrypto]
        --plugindir      [/usr/local/Cellar/mysql/8.0.12/lib/plugin]
        --socket         [/tmp/mysql.sock]
        --port           [0]
        --version        [8.0.12]
        --variable=VAR   VAR is one of:
                pkgincludedir [/usr/local/Cellar/mysql/8.0.12/include/mysql]
                pkglibdir     [/usr/local/Cellar/mysql/8.0.12/lib]
                plugindir     [/usr/local/Cellar/mysql/8.0.12/lib/plugin]

我尝试了许多不同的解决方案,但到目前为止都没有运气。感觉就像我要掉进一个兔子洞 - 非常感谢任何帮助;)

我查看了以下页面上的建议解决方案,但还没有解决方案:

我怀疑这个问题是由于同时安装 MySQL 服务器和连接器/C 中描述的问题...https://dev.mysql.com/doc/refman/5.7/en/c-api-multiple-installations.html 但不知道该怎么办。

【问题讨论】:

    标签: mysql django


    【解决方案1】:

    ld: library not found for -lssl -> 缺少 openssl 包,最好是 openssl 开发包。

    From this github issue

    brew install openssl
    export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/opt/openssl/lib/
    

    【讨论】:

    • 找到解决 -lssl library not found at github.com/brianmario/mysql2/issues/795 的建议尝试 brew install openssl 但得到 openssl 1.0.2q is already installed and up-to-date 尝试 brew install openssl@1.1 成功安装但 pip install mysqlclient 仍然失败并出现相同的错误 library not found for -lssl跨度>
    • 啊,我只需要运行export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/opt/openssl/lib/ 并得到Successfully installed mysqlclient-1.3.14
    猜你喜欢
    • 2017-11-05
    • 2021-01-03
    • 2021-03-09
    • 2017-02-15
    • 2013-10-11
    • 2012-11-29
    • 1970-01-01
    • 1970-01-01
    • 2013-07-13
    相关资源
    最近更新 更多