「mysql_config not found」エラーの解決方法

Djangoのバージョン3.0が今月リリースされました。

公式サイト: https://docs.djangoproject.com/en/3.0/releases/3.0/

前のプロジェクトでは2.2.4を使っていたのですが、フレームワークのバージョンが新しくなるとワクワクして使ってみたくなりますよね。

という訳で、今回はDjango3.0を試してみようと思いいつものように docker-composeで環境構築していた時の話です。

過去にまとめた記事を参考に進めていたところ、エラーが出て詰まってしまいました。

参考記事

mysql_config not foundエラー

requirements.txtに mysqlclient を記載し docker-compose up –buildでイメージを生成すると下記エラーが出ました。

Collecting mysqlclient==1.4.6
  Downloading https://files.pythonhosted.org/packages/d0/97/7326248ac8d5049968bf4ec708a5d3d4806e412a42e74160d7f266a3e03a/mysqlclient-1.4.6.tar.gz (85kB)
    ERROR: Command errored out with exit status 1:
     command: /usr/local/bin/python -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-slhgnavc/mysqlclient/setup.py'"'"'; __file__='"'"'/tmp/pip-install-slhgnavc/mysqlclient/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-install-slhgnavc/mysqlclient/pip-egg-info
         cwd: /tmp/pip-install-slhgnavc/mysqlclient/
    Complete output (12 lines):
    /bin/sh: mysql_config: not found
    /bin/sh: mariadb_config: not found
    /bin/sh: mysql_config: not found
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-install-slhgnavc/mysqlclient/setup.py", line 16, in <module>
        metadata, options = get_config()
      File "/tmp/pip-install-slhgnavc/mysqlclient/setup_posix.py", line 61, in get_config
        libs = mysql_config("libs")
      File "/tmp/pip-install-slhgnavc/mysqlclient/setup_posix.py", line 29, in mysql_config
        raise EnvironmentError("%s not found" % (_mysql_config_path,))
    OSError: mysql_config not found
    ----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
ERROR: Service 'django' failed to build: The command '/bin/sh -c pip install --no-cache-dir -r $APP_PATH/requirements.txt' returned a non-zero code: 1

解決方法

調べまくってようやく解決方法がわかりました。

Dockerfileに下記を追記し解決しました。

FROM python:3.6.10-alpine3.11
ENV APP app
RUN mkdir -p $APP
ENV APP_PATH /$APP
RUN apk add --no-cache build-base mariadb-connector-c-dev ← ここを追記
COPY requirements.txt $APP_PATH/
RUN pip install --no-cache-dir -r $APP_PATH/requirements.txt
WORKDIR $APP_PATH

Related Posts