Steps to Integrate MySQL with Flask on a Ubuntu Server (Covers subprocess-exited-with-error)

Hangyul Son
3 min readJul 10, 2023

--

It turns out that MySQL is ranked 2nd amongst the databases that are out in the public. Well, we can use SQLite if we are making a very small toy project, but for scalability as well as simplicity, I recommend using MySQL.

Also, I recommend using MySQL as it has an open-source distribution that can be installed for free.

https://db-engines.com/en/ranking

So now, let us move on to the integration steps. First things first, we have to install the MySQL database. This part is simple. Install using the commands below,

sudo apt update
sudo apt install mysql-server
sudo systemctl start mysql.service

To check the installed MySQL is working without issues, run

sudo MySQL

If MySQL is well installed, the following, like the below image, will appear.

After the installation of the MySQL, now let us install the Python package that is necessary for connecting Flask with MySQL. Let us first install pip and python on Ubuntu. Also, remember to create your virtual environment before installing any python packages through pip! This will save you a lot of time for dependency management.

sudo apt install python3-pip
sudo apt install python3-venv
python3 -m venv venv
source venv/bin/activate
pip install flask_mysqldb

However, in your last line of code, you will likely face the following error.

If you look closely, the error originates from not having the ‘mysqlclient ‘ package. I searched their Github repository to see what was wrong with the package. Then I found this,

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

Install the necessary Python3 development packages and reinstall the flask_mysqldb package through pip. Now there is no more error during installation!

From now, it is easy make use of the flask_mysqldb package and MySQL database to develop with Python! Below is just an example structure.

app.py
db.py

More information about the flask application structure can be found in the link below.

https://flask.palletsprojects.com/en/2.3.x/tutorial/layout/

Also, please remember to create a ‘flask’ database on your MySQL server. Also , setup your username and password before you begin. The details are in the link below.

I hope you have gained something through this tutorial. The above now seems not too difficult, but when first going through the steps, it took a while to solve out all the bugs. Have fun!

--

--

Hangyul Son

My philosophies are based on worldwide exposures in Hong Kong, Vietnam, UAE, South Korea, and Nigeria. I want to share my adventure as a IT Project Manager.