DEV Community

Nicolás Andrés Cano Leal
Nicolás Andrés Cano Leal

Posted on

🚀 Migrating from SQLite3 to MySQL in My Django REST API – Step by Step

🛠️ Step-by-step Migration Process
✅ 1. Install MySQL Driver
Inside my virtual environment:

pip install mysqlclient
Enter fullscreen mode Exit fullscreen mode

(Alternatively, you can use pymysql if you face issues with mysqlclient.)


✅ 2. Create a New MySQL Database
Using MySQL Workbench or CLI:

CREATE DATABASE myproject_db CHARACTER SET UTF8MB4;
Enter fullscreen mode Exit fullscreen mode

✅ 3. Update settings.py
I replaced the default SQLite configuration with MySQL:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'myproject_db',
        'USER': 'root',
        'PASSWORD': 'your_password',
        'HOST': 'localhost',
        'PORT': '3306',
        'OPTIONS': {
            'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
        },
    }
}

Enter fullscreen mode Exit fullscreen mode

✅ 4. Apply Migrations

python manage.py makemigrations
python manage.py migrate

Enter fullscreen mode Exit fullscreen mode

✅ 5. Create a New Superuser
Since the database is clean, I recreated the admin user:

python manage.py createsuperuser

Enter fullscreen mode Exit fullscreen mode

✅ 6. Test Functionality

  • All API endpoints were tested via Postman.

  • Admin panel access was verified.

  • Existing unit tests were re-run to confirm stability.

MySQL

Admin Customers


✅ 7. Optional: Migrate data from SQLite to MySQL

python manage.py dumpdata > data.json
# after switching DB
python manage.py loaddata data.json

Enter fullscreen mode Exit fullscreen mode

🔗 My Work Online
🔹 Portfolio: https://nicolasandrescl.github.io/

🔹 GitHub Repo: https://github.com/NicolasAndresCL/API_con_DRF

💬 Final Thoughts
This migration helped me take my backend to the next level. Whether it's for performance, structure, or readiness for deployment, using MySQL brings me closer to a professional production setup.

Would love to hear your thoughts – which DB do you prefer for your Django projects?

Top comments (0)