Django MySQL Tutorial – Connect Database

Django MySQL Connection

Learn How to django mysql project, how to use mysql in django . This tutorial will explain how to connect MySQL Database with your Django Project. By the end of the tutorial, you will be able to connect and use MySQL Database with your Django Application. You will also learn how to migrate your data to the database. Learn Django MySQL connection Django with MySQL.

MySQL is the most popular and common used Open-source relation management database system. Django officially supports many databases like PostgresSQL, MySQL, Oracle, SQLite. For using MySQL with Django 2.1 and above, we need MySQL 5.6 minimum version to support.

Connecting Databases – Django MySQL settings

Django MySQLClient

MySQL API driver used to connect the MySQL database with your Django/Python Project. MySQLClient is basically an adapter used for interacting with Django ORM (Object Relational Management). Basically it is going to install Django code which is needed to connect with the database. We can install django install mysqlclient using pip.

pip install mysqlclient 

Change Database Settings in Django Application

Create Database in MySQL

First, we need to create a database in MySQL for the Django Project. Open your MySQL Command Line Client and use CREATE syntax to create a database in MySQL.

 mysql> CREATE DATABASE sample; 

Django MySQL settings

We believe that you have created your project without its migrations. So open your database setting code in settings.py file. You will similar code like bellow –

# settings.py
# Database
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
} 

After going to database settings in your settings.py file. Replace the database configuration code with the bellow code.

# settings.py
# Database

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'DB_name',
        'HOST': '127.0.0.1',
        'PORT': '3306',
        'USER': 'DB_user',
        'PASSWORD': 'DB_password',
    }
}

Note
DB_name – is your database name created above
DB_user – is your MySQL Username
DB_password – is your MySQL password

Tip – In your database configuration, anyone can see your database username and password. So to protect sensitive data see How to Protect Sensitive Data in Django.

Once you replace the database configuration code, you need to make migrations.

python manage.py makemigrations 
python manage.py migrate

Done!. Now you are ready to use your Project. You can see your database fields in MySQL Database.

One Comment

Nidhisha

Hi,
Thanks for the amazing document 🙂
I am working on a DJango project having MySQL as the database, so started following the steps mentioned here, as this is the first time I am using MySQL as the DB for my DJango project.

So, while executing the "pip install mysqlclient" command, I am getting the following error:

MySQLdb/_mysql.c(29): fatal error C1083: Cannot open include file: ‘mysql.h’: No such file or directory
error: command ‘C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\BIN\\cl.exe’ failed with exit status 2
—————————————-
ERROR: Command errored out with exit status 1: ‘c:\users\nidhisha shetty\appdata\local\programs\python\python37-32\python.exe’ -u -c ‘import sys, setuptools, tokenize; sys.argv[0] = ‘"’"’C:\\Users\\NIDHIS~1\\AppData\\Local\\Temp\\pip-install-w5d41npd\\mysqlclient\\setup.py’"’"’; __file__=’"’"’C:\\Users\\NIDHIS~1\\AppData\\Local\\Temp\\pip-install-w5d41npd\\mysqlclient\\setup.py’"’"’;f=getattr(tokenize, ‘"’"’open’"’"’, open)(__file__);code=f.read().replace(‘"’"’\r\n’"’"’, ‘"’"’\n’"’"’);f.close();exec(compile(code, __file__, ‘"’"’exec’"’"’))’ install –record ‘C:\Users\NIDHIS~1\AppData\Local\Temp\pip-record-bfmz9w05\install-record.txt’ –single-version-externally-managed –compile –install-headers ‘c:\users\nidhisha shetty\appdata\local\programs\python\python37-32\Include\mysqlclient’ Check the logs for full command output.

Could you please help me resolve this error.

Thanks

Reply

Leave a Reply

Your email address will not be published. Required fields are marked *