After completing a Django project or during development, you must determine which database to use. By default, Django uses db.sqlite3 for its database. However, it is not recommended to use this database for a Django project in a production environment. Mysql database is one of the options for databases used in production. Other options include the PostgreSQL database, MariaDB, Oracle Database, MongoDB, SQL Server, Amazon RDS, Google Cloud SQL, and Azure SQL Database. This article illustrates how to connect a Django project with a MySQL database. See how to connect to the Postgres database here How to Connect a Django Application to PostgreSQL
Prerequisites:
- A Django project set up
- MySQL installed and configured
- Basic knowledge of Django and MySQL
Step 1: Install the pymysql package
Pymysql is a dependency that plays a key role in establishing a connection of the Django project with the Mysql database. Run the following command to install it: Ensure to update the version of pip as older versions may result in errors
pip install --upgrade pip #upgrade pip, not a must but recommended.
pip install pymysql
Step 2: Configure Django settings
Open the settings.py file of your project, and scroll to the database settings section. By default, we have SQLite3 database connection settings: see below.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
Replace the above code with the following:
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'"
}
}
}
Replace the NAME, USER, and PASSWORD with the database credentials as created on MySQL database. For Linux local or virtual server setup: see How to Install MySQL Database on Linux. On shared hosting with Cpanel access, see How To Create MySQL Database on Cpanel. Use ‘localhost’ for the HOST unless the database is hosted on an external server. In that case, specify the IP or the hostname of the external server.
Step 3: Update the ‘__init__.py’ file
Add the following code to the __init__.py file. This file is found under the project folder under the same folder with settings.py
import pymysql
pymysql.install_as_MySQLdb()
Step 4: Apply Database Migrations and create a superuser
After completing the updates, run the migrations and create a superuser. Run the following commands
python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser
python manage.py runserver
If the above works as expected, then the connection is successful. If you get the following error as captured or any other error, capture the key part of the error and search online:
File "/home/danny/Desktop/Django training 19th/djangoenv/lib/python3.11/site-packages/pymysql/_auth.py", line 266, in caching_sha2_password_auth
data = sha2_rsa_encrypt(conn.password, conn.salt, conn.server_public_key)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/danny/Desktop/Django training 19th/djangoenv/lib/python3.11/site-packages/pymysql/_auth.py", line 143, in sha2_rsa_encrypt
raise RuntimeError(
RuntimeError: 'cryptography' package is required for sha256_password or caching_sha2_password auth methods
From the above, it shows that a package name cryptography is missing. To resolve it, install cryptography
pip install cryptography
After installing all required packages, you should perform the migration without encountering any errors.
Conclusion:
With the above few steps, we have been able to configure a Django application with MySQL Database. For any questions, drop a comment in the comment section or check out our contacts for assistance.
Make a donation to support us
Web Hosting and email hosting Packages
Related content:
- A Practical Tutorial for Dockerizing Software Applications
- How to Configure a Docker App to a Domain Name
- Getting Started with Docker | Docker commands
- How To Run Scripts in Linux
- Deploy a Django Application on EC2 Instance with Nginx
- How to configure a domain to a docker container and install an SSL certificate on AWS