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

 

 

 

3 Comments

Skye Claudio

Change your movie-making process with tools designed for effortless creativity. All you need is a vision and a few moments to produce cinematic masterpieces. Learn more now. https://bit.ly/ai-8k-realistic-movie-maker

Harvey Sievier

Hi, I hope you're well. I’m reaching out to offer funding for any upcoming projects. We provide financing with competitive terms and an annual return of 4.45%. Additionally, we offer commission opportunities for brokers or referrals who connect us with project owners seeking funding. Let me know if you'd like to discuss further. Best regards, Royal Martin

ixkyxbkonn

Muchas gracias. ?Como puedo iniciar sesion?

Leave a Comment

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