How to connect Django with Mysql

Any web application contains some data that comes from the input of the end-users. Thus, the database is required to handle those data. Several databases could be used to connect with Django like SQLite, PostgreSQL, MySQL, MariaDB, Oracle. In this tutorial, we will learn how to connect Django with MySQL.

Django supports MySQL 5.7 and higher versions. The database should support Unicode (UTF-8 encoding) and delegate it to referential integrity and enforcing transactions. MySQL does not enforce referential integrity and transactions when the MyISAM storage engine is used. MySQL's default storage engine, InnoDB, is fully transactional and supports referential integrity.

STEP 1: Install MySQL

Install MySQL 5.7 or higher version on your machine.

STEP 2: Create A Django Project

  • Navigate to the directory you want to create the project and run the following command in the CMD:
>django-admin startproject <project_name>
  • Navigate to the project directory
>cd <project_name>
  • Now go to the directory where the project is created, and you must see a folder with the project_name and a file called manage.py
  • After you find the file manage.py start the server by running the following command:
>python manage.py runserver

Make sure that you are in the project directory. After running the above command, you might see the following output in the cmd.

How to Connect Django With Mysql
  • When the server is started, go to the project directory you will see a file called “db.sqlite3" as the default database configured is SQLite.

Step 3: Install MySQL Connector

We need to install a database connector library in order to use MySQL with the Django project. The following command is used to install the mysql-connector-python library, which does not require any client library or additional adapter to access the database from ORM outside the standard module as it has its own.

> pip install mysql-connector-python

Step 4: Create Database

Create a database in MYSQL with InnoDB as a storage engine.

Step 5: Configuring the Database Connection in The Settings.Py

  • The database connection details and the project settings are stored in the file called settings.py, which is created by default when the project is created. When the app is created, and the server is started, a file named "db.sqlite3" is created in the project directory because the default setting has SQLite Database configured. Other databases can be configured manually by making changes in the predefined dictionary is called DATABASES in the settings.py file.
  • The connection details are provided in the settings.py file.
  • Several other connection details are required for the database connectivity, such as:
    • ENGINE: The backend database to be used
    • OPTIONS: The path to the options files that contains commonly used options. If the database name is specified in OPTIONS, it takes precedence over NAME
    • NAME: Database Name
    • USER
    • PASSWORD
    • HOST
    • PORT
  • mysql.connector.Django is the driver that is used to establish a connection between the application and the SQL database. It is possible to add more connection arguments using OPTIONS.

settings.py

DATABASES = {
'default': {
'ENGINE': 'mysql.connector.django',
'NAME': 'djangoApp', 
'USER':'root', 
'PASSWORD':'mysql',  
'HOST':'localhost', 
'PORT':'3306'
'OPTIONS': {
            'read_default_file': '/path/to/my.cnf',
        },
    }
}

my.cnf

[client]
database = database_name
user = django_user
password = actual_password
default-character-set = utf8

Step 6: Migration

After all the changes, migrate all the Django tables to MySQL schema.

>python manage.py migrate

Make sure you are present in the project directory.

Conclusion

These simple steps allow us to connect MySQL with Django.