DjangoとMySQLを接続した時の備忘録です。
MySQLの設定を確認
Django側の設定で必要な項目は、
- データベース名
- ユーザー名(権限を持っている)
- ユーザーのパスワード
- ホスト名
- ポート番号
です。これらを実際に調べていきます。
MySQLサーバーへ入る
1 2 |
[vagrant@localhost ~]$ mysql -u Admin -pP@ssw0rd ⬆️ユーザーのパスワード |
¥sでデータベースの情報を取得します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
mysql> \s -------------- mysql Ver 14.14 Distrib 5.7.29, for Linux (x86_64) using EditLine wrapper Connection id: 4 Current database: ⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️使うのはこれ!!! Current user: Admin@localhost SSL: Not in use Current pager: stdout Using outfile: '' Using delimiter: ; Server version: 5.7.29 MySQL Community Server (GPL) Protocol version: 10 Connection: Localhost via UNIX socket Server characterset: latin1 Db characterset: latin1 Client characterset: utf8 Conn. characterset: utf8 UNIX socket: /var/lib/mysql/mysql.sock Uptime: 16 min 29 sec Threads: 1 Questions: 24 Slow queries: 0 Opens: 113 Flush tables: 1 Open tables: 106 Queries per second avg: 0.024 -------------- |
ユーザー名:Admin
ホスト名:loaclhost
になります。
ユーザーの作成方法、権限を付与する方法などは、公式ドキュメントを参照してみてください。
【MySQL】公式マニュアル
作成したデータベース名は"DiaryApp"にしました。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
mysql> create database DiaryApp; Query OK, 1 row affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | DiaryApp | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec) |
Django側の設定
次は、Django側の設定です。
setting.pyに設定を足すのと、models.pyを作成します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'DiaryApp', 'USER': 'Admin', 'PASSWORD': 'P@ssw0rd', 'HOST': '', 'PORT': '3306', 'OPTIONS': { # 制約を厳しくチェック 'sql_mode': 'traditional', }, } } |
1 2 3 4 5 6 7 8 9 10 11 |
# coding: utf-8 from django.db import models from datetime import date class Diary(models.Model): date = models.DateField(default=date.today, primary_key=True) title = models.CharField(max_length=128) body = models.TextField() created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) publishing = models.BooleanField(default=True) |
マイグレーション
models.pyに書いた「モデル」をデータベースに反映させます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
[vagrant@localhost diary_app]$ python manage.py makemigrations [app名] [vagrant@localhost diary_app]$ python manage.py migrate Operations to perform: Apply all migrations: admin, 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 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 sessions.0001_initial... OK |
MySQLに入って、正しくテーブルが作られているか確認してみます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
mysql> use DiaryApp Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> show tables; +----------------------------+ | Tables_in_DiaryApp | +----------------------------+ | auth_group | | auth_group_permissions | | auth_permission | | auth_user | | auth_user_groups | | auth_user_user_permissions | | diary_app_diary | | django_admin_log | | django_content_type | | django_migrations | | django_session | +----------------------------+ 11 rows in set (0.00 sec) |
[…] MySQLインストール参考 How to Install MySQL on CentOS 7 MySQL5.7の初期設定はこちら CentOS7にMySQLをインストールした時の備忘録 MySQLの初期設定は別記事にまとめました。 DjangoとMySQLの接続設定備忘録 […]