Skip to content

How to integrate MySql database with Django?

Download & install latest available version XAMPP. Remember MySQL will integrate with Django only with MariaDB 10.4 or later.

Create a directory for the project with following command.
mkdir mysqldjangotest

Enter into the directory
cd mysqldjangotest

Install Virtual Environment
python -m venv .venv

Activate Virtual Environment
.venv\Scripts\Activate.ps1

Install Django
(.venv)> pip install django

Start Project
(.venv)> django-admin startproject mysqlproject .

Enter the project directory
(.venv)> cd mysqlproject

Install MySQL Library for Django
pip install mysqlclient

Create MySQL Database from XAMPP PHPMYADMIN.

Open settings.py in VS Code and inside the DATABASES variable configure MySQL database values, and add values of your database.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'djangodb', # name your own database here
        'USER': 'root',
        'PASSWORD': '',
        'HOST': 'localhost',
        'PORT': '3306',
    }
}

Run the Server
(.venv)> python manage.py runserver

Run the Migration Command
(.venv)> python manage.py makemigrations

(.venv)> python manage.py migrate

Leave a Reply

Your email address will not be published. Required fields are marked *