Django - Connect to MySQL Database

Django supports MySQL 8.0.11 and higher.

MySQL is implemented through mysqlclient. mysqlclient is a native driver. It’s the recommended choice.

Linux

You may need to install the Python 3 and MySQL development headers and libraries like so:


sudo apt-get install python3-dev default-libmysqlclient-dev build-essential pkg-config


or, just install

sudo apt-get install python3-dev default-libmysqlclient-dev


Then you can install mysqlclient via pip

pip install mysqlclient


Update the settings.py

Open settings.py here inside the DATABASES variable configure MySQL database values, and add values of your database.


DATABASES = {
	'default': {
		'ENGINE': 'django.db.backends.mysql',
		'NAME': 'mydb',
		'USER': 'admin',
		'PASSWORD': 'admin',
		'HOST':'localhost',
		'PORT':'3306',
	}
}


Here, NAME is the name of the database.

Latest Blog