【问题标题】:django running on local machine cannot connect to docker container running MYSQL在本地机器上运行的 django 无法连接到运行 MYSQL 的 docker 容器
【发布时间】:2018-03-15 11:49:49
【问题描述】:

大家好,所以我试图在我的本地机器上运行 django 以连接到运行 mysql 的 docker 容器,但是在尝试通过 pip 安装 mysqlclient 时一直出错。首先,我在尝试安装 mysqlclient 时遇到此错误:

Collecting mysqlclient
  Using cached mysqlclient-1.3.12.tar.gz
    Complete output from command python setup.py egg_info:
    /bin/sh: mysql_config: command not found
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/private/var/folders/8d/xdwdnphs1w5b_vtxrc67__5h0000gn/T/pip-build-mejs2ys1/mysqlclient/setup.py", line 17, in <module>
        metadata, options = get_config()
      File "/private/var/folders/8d/xdwdnphs1w5b_vtxrc67__5h0000gn/T/pip-build-mejs2ys1/mysqlclient/setup_posix.py", line 44, in get_config
        libs = mysql_config("libs_r")
      File "/private/var/folders/8d/xdwdnphs1w5b_vtxrc67__5h0000gn/T/pip-build-mejs2ys1/mysqlclient/setup_posix.py", line 26, in mysql_config
        raise EnvironmentError("%s not found" % (mysql_config.path,))
    OSError: mysql_config not found

然后我继续安装mysql-connector-cvia brew。我重新运行pip install mysqlclient,现在我得到了这个错误:

Collecting mysqlclient
  Using cached mysqlclient-1.3.12.tar.gz
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/private/var/folders/8d/xdwdnphs1w5b_vtxrc67__5h0000gn/T/pip-build-8wu06grt/mysqlclient/setup.py", line 17, in <module>
        metadata, options = get_config()
      File "/private/var/folders/8d/xdwdnphs1w5b_vtxrc67__5h0000gn/T/pip-build-8wu06grt/mysqlclient/setup_posix.py", line 54, in get_config
        libraries = [dequote(i[2:]) for i in libs if i.startswith('-l')]
      File "/private/var/folders/8d/xdwdnphs1w5b_vtxrc67__5h0000gn/T/pip-build-8wu06grt/mysqlclient/setup_posix.py", line 54, in <listcomp>
        libraries = [dequote(i[2:]) for i in libs if i.startswith('-l')]
      File "/private/var/folders/8d/xdwdnphs1w5b_vtxrc67__5h0000gn/T/pip-build-8wu06grt/mysqlclient/setup_posix.py", line 12, in dequote
        if s[0] in "\"'" and s[0] == s[-1]:
    IndexError: string index out of range

有没有人在连接到在 docker 容器中运行的 mysql 时尝试在本地机器上运行 django 时遇到错误?

【问题讨论】:

  • 从/var/cache/apt/archieves中删除mysqlclient-1.3.12.tar.gz文件
  • @AvinashRaj 但我在 docker 中运行 mysql。我是否需要在我的本地主机上安装 mysql 才能从本地机器运行 django 应用程序以连接到运行 mysql 的 docker 容器

标签: python mysql django docker


【解决方案1】:

当您使用 macOS 时,请尝试以下代码:

brew remove mysql-connector-c
brew install mysql
pip3 install mysqlclient

这对你有用。

【讨论】:

  • 但我在 docker 中运行 mysql。我是否需要在我的本地主机上安装 mysql 才能从本地机器运行 django 应用程序以连接到运行 mysql 的 docker 容器
  • 相信我,这是simplist 的做法。 mysqlclientpython包依赖于mysql包中的C lib,所以这种方式是simplist的做法。
  • 当然还有更多的选项,比如 stackoverflow.com/questions/30990488/… ,但我推荐给你最好的方法。还有一件事,当你做brew install mysql 时,这并不意味着running mysql server :)
  • 谢谢,但现在我在尝试您的建议时遇到了错误
  • 我收到的错误是ld: library not found for -lssl clang: error: linker command failed with exit code 1
猜你喜欢
  • 1970-01-01
  • 2020-09-28
  • 2021-07-02
  • 1970-01-01
  • 2019-11-16
  • 2017-06-02
  • 2017-11-03
  • 2021-01-11
  • 2021-11-14
相关资源
最近更新 更多