Django
MySQL
Database Setup
Web Development
Python Framework

Setting Django up to use MySQL

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

Switching a Django project from SQLite to MySQL is mostly configuration work, but the details matter. You need a MySQL server, a Python driver, a database and user with the right permissions, and a Django DATABASES setting that matches the server's connection and character-set choices.

Install the MySQL Driver

Django needs a DB API driver to talk to MySQL. The most common choice is mysqlclient.

bash
pip install mysqlclient

If that build fails, the missing piece is usually system development libraries for MySQL or MariaDB. The exact package names depend on the operating system, but the Python package itself is the Django-side dependency you need.

Create the Database and User

Before changing Django settings, create a database and a dedicated user. A dedicated user is better than connecting as root because it limits accidental damage and makes production credentials easier to rotate.

sql
1CREATE DATABASE myapp
2  CHARACTER SET utf8mb4
3  COLLATE utf8mb4_unicode_ci;
4
5CREATE USER 'myapp_user'@'localhost' IDENTIFIED BY 'strong_password';
6GRANT ALL PRIVILEGES ON myapp.* TO 'myapp_user'@'localhost';
7FLUSH PRIVILEGES;

Using utf8mb4 is important because it supports full Unicode, including emoji and other characters that older MySQL encodings can mishandle.

Configure settings.py

Update Django's database configuration to point at MySQL.

python
1DATABASES = {
2    "default": {
3        "ENGINE": "django.db.backends.mysql",
4        "NAME": "myapp",
5        "USER": "myapp_user",
6        "PASSWORD": "strong_password",
7        "HOST": "127.0.0.1",
8        "PORT": "3306",
9        "OPTIONS": {
10            "charset": "utf8mb4",
11        },
12    }
13}

Using 127.0.0.1 instead of localhost is often a deliberate choice. Depending on the driver and local MySQL setup, localhost can trigger a Unix socket connection while 127.0.0.1 forces TCP. Either can be correct, but you should choose intentionally.

In real projects, keep credentials in environment variables instead of hard-coding them.

python
1import os
2
3DATABASES = {
4    "default": {
5        "ENGINE": "django.db.backends.mysql",
6        "NAME": os.environ["DB_NAME"],
7        "USER": os.environ["DB_USER"],
8        "PASSWORD": os.environ["DB_PASSWORD"],
9        "HOST": os.environ.get("DB_HOST", "127.0.0.1"),
10        "PORT": os.environ.get("DB_PORT", "3306"),
11        "OPTIONS": {"charset": "utf8mb4"},
12    }
13}

Run Migrations and Verify the Connection

Once the configuration is in place, initialize the schema:

bash
python manage.py migrate

Then confirm that Django can open the database connection:

bash
python manage.py dbshell

If migrations run and dbshell connects, the basic setup is working.

Existing Projects and Data Migration

If you are moving an existing project from SQLite to MySQL, changing DATABASES is only part of the work. You also need to move the actual data. A common path is:

  1. dump data from the old database
  2. switch Django settings to MySQL
  3. run migrations against MySQL
  4. load the data dump into the new database

For Django-managed data, dumpdata and loaddata can help, though large projects sometimes need more controlled migration steps.

Common Pitfalls

The most common issue is driver installation failure. If pip install mysqlclient fails, the fix is usually to install the native client libraries that the package compiles against.

Another pitfall is authentication or host mismatch. Access denied errors usually mean the user, password, allowed host, or grants do not match the connection Django is trying to make.

Encoding mistakes are also common. If you do not use utf8mb4, some Unicode characters may fail to store correctly or may round-trip incorrectly later.

Finally, be careful with SQL modes and schema assumptions when moving from SQLite. SQLite is permissive in ways MySQL is not, so a project that "just worked" in development can expose stricter behavior after the switch.

Summary

  • Install a MySQL driver such as mysqlclient.
  • Create a dedicated MySQL database and user before changing Django settings.
  • Configure DATABASES with the MySQL engine, credentials, host, port, and utf8mb4.
  • Run migrations and verify the connection with dbshell.
  • For existing projects, plan the data migration and watch for encoding, auth, and SQL-compatibility issues.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.