Saturday, October 11, 2025
HomeLanguagesHow to integrate Mysql database with Django?

How to integrate Mysql database with Django?

Django is a Python-based web framework that allows you to quickly create efficient web applications. It is also called batteries included framework because Django provides built-in features for everything including Django Admin Interface, default database – SQLlite3, etc. 

Installation

Install the Mysql database, After downloading install the setup and set the admin and password.

https://dev.mysql.com/downloads/installer/

Install Django

pip install django

Then install another library to use the MySQL database

pip install mysqlclient

Steps to connect MySQL to Django

Step 1: Create a new project

django-admin startproject MyDB

Step 2: Move to the MyDB folder.

cd MyDB

Step 3: Create a MySql database.

Step 4: Update the settings.py

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

Python3




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


First, we have replaced the ‘django.db.backends.sqlite3’ to ‘django.db.backends.mysql’. This is basically indicating we shift SQLite to MySQL database.

  • NAME: It indicates the name of the database we want to connect.
  • USER: The MYSQL username is the one who has access to the database and manages it.
  • PASSWORD: It is the password of the database. 
  • HOST: It is indicated by “127.0.0.1” and “PORT” “3306” that the MySQL database is accessible at hostname “0.0.1” and on port “3306.”

Step 5: Run the server.

python manage.py runserver

Step 6: Run the migration command

python manage.py makemigrations
How to connect MySQL to Django

 

python manage.py migrate
How to connect MySQL to Django

 

RELATED ARTICLES

Most Popular

Dominic
32352 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6720 POSTS0 COMMENTS
Nicole Veronica
11885 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6840 POSTS0 COMMENTS
Ted Musemwa
7104 POSTS0 COMMENTS
Thapelo Manthata
6795 POSTS0 COMMENTS
Umr Jansen
6794 POSTS0 COMMENTS