How to connect the MySQL Database to the Django Project

How to connect the MySQL Database to the Django Project

This tutorial will explain how to connect MySQL Database with your Django Project. By the end of the tutorial, you will be able to connect and use MySQL Database with your Django Application. You will also learn how to migrate your data to the database.

MySQL is the most popular and common used Open-source relation management database system. Django officially supports many databases like PostgresSQL, MySQL, Oracle, SQLite. For using MySQL with Django 2.1 and above, we need MySQL 5.6 minimum version to support.

Connecting Databases with Django Project


MySQL API driver used to connect the MySQL database with your Django/Python Project. MySQLClient is basically an adapter used for interacting with Django ORM (Object Relational Management). Basically it is going to install Django code which is needed to connect with the database. We can install mysqlclient using pip.

pip install mysqlclient 

Change Database Settings in Django Application

Create Database in MySQL

First, we need to create a database in MySQL for the Django Project. Open your MySQL Command Line Client and use CREATE syntax to create a database in MySQL.

 mysql> CREATE DATABASE sample; 

MySQL Database Setting Code

We believe that you have created your project without its migrations. So open your database setting code in file. You will similar code like bellow –

# Database

    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),

After going to database settings in your file. Replace the database configuration code with the bellow code.

# Database

    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'DB_name',
        'HOST': '',
        'PORT': '3306',
        'USER': 'DB_user',
        'PASSWORD': 'DB_password',


  • DB_name – is your database name created above
  • DB_user – is your MySQL Username
  • DB_password – is your MySQL password

Tip – In your database configuration, anyone can see your database username and password. So to protect sensitive data see How to Protect Sensitive Data in Django.

Once you replace the database configuration code, you need to make migrations.

python makemigrations 
python migrate

Done!. Now you are ready to use your Project. You can see your database fields in MySQL Database.

Thanks for reading !

Originally published by Huzaif Sayyed at

mysql python Database Django

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Best MySQL DigitalOcean Performance – ScaleGrid vs. DigitalOcean Managed Databases

Compare ScaleGrid MySQL vs. DigitalOcean Managed Databases - See which offers the best MySQL throughput, latency, and pricing on DigitalOcean across workloads.

Python Database Connection - How to Connect Python with MySQL Database

This video on 'Python Database Connection', you will learn how to establish a connection between Python and MySQL DB and perform CRUD operations on it.

Python Django Tutorial | Django Course

🔥Intellipaat Django course: 👉This Python Django tutorial will help you learn what is django web development &...

Simple Register and Login Application using Python 3 and MySQL Database Server

This article explains the creations of a simple login and user registration application with Python3 and MySQL Database Server. In this tutorial, you'll see Simple Register and Login Application using Python 3 and MySQL Database Server

Django Rendering Data from Mysql Database

In this article we are going to talk about Django Rendering Data from Mysql Database , OK by default when you create