Learn how to connect MySQL database with Python Django windows. Django is a free, open-source web framework written in the most popular Python programming language. It allows for scalability, re-usability, and rapid development. Django can be connected to different databases like SQLite, MySQL, PostgreSQL, etc.
In this tutorial, you will learn how to connect mysql database in django . You’ll learn how to create your Django project in Virtual Environment. You will also learn how to edit your Django Project Settings file and connect your web application to MySQL Database.
Prerequisites – Whether you use Windows, Ubuntu or Mac OS, Install MySQL on your system before proceeding through this tutorial.
I am assuming that you have already installed the MySQL Database. So let’s get started.
Create the Initial Django Project Skeleton
We are going to install Django in Virtual Environment. So let’s create a directory with a meaningful name and move to that directory. For example, I am naming it blog.
mkdir blog cd blog
Now, we will install a virtual environment in that directory using virtualenv.
virtualenv env
After creating virtualenv
, Activate that virtual env.
source env/bin/activate
Now let’s install Django in virtualenv. I am using Python Pip3.
pip3 install django
And create a Django Project using the django-admin command.
django-admin startproject blog .
Note – We are using a single period .
means the current working directory, otherwise, it will look like this – blog/blog
.
Install MySQL Database Connector and edit Settings
To use MySQL with our django project, we will need a Python 3 database connector library compatible with Django. So, we will install the database connector, mysqlclient
, which is a forked version of MySQLdb
.
According to the mysqlclient
documentation, “MySQLdb
is a thread-compatible interface to the popular MySQL
database server that provides the Python database API.” The main difference being that mysqlclient
has the added benefit of including Python 3 support.
Lets first install python3-dev which contains the header files you need to build Python extensions. Run the following command to install python3-dev
.
sudo apt-get install python3-dev
Once python3-dev
is installed, we can install the necessary Python and MySQL development headers and libraries:
sudo apt-get install python3-dev libmysqlclient-dev
When you see the following output:
Output: After this operation, 11.8 MB of additional disk space will be used. Do you want to continue? [Y/n]
Enter y
then press ENTER
to continue.
Now let’s install mysqlclient. Run the following command for installing mysqlclient
.
pip3 install mysqlclient
Create MySQL Database For Your Django Project
You can create a Database using PhpMyAdmin or using the command line. For our tutorial, we are using the command line interface. Login into your MySQL with your username and password.
mysql -u db_user -p
Let us show some database by running mysql show command
mysql> SHOW DATABASES;
You’ll see output similar to the following, assuming that you haven’t created any databases yet:
Output +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec)
We will be creating a Database with the name blog_data.
mysql> CREATE DATABASE blog_data;
Next, verify that the database is now listed in your list of available databases:
mysql> SHOW DATABASES;
You should see that the blog_data
database is among the databases included in the output:
output +--------------------+ | Database | +--------------------+ | information_schema | | blog_data | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec)
You’ve successfully created a MySQL database for your blog.
Whenever you’d like to exit MySQL server, press CTRL
+ D
.
Add the MySQL Database Connection to your Application Settings.py
Finally, we are going to be adding the database credentials to your Django application.
Note: It is important to remember that connection settings, according to the Django documentation, are used in the following order:
– OPTIONS
– NAME
, USER
, PASSWORD
, HOST
, PORT
– MySQL option files.
Open your settings.py file and replace your current DATABASES line with the following –
# settings.py file # Database # https://docs.djangoproject.com/en/2.1/ref/settings/#databases DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'your_database_name', # example - blog_data 'USER': 'your_mysql_user', 'PASSWORD': 'your_mysql_password', 'HOST': 'localhost', 'PORT': '80', } }
Change the database details respectively.
Test MySQL Connection to Application
We will be verifying our Django Application and check if our Application configuration detects MySQL Server.
python3 manage.py runserver
If everything is working fine, you have created a database and if you have not applied migrations, you will get output like this –
Output: You have 17 unapplied migration(s). ... Run 'python manage.py migrate' to apply them.
This means our application. Now we just need to migrate our initial data to our database. Run the migrations commands.
python3 manage.py makemigrations python3 manage.py migrate
Conclusion
We have created a database and applied the configuration to our projects. Now you can create your own models and apply migrations accordingly.
Thi Tutorial Explains Django – mysql connection, connect to mysql, connect to mysql, how to connect mysql database in python django, connecting django to mysql, connect with mysql, connecting with mysql.
GitHub – Run Example Locally
It is also available on GitHub – https://github.com/studygyaan/How-to-Connect-MySQL-Database-in-Django-Project
Clone the Repository –
git clone https://github.com/studygyaan/How-to-Connect-MySQL-Database-in-Django-Project.git
Change Directory
cd How-to-Connect-MySQL-Database-in-Django-Project
Create Virtual Environment – VirtualENV
virtualenv env
Activate Virtual Environment
source env/bin/activate
Run requirement.txt file to install libraries using Pip3
pip3 install -r requirements.txt
Run the server
python3 manage.py runserver
And open http://localhost:8000/ in your browser.