Django MySql Database
The page explains how to configure Django to use a MySql database.
Django is a leading Python web framework built by experts using a bateries-included concept. Being such a mature framework Django provides an easy way to switch from the default SQLite database to other database engines like MySql, PostgreSQL, or Oracle. For newcomers, MySql is a powerful open-source relational database where the information is correlated and saved in one or more tables.

Django Database System

Django provides a generic way to access multiple database backends using a generic interface. In theory, Django empowers us to switch between DB Engines without updating the SQL code. The default SQLite database usually covers all requirements for small or demo projects but for production use, a more powerful database engine like MySql or PostgreSQL is recommended.
The database settings are saved in the file referred by manage.py file. For all Django starters, this file is saved inside the core directory:
1
< PROJECT ROOT >
2
|
3
|-- manage.py # Specify the settings file
4
|
5
|-- core/ # Implements app logic and serve the static assets
6
| |-- settings.py # Django app bootstrapper
7
| |-- wsgi.py # Start the app in production
8
| |-- urls.py # Define URLs served by all apps/nodes
Copied!
Let's visualize the contents of the settings.py file that configures the database interface.
1
# File: core/settings.py
2
...
3
DATABASES = {
4
'default': {
5
'ENGINE': 'django.db.backends.sqlite3',
6
'NAME' : 'db.sqlite3',
7
}
8
}
9
...
Copied!
The above snippet is provided by when Django scaffolds the project. We can see that the SQLite driver is specified by the ENGINE variable.

Update for MySql

To use MySql as the backend engine for a Django project, we need to follow a simple setup:
    Install the MySql Server (we can also use a remote one)
    Install the Mysql Python driver - used by Django to connect and communicate
    Create the Mysql database and the user
    Update settings Django
    Execute the Django migration and create the project tables
​

Install MySql Server

The installation process is different on different systems but this phase should not be a blocking point because Unix systems provide by default a MySql server and for Windows, we can use a visual installer. For more information please access the download page and select the installer that matches your operating system:

​

Install the Python Driver

To successfully access the Mysql Engine, Django needs a driver (aka a connector) to translate the Python queries to pure SQL instructions.
1
$ pip install mysqlclient
Copied!
The above instruction will install the Python MySql driver globally in the system. Another way is to use a virtual environment that sandboxes the installation.
1
$ # Create and activate the virtual environment
2
$ virtualenv env
3
$ source env/bin/activate
4
$
5
$ # install the mysql driver
6
$ pip install mysqlclient
Copied!

​

Create the MySql Database

During the initial setup, Django creates the project tables but cannot create the database. To have a usable project we need the database credentials used later by the Django project. The database can be created visually using a database tool (like MySQL Workbench) or using the terminal:
1
CREATE DATABASE mytestdb;
Copied!
Create a new Mysql user
1
$ CREATE USER 'test'@'localhost' IDENTIFIED BY 'Secret_1234';
Copied!
Grant all privileges to the newly created user
1
GRANT ALL PRIVILEGES ON `mytestdb` . * TO 'test'@'localhost';
2
FLUSH PRIVILEGES;
Copied!

​

Update Django Settings

Once the MySql database is created we can move on and update the project settings to use a MySql server.
1
# File: core/settings.py
2
...
3
DATABASES = {
4
'default': {
5
'ENGINE' : 'django.db.backends.mysql', # <-- UPDATED line
6
'NAME' : 'mytestdb', # <-- UPDATED line
7
'USER' : 'test', # <-- UPDATED line
8
'PASSWORD': 'Secret_1234', # <-- UPDATED line
9
'HOST' : 'localhost', # <-- UPDATED line
10
'PORT' : '3306',
11
}
12
}
13
...
Copied!
​

Start the project

The next step in our simple tutorial is to run the Django migration that will create all necessary tables.
1
$ # Create tables
2
$ python manage.py makemigrations
3
$ python manage.py migrate
Copied!
Start the Django project
1
$ # Start the application (development mode)
2
$ python manage.py runserver
Copied!
At this point, Django should be successfully connected to the Mysql Server and we can check the database and list the newly created tables during the database migration.
Django - The default page
Last modified 1mo ago