MySQLにDjangoからアクセスする方法

私はxamppでMySQLを立ち上げて、Djangoから接続してみたので、
その方法を書き記します。

mysqlclientをインストールする

公式では、mysqlclientがお勧めされていたので、これをインストールしましょう。
mysqlclient

pip install mysqlclient
django公式

DBの情報を設定する

settings.py

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

接続確認をする

djangoのmanage.pyのあるフォルダに移動して下のコマンドで確認

python manage.py dbshell

接続結果

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 10.1.37-MariaDB mariadb.org binary distribution

Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です