0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Djangoのアプリケーションをローカル環境に、MySQLをDockerで構成しようしたらエラーがでた

Posted at

エラーの内容

django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
Did you install mysqlclient?

mysqlclientをインストールしましたか??なんだろう

mysqlclientとは?

PythonからMySQLに接続するためのパッケージとのこと。
Djangoの公式サイトでは「mysqlclient」の方を推奨しているとか。

pymysqlって奴もあるけど、公式推奨を使おう。

確かに入れてないのでインストール。

pip install mysqlclient

まだエラーが出る。。

 OSError: mysql_config not found

上記のようなエラーが。。

Pypiのmysqlclientのインストール方法を見てみた。

mysqlclientをインストールする前に、brewでMySQLか、サーバー不要な人はmysql-clientをインストールしろと。

今回は、DockerのMySQLを使用するためサーバー不要の手順で実施。

$ brew install mysql-client
$ echo 'export PATH="/usr/local/opt/mysql-client/bin:$PATH"' >> ~/.bash_profile
$ export PATH="/usr/local/opt/mysql-client/bin:$PATH"
$ pip install mysqlclient

成功!!!

Djangoのsetting.pyの設定

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': '',     # コンテナ作成した時に指定したDBの名前
        'USER': 'root',
        'PASSWORD': '', # rootのパスワード
        'HOST': '127.0.0.1',
        'PORT': '3306'  # コンテナ作成した時に、特にポート指定してなければこのまま
    }
}

python manage.py runserverを実行。。。。

うまく動きました!!

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?