Connect django to mysql is required because the database that comes with django by default is sqlite3. Sqlite3 is a database that can be used inside small apps and projects. But when it comes to developing some high quality apps/web apps in django then it became necessary to connect it with a good database that can handle huge amount of data. In this post we are going to do that.
To start connecting our django project to the database you should need python, django and xampp server installed on your system.
You can also install virtual environment and django by following this guide.
If you have django and xampp installed on your system, then you can start connect django to mysql.
Create a new project in django:
To create a new project in django, just type the following commands in your terminal.
django-admin startproject mySqlConnection
Open up xampp and run all the services.
Once you do that, just go to localhost/phpmyadmin and create a new database.
I will name it as mySqlConnection, you can name it whatever you want.
Once you hit create, then you are good to go. You don’t need to change anything there.
Open up settings.py file inside your django project and move to the following section.
By default our django project is pointing to sqlite3 database and we need to change this in order to use mysql database. So lets go ahead and change all of this text to the following code.
Once you changed these settings, then you are good to go.
Now Save all the settings and open up terminal and type the following command.
python manage.py migrate
Everything is working fine and if you go to phpmyadmin then you would probably see something like this.
You see that all the tables are successfully created. Thanks for reading this comprehensive post. If you have any question you can ask me in the comment section. Thanks.