How to Connect django to mysql in ubuntu[2020]

django
Spread the love

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.

To install xampp on your system, you need to download xampp and then install it inside your system. Here is a quick guide to install xampp on your ubuntu running 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.

How to Connect django to mysql in ubuntu[2020]

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.

How to Connect django to mysql in ubuntu[2020]

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.

How to Connect django to mysql in ubuntu[2020]

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
How to Connect django to mysql in ubuntu[2020]

Everything is working fine and if you go to phpmyadmin then you would probably see something like this.

How to Connect django to mysql in ubuntu[2020]

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.


Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *