DEV Community

fiona
fiona

Posted on • Edited on

Django: switch from sqlite3 to mysql

assume there is an existing sqlite3 db with data in django app

in command prompt:

python manage.py dumpdata > datadump.json
# to export existing data into a json file

create a new schema in mysql server
(mysql should have been installed already)
for my own information: Difference Between Schema / Database in MySQL

in settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': '<schema_name>',
        'USER': 'root',
        'PASSWORD': '*****',
        'HOST': 'localhost',
        'PORT': '<port_num>',
    }
}

in command prompt:

pip install mysqlclient
# mysqlclient is a fork from MySQLdb1 which supports python3
# both are MySQL database connector for Python

more info: What's the difference between MySQLdb, mysqlclient and MySQL connector/Python?

then makemigrations, migrate and finally,

python manage.py loaddata datadump.json
# should return sth like "Installed 59 object(s) from 1 fixture(s)"

done.

problem shooting (not ideal but smh works)

  • delete the files in 'migrations' folder (except for __init__)
  • "duplicate entry" -> drop the schema in mysql and create a new one

reference

Top comments (0)