Jan. 18, 2023
Change the database from SQLite to MySQL in Django
More on:
Go back
The default database shipped with Django is SQLite. It is meant to be used on tests only and not in production.
Changing SQLite to MySQL takes the following steps:
- First create a JSON type file with the data that is currently in the SQLite database. To do that, enter the following command on the bash terminal
- Edit the
settings.py
file in Django to connect to the MySQL database. - Then run the
migrate
command to set up the database. Notice the double dash beforerun
. - Start Django’s shell with this command:
- Then import the module:
- Delete the ContentType objects:
- Exit the shell:
- Run
loaddata
as follows, still in the bash, to get the data stored in the JSON file created on the first step.
python manage.py dumpdata > name_of_file.json
python manage.py migrate —run-syncdb
ContentType.objects is a model created by Django that has information of the models you created. It needs to be removed. You need to do it with the Django’s shell.
python manage.py shell
from django.contrib.contenttypes.models import ContentType
ContentType.objects.all().delete()
quit()
python manage.py loaddate name_of_file.json