In this article, we will walk you through how to connect a Django project to a dockerized MySQL database. This setup allows you to leverage the benefits of containerization, such as consistency across different development environments and simplified dependency management. By the end of this guide, you will have a Django application seamlessly interacting with a Dockerized MySQL database, enhancing your development workflow and project scalability.
Django project settings
The first setup will be on the Django project. The goal is to add the required lines of code for database connection. To connect Django with MySQL, there are 2 must-have dependencies, besides others used in your project.
Install dependencies
Install the 2 required MySQL dependencies: pymysql and cryptography These can be installed directly or added to the requirements.txt file and installed together with other dependencies.
pip install pymysql
pip install cryptography
Update the the ‘__init__.py’ file
This file is in the project folder, the same folder as the settings.py file. Add the following lines of code.
import pymysql
pymysql.install_as_MySQLdb()
Update the settings.py file
Add the following block of code containing the MySQL access details. Remove the default database configurations. Note that you will use the same database details here when creating a MySQL Docker container.
#Remove this or any other database defination.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
#Add this
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'djangodb',
'USER': 'djangouser',
'PASSWORD': 'userpass',
'HOST': 'localhost',
'PORT': '3306',
'OPTIONS': {
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"
}
}
}
#Note: Replace the following with the preferred database details: NAME, USER,PASSWORD.
Save the above changes and proceed to create a docker container.
Install docker-engine
To create docker containers, you need to have docker installed and the required dependencies. See How to install Docker on a Linux machine. You can also refer to docker documentation on how to install docker.
Once docker is set up, proceed to create a MySQL docker container.
Create a MySQL Docker container
Run the following command to create a MySQL docker container.
docker run -d \
--name my-django-mysql \
-e MYSQL_DATABASE=djangodb \
-e MYSQL_USER=djangouser \
-e MYSQL_PASSWORD=userpass \
-e MYSQL_ROOT_PASSWORD=rootpassword \
-p 3306:3306 \
-v /home/ubuntu/database:/var/lib/mysql \
mysql:latest
Explanation:
summary: |
The Docker command runs a MySQL container in detached mode with the
name `my-django-mysql`. It sets up environment variables for the
Database Name =`djangodb`
Database User =`djangouser`
User Password =`userpass`
Root Password =`rootpassword`.
The MySQL container's port 3306 is mapped to the host's port 3306. Additionally, the container's data directory (`/var/lib/mysql`) is mounted to a persistent storage location on the host machine at `/home/ubuntu/database`. This ensures that the database data is retained even if the container is stopped or removed.
- Adjust the above values as needed. Ensure the values you use here are updated in the settings.py file of your Django project.
Allow a few seconds for a connection to establish after you create the Docker container. Once you confirm the above works, proceed and do migrations and create a super admin for your Django application. Adjust all other settings as needed in a production environment. With the above description, we will have been able to connect a Django Project to a Dockerized MySQL Database
Note that this article on Django project settings does not extend how to deploy your application on a live production server. After completing the setup, check out any of the resources below:
- How to deploy Django project on an Apache Server
- Deploy a Django Application on EC2 Instance with Nginx
Note, that to manage docker containers, you need to have a basic understanding of how docker containers work. See an article on Getting Started with Docker or watch the video below.
Make a donation to support us
Web Hosting and email hosting Packages
Related content: