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.
Introduction
Configuring Django for MySQL is mostly a matter of installing the client driver, creating the database and user, and pointing settings.py at the correct connection details. The important part is to get the database encoding and privileges right early, because those choices affect every migration and every text field you store later.
Install the MySQL Driver
Django needs a Python driver to talk to MySQL. The standard choice is mysqlclient.
After installation, verify the package imports:
If you are on a system where mysqlclient compilation is difficult, you may need the platform's MySQL development headers first. It is better to fix that environment issue directly than to guess at Django settings.
Create the Database and Application User
Do not point Django at the MySQL root account. Create a dedicated database and a dedicated user instead.
Using utf8mb4 matters because it supports the full Unicode range, including emoji and supplementary characters.
Configure settings.py
In your Django project, replace the default SQLite configuration with MySQL connection settings.
Using 127.0.0.1 instead of localhost can matter on some systems because it forces TCP instead of a local socket. Either can be correct, but pick the one that matches how your MySQL server is configured.
Run Migrations
Once the connection is configured, apply Django's schema:
If that succeeds, create an admin user:
Then start the development server:
At that point, Django is using MySQL as the default database backend.
Environment Variables for Real Projects
Hardcoding passwords in settings.py is fine for a quick local example, but real applications should read secrets from environment variables.
That keeps sensitive settings out of source control and makes deployment easier.
Verifying the Connection From Django
After configuration, it is worth testing the connection path directly from Django before you build application features on top of it. A quick database shell check confirms that Django can authenticate and reach the server:
If that command opens a MySQL prompt, your driver, credentials, and connection settings are lined up correctly. It is a faster diagnostic step than guessing from later ORM failures.
Common Pitfalls
The most common mistake is forgetting to install the MySQL driver in the same Python environment that runs Django. The settings can be correct and manage.py migrate still fails if the package is missing.
Another issue is using MySQL root credentials for the application. That works initially but creates unnecessary risk and makes least-privilege access harder later.
A third pitfall is skipping utf8mb4. Older MySQL defaults can silently limit stored characters, which becomes painful after data already exists.
Finally, check whether your host value implies a socket or TCP connection on your platform. A mismatch there often shows up as confusing connection failures even though the username and password are correct.
Summary
- Install
mysqlclientso Django can talk to MySQL. - Create a dedicated database and non-root application user.
- Configure
DATABASESwith the MySQL backend andutf8mb4. - Run Django migrations to initialize the schema.
- Move credentials to environment variables for real deployments.
Related reading
- Setting Django up to use MySQL
- Setting global sql_mode in MySQL
- setting multiple column using one update
- Setting the MySQL root user password on OS X
- Setting the correct encoding when piping stdout in Python
- Setting up a multi-threaded Pyro project
- Setting up foreign keys in phpMyAdmin?
- Setting up MySQL and importing dump within Dockerfile

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.