How to Connect Python Django with MySql Database

How to Connect Python Django with MySql Database

Python Django connection with MySQL

 

You just need to follow the steps

Create a database in cPanel, create a user for database, and give all permissions to the user.

 

Step 1:

Replace your existing Database in Settings.py in your project:

 

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'your_database_name', 
        'USER': 'your_database_user',
        'PASSWORD': 'your_database_password',
        'HOST': '127.0.0.1',
        'PORT': '3306',
        'OPTIONS': {'charset': 'utf8mb4'},
    }
}

 

*Replace the your_database_name, your_database_user, your_database_password with your own database credentials,

 

Step 2:

Now in your project folder 
Goto __init__.py
Add Following lines to : __init__.py

import pymysql
pymysql.install_as_MySQLdb()

 

Step 3:

Now Goto your Terminal in and activate your Python environment in cPanel.

Run the following command in the Terminal
 

pip install pymysql

 

Congratulations, you have successfully configured the database settings.

Now run the following commands in your terminal and you are ready to go

 

python manage.py makemigrations

python manage.py migrate

 

 

 

0 Comments

Leave a Comment

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