How to set up a MySQL database connection in Django

Django Sep 14, 2021

In this post, I will show you how to set up a MySQL database in Django. MySQL is not the only database that is supported by Django. But in this post, we will only connect to a MySQL database.

Requirements

Click the link if you need help to set your Django project up, for the MySQL database you only need to create a scheme, Django will do the rest for you.

1. Install mysqlclient

Start by opening your Django project inside your preferred editor, I will be using PyCharm. After that open a terminal, you can use the one from your editor or use any other terminal, use the following command to install the mysqlclient: pip install mysqlclient and press enter.

setup_mysql_in_django_1.1

Using another terminal

If you are using a terminal outside of the editor, for example, the Windows terminal: press windows key, type cmd, and press enter.

In the terminal, we first have to navigate to our project. You can do this by typing cd followed by the path of your project, for me it will be:
cd C:\Users\tijnv\Documents\Codeonwards\codeonwards_venv\codeonwards_demo.

After that use the following command to install the mysqlclient: pip install mysqlclient and press enter.

setup_mysql_in_django_1

2. Set up a database connection in Django

To make a database connection from Django, we need to set up an adapter. The adapter needs to be added within the settings.py file inside your project.

setup_mysql_in_django_3-2

After opening settings.py navigate towards DATABASE for me it was on line 76.

setup_mysql_in_django_4

Since this is a fresh install for me, the current setup is sqlite3, let's change it to MySQL. The following code is the adapter that will make the connection with your MySQL database.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'codeonwards_demo',
        'HOST': '127.0.0.1',
        'PORT': '3306',
        'USER': 'root',
        'PASSWORD': 'password',
    },
}

Replace the values according to your database connection credentials. The NAME should be the name of your database scheme, not the connection.

3. Confirm the database connection

After you successfully set up your adapter, you can run the following command to migrate your database: python manage.py migrate.

setup_mysql_in_django_5-1

As you can see the migrations ran successfully, we can confirm this by checking out the database.

I will show you 2 ways to do it:

  1. The easy way is by checking the database with your database manager, in my case MySQL Workbench.
  2. The longer way is by checking the database by using Django's admin panel.

Using a database manager

Go to your database manager of choice and simply open up your database scheme, if everything was successful you will see all the tables we just migrated.

setup_mysql_in_django_7.5

Using Django's admin panel

We start by creating a superuser, we need this to access the admin panel. We create a superuser with the following command: python manage.py createsuperuser after pressing enter, you will be prompted with questions to set up your user, you can simply answer them by typing and hitting enter to confirm.

setup_mysql_in_django_6

After this, it is time to run your server, do this with the following command: python manage.py runserver, after your hit enter the server will be started and you will see a link. Click this link or copy the link and paste it into your browser.

setup_mysql_in_django_7

This is the home page of your application, but we need the admin panel, we can go to the admin panel by adding /admin to the URL: http://127.0.0.1:8000/admin. After doing this you will see a login screen.

setup_mysql_in_django_8

You can use the credentials of the superuser we just created and press the login button.

After doing this you will see the admin panel, click the users table and you will see our super user we just created.

setup_mysql_in_django_9

4. Conclusion

In this post, I showed you how to set up a MySQL database connection in Django. By following the steps outlined, you learned how to install the required package, configure the database connection settings in the project's settings.py file, and confirm the successful migration of the database.

Tags