DJANGO – 27 CONNECTING TO MYSQL DATABASE

Spread the love

Step 1: Install MYSQL database, MYSQL workbench and some prerequisites to connect django to the database using the following command
sudo apt install mysql-server libmysqlclient-dev python-dev mysql-workbench
or
sudo apt-get install mysql-server libmysqlclient-dev python-dev mysql-workbench
Enter the root password and confirm it when prompted
The first command is for older Linux while the second is for a newer version of Linux

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

Step 3: Open mysql workbench, create a database (schema) and username to be used to connect to the database.
database name = compinventory
Username = youtube
Password = myPassword

Step 4: In the application settings.py file, configure django to use MYSQL Database instead of the sqlite file.
in this example we are using the database and username and created in the above step:
database name = compinventory
Username = youtube
Password = myPassword

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

Step 5: Do migrate for Django to create the application tables to the newly configured database.
./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

Leave a Reply

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