Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Django
  • Views
  • Model
  • Template
  • Forms
  • Jinja
  • Python SQLite
  • Flask
  • Json
  • Postman
  • Interview Ques
  • MongoDB
  • Python MongoDB
  • Python Database
  • ReactJS
  • Vue.js
Open In App
Next Article:
How to integrate Mysql database with Django?
Next article icon

How to integrate Mysql database with Django?

Last Updated : 07 Nov, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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

Let's first understand how to install MySQL to install the Mysql database you can refer to the link below and install MySQL on your local machine.

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

After downloading install the setup and set the admin and password for mysql. Then, install Django, and mysqlclient, for that you need to run the following command in your terminal.

pip install django pip install mysqlclient 

Steps to connect MySQL to Django

Step 1: Create a new project and start the process to connect django with mysql.

django-admin startproject MyDB  

Step 2: Move to the MyDB folder.

cd MyDB  

Note: You should have MySQL installed and be able to log in with appropriate privileges to create a database.

Step 3: Once you're logged into MySQL, you can create a new database. Replace your_database_name with the desired name for your database.

CREATE DATABASE your_database_name 

For better understanding you may refer to the article: 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
makemigration command
python manage.py migrate 
How to connect MySQL to Django
Apply migrations

Next Article
How to integrate Mysql database with Django?

V

vivekpisal12345
Improve
Article Tags :
  • Python
  • Web Technologies
  • mysql
  • Python Django
  • Python Framework
Practice Tags :
  • python

Similar Reads

    How to use PostgreSQL Database in Django?
    This article revolves around how can you change your default Django SQLite-server to PostgreSQL. PostgreSQL and SQLite are the most widely used RDBMS relational database management systems. They are both open-source and free. There are some major differences that you should be consider when you are
    2 min read
    Designing a RESTful API to interact with SQLite database
    In this chapter, we will create Django API views for HTTP requests and will discuss how Django and Django REST framework process each HTTP request.  Creating Django ViewsRouting URLs to Django views and functionsLaunching Django's development serverMaking HTTP requests using the command-line toolMak
    10 min read
    Working with Multiple Databases in Django
    Django stands out as a powerful and versatile framework for building dynamic and scalable applications. One of its notable features is its robust support for database management, offering developers flexibility in choosing and integrating various database systems into their projects. In this article
    3 min read
    How to Connect Python with SQL Database?
    In this article, we will learn how to connect SQL with Python using the MySQL Connector Python module. Below diagram illustrates how a connection request is sent to MySQL connector Python, how it gets accepted from the database and how the cursor is executed with result data.SQL connection with Pyth
    2 min read
    How to Clone and Save a Django Model Instance to the Database
    In the realm of web development, Django stands out as a robust and versatile framework for building web applications swiftly and efficiently. One common requirement in Django projects is the ability to clone or duplicate existing model instances and save them to the database. This functionality is p
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences