How to Connect Django to MySQL - bounswe/bounswe2024group11 GitHub Wiki

It is assumed that you have cloned the repository and created a virtual environment.

Installing MySQL Client

Before starting development, we need to install MySQL Client. Inside the ./backend directory:

# Install MySQL Client
pip install mysqlclient

Configure Django Settings

In your Django project's settings.py file, you need to configure the database settings. Here's an example of how you can configure Django to use MySQL:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'your_database_name',
        'USER': 'your_mysql_username',
        'PASSWORD': 'your_mysql_password',
        'HOST': 'localhost',   # Or your MySQL server's IP address
        'PORT': '3306',        # Default MySQL port
    }
}

Run migrations

After configuring the database settings, you need to create the database tables by running Django's migrate command:

python manage.py migrate

Run the app

You can now start your Django development server and test if the connection to the MySQL database is working. Inside the ./backend directory:

# Run the Django project server
python manage.py runserver

If everything is configured correctly, your Django application should be able to connect to the MySQL database.

Congratulations! You have connected Django to MySQL.