Integrating MySQL in Django

Django uses SQLite as its database by default. But, what if you need to integrate some other database to your Django web application.
In this blog, we will look into using MySQL database for a Django project.

Table of contents:

  1. Downloading the resources
  2. Setting up the environment
  3. Setting up a project
  4. Configuring Django project settings
  5. Testing the integration

Let us get started.

Downloading the resources

Besides having Django installed in your machine, a MySQL Client must also be running actively on your machine.

If you do not have MySQL installed in your machine, get it here.

If you already have one, make sure it is up and running.
task-mgr

Just like this, on windows.

Next, we need the requirements for connecting Django with MySQL.

Setting up the environment

Execute the following commands in your virtual environment in order to integrate MySQL database in Django:

pip install django-mysql mysqlclient

That's all. Next up, we'll set up our project.

Setting up a project

First, we need a project to get started. Create it using:

django-admin startproject mysite

This is what the directory structure looks like:

mysite/
    manage.py
    mysite/
        __init__.py
        settings.py
        urls.py
        wsgi.py

Now, we need an application inside our project. Create it using:

Note: To create your app, make sure you're in the same directory as manage.py.

python manage.py startapp app

Now your app directory structure looks as:

app/
    __init__.py
    admin.py
    apps.py
    migrations/
        __init__.py
    models.py
    tests.py
    views.py

Configuring Django project settings

Finally, we need to configure the project settings to use our MySQL database.

Open the project's settings file, settings.py.

Now, scroll down to INSTALLED_APPS section and add 'django_mysql' as shown below:

INSTALLED_APPS = [
                    'django_mysql'
                 ]

Scroll down to DATABASES and remove the existing code. Add the below fragment:

DATABASES = {
        'default': {
                'ENGINE' : 'django.db.backends.mysql',
                'USER' : 'root',    # default username, change accordingly
                'PASSWORD' : '',    # default password, change accordingly
                'NAME' : '',        # enter your database name
                'HOST' : '127.0.0.1',
                'PORT' : '3306',    # default port for MySQL is 3306, change accordingly
                'OPTIONS' : {
                        'init_command' : "SET default_storage_engine=MyISAM",
                        'charset' : 'utf8mb4',
                }
        }
}

Lastly, Save the changes.

Testing the integration

To test whether we finally made a successful Django-MySQL integretion, lets's create a model and check if it executes without errors.

Inside, the app directory, open up the models.py and paste the below code for a sample table.

from django.db import models

class documents(models.Model):
    title = models.CharField(max_length=200)
    description = models.CharField(max_length=500)

Save the changes and exit.

In the terminal (virtual environment must be active), inside the project directory, execute the following command:

python manage.py makemigrations
python manage.py migrate

If the execution is successful, the Django-MySQL integration is established.