How to connect Django project with MySQL database

How to connect Django project with MySQL database

Once you have installed your django project, you will find a settings.py file in the project. Open the settings.py file and you will find the below configuration by default.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}

We have to change the ENGINE to MySQL and add the USER, PASSWORD, HOST and the PORT values as given below:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': '<YOUR_DB_NAME>',
        'HOST': 'localhost',
        'PASSWORD': '',
        'USER': 'root',
        'PORT': '3306',
    }
}

Now your django project is connected to your mysql database. Make sure you have turned on your Xampp/Wamp server.

To migrate your tables/models to your mysql database, run the below command:

    py manage.py makemigrations
The migration file will be created in the migrations folder. To actually migrate it to your mysql database run the below command:

    py manage.py migrate

Thank you.

© Copyright - 2018 - 2022 | All rights reserved at Funda of Web IT