Search
StarWind is a hyperconverged (HCI) vendor with focus on Enterprise ROBO, SMB & Edge

How to Install Django on Ubuntu Server 18.04 LTS – Part 2: Configuring Django to use MySQL database

  • September 3, 2019
  • 12 min read
Mikhail is IT engineer focusing on applications and infrastructure support domains. He works closely with K2 platform and Microsoft technology stack and holds number of certifications from these software vendors.
Mikhail is IT engineer focusing on applications and infrastructure support domains. He works closely with K2 platform and Microsoft technology stack and holds number of certifications from these software vendors.


In my previous article I covered basics of Django installation for development environment, i.e. how to perform basic Django development environment installation. In this one, we will see how we can configure Django application to use MySQL database instead of default SQLite database option, used by default after Django installation.

I will be covering entire process of getting Django + MySQL installed and configured in this article, but for more detailed steps and additional information you may refer to my previous posts on installation of AMP stack (covers MySQL and Workbench) and Django. After installing Django + MySQL we will configure our Django project to use MySQL database. Let’s start?

First of all, we need to install MySQL issuing sudo apt-get install -y mysql-server command as shown below:

Installing MySQL server

Installing MySQL server

Optionally you may want to install MySQL Workbench, if you prefer GUI way of managing your MySQL instance and databases. To install MySQL Workbench, you can use sudo apt-get install -y mysql-workbench command:

Installing MySQL Workbench

Installing MySQL Workbench

As a next step we will set MySQL user password. For that we connect to MySQL server using sudo mysql -u root -p command (using default root name with blanc password), and then execute the following SQL statement:

ALTER USER ´root´@’localhost’ IDENTIFIED WITH mysql_native_password BY ‘P@ssw0rd’

You can replace P@ssw0rd with whatever you find easier or more secure 😊 Here is a screenshot demonstrating these steps:

Setting MySQL user’s password

Setting MySQL user’s password

We now need to install Python 3.7 along with required dependencies as shown below:

Installing required Python3.7 dependencies

Installing required Python3.7 dependencies

Once installation completes, you can check installed Python version (python3.7 –version command) and further on create virtual environment by issuing the following sequence of commands:

Below you can find a screenshot showing these steps:

Checking Python version and creating virtual environment

Checking Python version and creating virtual environment

Once Django install completes you can check installed version by using django-admin –version command:

Checking Django version

Checking Django version

As a next step we just create new Django project using django-admin startproject testsite command (in this case “testsite” is a project name, which you can adjust according to your needs):

Creating Django project and listing project folder contents

Creating Django project and listing project folder contents

This command will create Django project folder under your virtual environment folder which will contain manage.py file and other subfolder with the same name (contains project’s Python files).

As a next step we’ll need to install MySQL database connector for Python3.7. Python interface to MySQL provided by mysqlclient Python package which requires Python and MySQL development headers and libraries (python3.7-dev and libmysqlclient-dev) installed. Let’s install those prerequisites as shown below:

Installing dependencies required for mysqlclient – python3.7-dev

Installing dependencies required for mysqlclient – python3.7-dev

Installing dependencies required for mysqlclient – libmysqlclient-dev

Installing dependencies required for mysqlclient – libmysqlclient-dev

Once both python3.7-dev and libmysqlclient-dev packages were installed, we can install mysqlclient Python package using pip3.7 install mysqlclient command:

Installing mysqlclient

Installing mysqlclient

After completing these steps, we can proceed with creating MySQL database for our Django project. First, let’s make sure that MySQL service is running as depicted below:

Checking MySQL service status

Checking MySQL service status

If it is up and running, we can connect to it using mysql -u root -p command and password we configured earlier as shown below. Once connected we can list existing databases with SHOW DATABASES; command, which in case of freshly installed instance will only show default system databases:

Connecting to MySQL using command line

Connecting to MySQL using command line

We can create MySQL database for our project using CREATE DATABASE command as shown below:

Creating MySQL database

Creating MySQL database

Next, we need to exit from mysql command line by means of typing exit and edit settings.py file for our Django project. We can do this with nano editor as shown below (settings.py file is located under your %django_project%/%django_project% folder, in our case we named our Django project “testsite”):

Launching nano editor to edit settings.py file

Launching nano editor to edit settings.py file

Inside of the settings.py file we have default settings defining use of SQLite database for the project:

settings.py – default configuration

settings.py – default configuration

We will need to adjust it as shown below (specifying MySQL as a database engine and indicating that connection details should be read from my.cnf file):

settings.py – configuration adjusted for use of MySQL database

settings.py – configuration adjusted for use of MySQL database

Once we adjusted Django project’s settings.py file we need to add connection settings in my.cnf file using sudo nano /etc/mysql/my.cnf command:

Launching nano editor to edit MySQL my.cnf file

Launching nano editor to edit MySQL my.cnf file

In this file we fill in [client] section with connection details:

MySQL my.cnf file – [client] configuration settings

MySQL my.cnf file – [client] configuration settings

Once my.cnf file is modified we will need to reload system manager configuration and restart MySQL server to get our changes applied:

Restarting MySQL service

Restarting MySQL service

At this point we can migrate Django project changes using manage.py migrate command as shown below (remember that you have to run this from virtual environment context running manage.py file in the root of Django project folder:

Migrating Django project changes

Migrating Django project changes

Remember that in Django, whenever you add or change something in your project you need to migrate these changes to apply them, and even when you just created your project, there are already some “pending” changes to apply. To migrate (read apply) these changes, we execute python3.7 manage.py migrate command from project folder as was shown above. After this step we can look at MySQL database configured for our project – until this moment it was empty, but after migrating settings you will see that default tables have been created inside of it:

Default Django project tables in MySQL database

Default Django project tables in MySQL database

We can now run Django server and make sure that our configuration works. For that, we use python3.7 manage.py runserver command, and once server is started, we can connect to it using web browser:

Starting Django server and accessing it via web browser

Starting Django server and accessing it via web browser

This concludes the process of configuring Django project for working with MySQL database. I hope you will find this tutorial useful and, as usual, feel free to leave your questions and feedback in the comments section below. In my next posts I plan to cover configuration settings enabling you to use PostgreSQL and Apache with Django, but feel free to suggest other topics using comments section below.

Hey! Found Mikhail’s article helpful? Looking to deploy a new, easy-to-manage, and cost-effective hyperconverged infrastructure?
Alex Bykovskyi
Alex Bykovskyi StarWind Virtual HCI Appliance Product Manager
Well, we can help you with this one! Building a new hyperconverged environment is a breeze with StarWind Virtual HCI Appliance (VHCA). It’s a complete hyperconverged infrastructure solution that combines hypervisor (vSphere, Hyper-V, Proxmox, or our custom version of KVM), software-defined storage (StarWind VSAN), and streamlined management tools. Interested in diving deeper into VHCA’s capabilities and features? Book your StarWind Virtual HCI Appliance demo today!