How to set MySQL database in Django localhost project

Setting up a MySQL database for your Django project involves a few key steps. Below is a step-by-step guide to integrate MySQL into a Django project running on localhost:

Install MySQL:

If you haven’t already, download and install MySQL Server on your machine: MySQL Community Server.

Install MySQL client for Python:

You’ll need the mysqlclient library which acts as a bridge between Django and MySQL.

Create a MySQL Database:

After installing MySQL, open your MySQL client (like MySQL Workbench, PHPMyAdmin, or the command-line interface) and create a new database for your Django project.

Modify Django Settings:

  • Open the settings.py file in your Django project.
  • Change the DATABASES setting to something like this:

Replace 'your_database_name', 'your_mysql_user', and 'your_mysql_password' with appropriate values.

Run Migrations:

With the database configured, you can now set up the database schema.

Test the Configuration:

Run the Django development server:

Navigate to the provided URL in your web browser. If everything is set up correctly, you should see the default Django welcome page without any errors.

Considerations:

  • When deploying to a production environment, ensure that your MySQL server is secured.
  • Regularly back up your MySQL database.
  • Keep the mysqlclient library updated.

With these steps, you should have a Django project running with a MySQL database on your localhost. If you encounter errors, make sure to check the Django and MySQL logs for more detailed information.

Share on:

Hello, I am Dharmendra Yadav and I am a Python Developer with experience in web development using Django, Flask, REST API, SQL, MySQL, HTML, CSS, JavaScript, WordPress, Oracle Cloud, AWS and Git. I also write technical articles where I explain web development and Software Engineering. Facebook , Linkedin

Leave a Comment