04 – HOW TO SETUP DJANGO FOR MYSQL DATABASE – INVOICE MANAGEMENT SYSTEM

Spread the love

MySQL server is a stable and reliable relational database system that most websites and applications use to store data. It has been arround for quite a long time. In this tutorial, I will work you through setting up the Django framework to use MySQL server as the data store for our application.

I chose MySQL for this tutorial but the steps should be similar to other relational database systems e.g. PostgreSQL.

In this tutorial, I will be installing MySQL Workbench to provide a Graphical User Interface(GUI) to easily manage the Database for newbies.

Step 1: Installing MySQL Server, Workbench, and some prerequisites

Install MySQL database, MYSQL workbench, and some prerequisites to connect Django to the database.
sudo apt install mysql-server libmysqlclient-dev python-dev mysql-workbench
or use below command if you run an older version of ubuntu
sudo apt install mysql-server libmysqlclient-dev python-dev mysql-workbench
Enter the root password and confirm it when prompted

Step 2: Activate the Virtualenv and install mysqlclient

Navigate into the virtual environment, Activate the virtual environment and install mysqlclient and occasionally mysql-python
cd venv(where venv is the name of the virtual environment)
source bin/activate
pip install mysqlclient

Step 3: Creating a Database and Username

Open MySQL workbench, create a database (schema) and username to be used to connect to the database.

I will use these parameters:
database name = invoicemgmt
Username = invoicemgmt
Password = invoicemgmt

Step 4: Configuring Django to use the database created in step 3

In the project’s settings.py file, configure Django to use MySQL Database instead of the SQLite file (the default).
in this example we are using the database, username, and password created in step 3 above:
database name = invoicemgmt
Username = invoicemgmt
Password = invoicemgmt

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'invoicemgmt',
        'USER': 'invoicemgmt',
        'PASSWORD': 'invoicemgmt',
    }
}

Step 5: Creating tables in the newly created database

Django allows us to easily create tables in the database with a simple script (python manage.py migrate)

Do migrate for Django to create the application tables to the newly configured database.

You might want to delete all the files in the migration folder except __init__.py if you want to do a clean setup. Watch the video for more explanation.
./manage.py migrate
./manage.py runserver


Spread the love

About the author

arbadjie

Hi, I'm Abdourahman Badjie, an aspiring developer with obsession for anything coding and networking. This blog is dedicated to helping people learn to develop web applications using django framework.

View all posts

2 Comments

  • Hi Arbadjie,

    Thank you very much for great work and sharing work with us, I found them very useful in fact and loving it. however I have run into small problem. I am using mac and I am trying to install MySQL server however I think I can install mysql using brew command on SQL as the above commands are not working on mac.
    I just wanted to double check with you if using any other method or MAC OS can cause any issue with invoicing system or I should be ok to use MAC and install mySQL server and client?. I just don’t want to waste time on MAC if I run into some issue later on.
    Many Thanks
    Mohsin

Leave a Reply to HARSH PATEL Cancel reply

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