Configure Django ORM with MySQL

Django uses sqlite as the default database. In this post, I’ll explain how to configure Django settings to connect with MySQL Database

Prerequisite

  • Install Django
  • Install MySQL
  • Install mysqlclient

Configure settings.py file

In settings.py file under the project folder, change the DATABASES configuration.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': "db_name",
        'USER': 'user',
        'PASSWORD': "password",
        'HOST': "192.168.x.xx", #ip address of the mysql
        "PORT": "3306" #default is 3306
    }
}

Create Initial Migrations

Migrate initial django tables to the database, run

python manager.py makemigrations
python manager.py migrate

   Reprint policy


《Configure Django ORM with MySQL》 by Isaac Zhou is licensed under a Creative Commons Attribution 4.0 International License
  TOC