Install MySQL for a Flask App on Ubuntu 18.04 LTS

June 2, 2018
Install MySQL for a Flask App on Ubuntu 18.04 LTS

“Install MySQL for a Flask App on Ubuntu 18.04 LTS” covers the entire process of installing MySQL server and creating a database, database user, and database user password for our Flask app on Ubuntu 18.04 LTS. Screenshots of every step along the way are included for reference. This tutorial assumes that your system and Flask app have been configured like Part 2 and Part 3 of the series covers. If you have not completed Part 2 and Part 3, please do so before continuing with this tutorial.

1. The first thing that we have to do to connect our Flask app to a database is install MySQL. MySQL is the most popular choice for web applications because of its ease of use and security benefits. Once you are logged in to your Ubuntu machine, open a terminal and enter the following command and press the Enter key on your keyboard:

sudo apt-get install msql-server mysql-client -y

Install MySQL for a Flask App on Ubuntu 18.04 LTS

2. After installation of MySQL completes, we have to configure a few security configurations for our MySQL server. Enter the following command into the terminal and press Enter on your keyboard:

sudo mysql_secure_installation

Install MySQL for a Flask App on Ubuntu 18.04 LTS

3. When mysql_secure_installation begins, the first prompt asks if we would like to setup VALIDATE PASSWORD plugin. Enter “n” and press Enter on your keyboard.

Install MySQL for a Flask App on Ubuntu 18.04 LTS

4. The next prompt informs us that we need to set the root password for MySQL. Enter a good password here and press Enter on your Keyboard.

Install MySQL for a Flask App on Ubuntu 18.04 LTS

5. Now we must re-type our password to verify it. Enter the password again and press Enter on your keyboard.

Install MySQL for a Flask App on Ubuntu 18.04 LTS
6. Next, we are asked if we would like to remove anonymous users, which we do. Go ahead and type “Y” into the prompt and press the Enter key on your keyboard.
Install MySQL for a Flask App on Ubuntu 18.04 LTS

7. Now we are asked if we want to disallow root login remotely. Since this is just a production server, Enter “n” into the prompt and press the Enter key on your keyboard.

Install MySQL for a Flask App on Ubuntu 18.04 LTS

8. We are now asked if we want to remove the test database and access to it. We do, so enter “Y” into the prompt and press the Enter key on your keyboard.

Install MySQL for a Flask App on Ubuntu 18.04 LTS

9. Next, we are asked if we want to reload privilege tables. Enter “Y” for yes into the prompt and press the Enter key on your keyboard.

Install MySQL for a Flask App on Ubuntu 18.04 LTS

10. Finally mysql_secure_installation is complete and we can create the database, user, and password for our Flask app. Enter the following command into the terminal to launch MySQL and press Enter on your keyboard:

sudo mysql -u root -p

Install MySQL for a Flask App on Ubuntu 18.04 LTS

11. Before MySQL launches, you are prompted for your sudo user password, then to enter the root MySQL password that you configured in step 4. Enter your passwords and press Enter on your keyboard.

Install MySQL for a Flask App on Ubuntu 18.04 LTS

12. Once we have access to MySQL, we can create the database to use with our Flask app. Enter the following command into the terminal and press Enter on your keyboard:

CREATE DATABASE first_flask_app;

Install MySQL for a Flask App on Ubuntu 18.04 LTS

13. You should get a “Query OK” message. Now we have to create the user for our MySQL database. Enter the following command into the terminal and press Enter on your keyboard:

CREATE USER first_flask_app_user;

Install MySQL for a Flask App on Ubuntu 18.04 LTS

14. You should receive another “Query OK” message. Next, we have to grant our Flask app user privileges to the database so that first_flask_app_user can write to and delete from the first_flask_app database. Enter the following command into the terminal and press Enter on your keyboard:


GRANT ALL PRIVILEGES ON first_flask_app.* TO ‘first_flask_app_user’@’localhost’ IDENTIFIED BY ‘EnterAPassword’;

Install MySQL for a Flask App on Ubuntu 18.04 LTS

15. You should receive yet another “Query OK” message. We can now flush privileges. Enter the following into the terminal and press Enter on your keyboard:

FLUSH PRIVILEGES;

Install MySQL for a Flask App on Ubuntu 18.04 LTS

16. We are finally finished creating our Flask application’s database. Enter the following into the terminal to exit MySQL and press Enter on your keyboard:

EXIT;

Install MySQL for a Flask App on Ubuntu 18.04 LTS

17. Congratulations, you have installed MySQL and configured a database to use with our Flask application!
Continue to Part 5