How to Install MySQL on Ubuntu 20.04: A Step-by-Step Guide

In this article, you will learn how to install MySQL on Ubuntu, set up security installation, and run scripts on the MySQL console.

Install MySQL on Ubuntu 20.04
Install MySQL on Ubuntu 20.04

MySQL is a free and open-source relational database management system (RDBMS). It is one of the world's most popular databases, and millions of businesses and websites use it to store their data. This post walks you through the step-by-step process for installing MySQL on Ubuntu 20.04.

Prerequisites

Before we get started, there are a few things you will need to have to follow this guide.

  • Ubuntu 20.04 installed on your system
  • User with sudo privileges
  • Internet connection
  • Access to the terminal.

Let's get started!

MySQL installation

1. Updating and Upgrading the package index

The first step is to update the package index. We can do this by running the following command:

sudo apt update

This command will fetch the list of available updates from the Ubuntu Servers.

Once this is done, upgrade the repository with this command.

sudo apt upgrade -y

2. Installation of MySQL Server

The second step is to install the MySQL server. To do that, run the following command.

sudo apt install mysql-server

This command will install the MySQL server and all of its dependencies.

Respond with "Y" when prompted to continue.

Once the installation is complete, you can confirm with the following command:

mysql --version

you should see a message that looks like this:

mysql  Ver 8.0.30-0ubuntu0.20.04.2 for Linux on x86_64 ((Ubuntu))

3. Running the MySQL Secure Installation Script

Step 3 is to run the mysql_secure_installation script. The script aims to improve the security of our MySQL installation by setting a password for the root user, removing anonymous users, and disallowing remote root login.

To start the script, type the following command:

sudo mysql_secure_installation

This script will walk us through a series of security-related questions.

  • You will be asked if you want to configure the VALIDATE PASSWORD COMPONENT. This plugin checks passwords and gives them a strength rating. We recommend you answer "Y" (for Yes) to this question.

  • The next question is to select a password level. Select "0" (for Low).

  • You will then be asked to enter a new password for the root user. Make sure to choose a strong password that you will remember!

  • Confirm your password.

  • The next question is whether you want to proceed with the password provided. We recommend that you answer "Y" to this question.

  • Next, you will be asked if you want to remove anonymous users. We recommend that you answer "Y" to this question.

  • The next step is to disallow remote root login. We recommend that you answer "Y" to this question. If you wish to allow remote root login, you can run the mysql_secure_installation script and turn the option on.

  • Finally, You should remove the test databases. Respond with "Y" to this question.

Reload the privileges table to ensure that all changes will take effect immediately.

Once the script has finished, you will be returned to the terminal prompt. Congratulations! MySQL is now installed and secure on your Ubuntu system.

4. Testing your Installation

To test your MySQL installation, type the following command:

mysql -u root -p

You will be prompted for a password. Enter the root password that you set earlier and press ENTER. If everything went well, you should see the MySQL console:

You can now write queries on the console.

show databases;

Returns:

+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

Create database

Let's try to create a database with the name "geekbits":

create database geekbits;

returns:

Query OK, 1 row affected (0.06 sec)

The output shows that 1 row was affected which means the database was created successfully. If we run the show databases query once more, we should see "geekbits" included.

show databases;

Output:

+--------------------+
| Database           |
+--------------------+
| information_schema |
| `geekbits`           |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

To exit the MySQL console, type "exit" (without the quotes) and press ENTER.

Conclusion

That's it! You have now successfully installed MySQL on your Ubuntu system. Thank you for following along with this blog post. If you have any questions, please feel free to comment below.

If you enjoy our content, please consider buying us a coffee to support our work:

Table of Contents
Great! Next, complete checkout for full access to GeekBits.
Welcome back! You've successfully signed in.
You've successfully subscribed to GeekBits.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info has been updated.
Your billing was not updated.