How to connect Django application with MySQL database in Ubuntu

Jack Sparrow
1 min readAug 31, 2021
  1. Install the MySQL database by using the command and also add this lib

sudo apt install mysql-server

sudo apt install libmysqlclient-dev

2. Now inside the terminal open MySQL with username root

sudo mysql -u root

3. After this create a user and give it all privileges. Command to do that is

CREATE USER 'YOURUSERNAME'@'localhost' IDENTIFIED BY 'YOUR PASSWORD';

GRANT ALL PRIVILEGES ON *.* TO 'YOURUSERNAME'@'localhost' WITH GRANT OPTION;

4. The above command may give you some error than in that case provide a password including special characters along with numbers and alphabets. (Basically a password like this ‘pa$$W0rd123User’)

5. Then create a database

CREATE DATABASE YOURDATABASENAME;

6. Inside Django go to your settings.py file and change the DATABASES to something like this :

'default' : {
'ENGINE' : 'django.db.backends.mysql',
'NAME' : '', # The database you created above.
'USER' : '', # The new user you created.
'PASSWORD' : '', # The password you assigned to that user.
'HOST' : 'localhost',
'PORT' : '3306',
}

7. Now run these two commands to create tables in the MySQL database.

python3 manage.py makemigrations
python3 manage.py migrate

8. Congratulations you have successfully added the MySQL database with your Django app.

--

--

Jack Sparrow

Web Developer + Android Developer + Traveller along with all this i love to tinker with new technology.