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でMAMPのMySQLへ接続

Last updated at Posted at 2021-07-08

#はじめに
Djangoのデータベースを標準のsqliteから、MAMPのMySQLへ変更する手順を記載します。

##1.データベースの作成
ターミナルで以下コマンドを実行し、binディレクトリへ移動

cd /Applications/MAMP/Library/bin/

MySQLのサーバーを起動し、データベースへ接続
※パスワードは初期設定の場合、 root

mysql.server restart
./mysql -u root -p

データベースを作成(今回、データベース名はtest)
作成がされているか確認。

mysql> create database test;
Query OK, 1 row affected (0.06 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| sys                |
+--------------------+

##2.作成したアプリ(Django)でpymysqlをインストール

$ pip install pymysql

##3.manage.pyの修正

manage.py
import pymysql

pymysql.install_as_MySQLdb()

##4.settings.pyの修正

settings.py
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'test', #作成したDB名
        'USER': 'root', #ユーザ名
        'PASSWORD': ,#パスワード
        'HOST': '/Applications/MAMP/tmp/mysql/mysql.sock',
        'PORT': '8889'  # MAMPのため,
    }
}

##5.マイグレーションを実行
python manage.py migrate でマイグレーションを実行し、データベースにテーブルを追加

$ python manage.py migrate
Operations to perform:
  Apply all migrations: admin, api, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying api.0001_initial... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying auth.0012_alter_user_first_name_max_length... OK
  Applying sessions.0001_initial... OK

##6.テーブルの確認
無事に作成できていることを確認

mysql> use test;
Database changed
mysql> show tables;
+----------------------------+
| Tables_in_test             |
+----------------------------+
| api_category               |
| api_profile                |
| api_task                   |
| auth_group                 |
| auth_group_permissions     |
| auth_permission            |
| auth_user                  |
| auth_user_groups           |
| auth_user_user_permissions |
| django_admin_log           |
| django_content_type        |
| django_migrations          |
| django_session             |
+----------------------------+
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?