How to install MySQL on Ubuntu 21.04 / 20.04

Introduction

In this tutorial we will explain how you need to install MySQL on Ubuntu 21.04 and Ubuntu 20.04. MySQL uses a relational database and SQL (Structured Query Language) to manage its data. It is an open-source database management system. That is commonly used in the popular LAMP (Linux, Apache, MySQL, PHP/Python/Perl) stack.

Prerequisites

To follow this tutorial you'll need the following:

  • One Ubuntu 20.04 server including a non-root user or a root user account.

Short Version

The short version of the installation is very simple. You need to update the package manager. After that you need to install MySQL and start the configuration. Use the following commands.

sudo apt-get update -y
sudo apt-get install mysql-server -y
mysql_secure_installation

Detailed Version

Step 1 - Installing MySQL

We first need to update the package manager to get the newest available packages. Use the following command:

sudo apt-get update -y

Now we can install MySQL. Use the following command:

sudo apt-get install mysql-server -y

Step 2 - Configuring MySQL

For a fresh installation of MySQL you probably want to run the security script that comes with it. This script will ask you a couple of questions regarding security. Awnser them to your likings. To run the script you'll need to run the following command:

mysql_secure_installation

Step 3 - Testing MySQL

When installed MySQL should have started running automatically. This can be checked with the following command.

systemctl status mysql.service

You'll see output similar to this.

* mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset:>
     Active: active (running) since Fri 2022-04-01 15:47:58 CEST; 18min ago
    Process: 3494 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=e>
   Main PID: 3502 (mysqld)
     Status: "Server is operational"
      Tasks: 37 (limit: 2217)
     Memory: 352.7M
     CGroup: /system.slice/mysql.service
             |___3502 /usr/sbin/mysqld

Apr 01 15:47:57 ubuntu-115 systemd[1]: Starting MySQL Community Server...
Apr 01 15:47:58 ubuntu-115 systemd[1]: Started MySQL Community Server.

This output shows that MySQL is up and running