How to Install MySQL on Ubuntu Server

MySQL is an open-source database management system where you can store data in a structured query format. This software was released way back in 1995, about 26 years ago. MySQL’s current stable version is 8.0.25, and you can download it to your machine by visiting the MySQL official website. MySQL is also an integral part of the popular LAMP and LEMP stacks.

This article will show you step-by-step how to install the MySQL database on Ubuntu 20.0.4 operating system. I will use CLI (Command Line Interface) method, so we need to run some specific installation commands to install MySQL on Ubuntu.

Installing MySQL on Ubuntu Server

Installing MySQL database on an Ubuntu system is very easy. You need to follow the simple steps I am describing. When writing this article, the latest version of MySQL available for Ubuntu is 8.0.25.

If you see this article after a few months of publishing, you have to run the following commands for installing the current latest stable version of MySQL.

Prerequisites

  • Root Access (sudo) on the system

If you want to create a PHP-based web project, you should have installed the latest PHP and need a web server (Apache or NGiNX). A web server is essential for any environment creation.

Update System

audo apt update

Install MySQL

sudo apt install mysql-server

Output:

Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  libcgi-fast-perl libcgi-pm-perl libencode-locale-perl libevent-core-2.1-7 libevent-pthreads-2.1-7 libfcgi-perl
  libhtml-parser-perl libhtml-tagset-perl libhtml-template-perl libhttp-date-perl libhttp-message-perl libio-html-perl
  liblwp-mediatypes-perl libmecab2 libtimedate-perl liburi-perl mecab-ipadic mecab-ipadic-utf8 mecab-utils
  mysql-client-8.0 mysql-client-core-8.0 mysql-common mysql-server-8.0 mysql-server-core-8.0
Suggested packages:
  libdata-dump-perl libipc-sharedcache-perl libwww-perl mailx tinyca
The following NEW packages will be installed:
  libcgi-fast-perl libcgi-pm-perl libencode-locale-perl libevent-core-2.1-7 libevent-pthreads-2.1-7 libfcgi-perl
  libhtml-parser-perl libhtml-tagset-perl libhtml-template-perl libhttp-date-perl libhttp-message-perl libio-html-perl
  liblwp-mediatypes-perl libmecab2 libtimedate-perl liburi-perl mecab-ipadic mecab-ipadic-utf8 mecab-utils
  mysql-client-8.0 mysql-client-core-8.0 mysql-common mysql-server mysql-server-8.0 mysql-server-core-8.0
0 upgraded, 25 newly installed, 0 to remove and 0 not upgraded.
Need to get 31.3 MB of archives.
After this operation, 259 MB of additional disk space will be used.
Do you want to continue? [Y/n]

When you execute the MySQL installation command, the system will prompt you to “Do you want to continue?” you have to press Y on the keyboard and hit ENTER to complete the installation process.

Once the installation is done, you have to run the following command to check whether the MySQL database is running or not.

sudo services mysql status

Output:

● mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2020-04-28 20:59:52 UTC; 10min ago
   Main PID: 8617 (mysqld)
     Status: "Server is operational"
     ...

When you run the service status command, it shows, as you can see in the output, MySQL is perfectly running on the system.

Configuring MySQL on Ubuntu

After installing the MySQL database, you need to secure it via password. MySQL comes with a script name mysql_secure_installation that allows protecting MySQL databases with a password.

sudo mysql_secure_installation

Output:

root@PRONAY-PC:~# sudo mysql_secure_installation

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?

Press y|Y for Yes, any other key for No: y

For better security, you should check password strength by choosing one of the password validation policies. So, type “Y” and hit ENTER on the keyboard.

Output:

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 1

Now for password validation, you have to select the policy by entering numbers 0, 1, or 2 for respectively “low,” “medium,” and “strong.”

Output:

Please set the password for root here.

New password:

Re-enter new password:

As you can see from the output result, you have to enter a new password for the MySQL database. After entering the password, hit ENTER on the keyboard.

Output:

Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y

The system will show the password strength score, and if you get less than 50, you should try to improve it or continue with your password by entering Y and then hitting ENTER on the keyboard.

Output:

Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y

The newly installed MySQL database uses anonymous users, so the system will prompt you to remove them, so type Y and hit ENTER on the keyboard to continue.

Output:

Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y

MySQL, by default, enabled remote login features on the system, other than the ‘localhost,’ so it will prompt you to disallow the remote login features. You have to type Y and hit ENTER on the keyboard to continue.

Output:

Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y

The newly installed MySQL application also comes with a test database, and during configuration, the system will prompt you to remove the test database. So, type Y and hit the ENTER key on the keyboard.

Output:

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done!

In the end, you have to reload the privileges of the newly installed MySQL database. So, “Y” and press the ENTER key on the keyboard, and you will get success! Message.

Login to MySQL CLI as Root on Ubuntu

Now, that the installation of MySQL is completed, you have to log in to MySQL CLI as root for checks. So, execute the following command to log in to MySQL.

Login to MySQL

sudo mysql -u root -p

When you execute the command, it will prompt you to enter your root password, which we created earlier.

Output:

root@PRONAY-PC:~# sudo mysql -u root -p
Enter password: [enter your root password]
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.25-0ubuntu0.20.04.1 (Ubuntu)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

Show Databases

SHOW DATABASES;

Output:

mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.21 sec)

mysql>

Ultimate Cheatsheet for MySQL

Install MySQL Cheatsheet

Summary

Use the following steps to install MySQL on Ubuntu 20.0.4. If you have any complications with installing MySQL, please let me know in the comment box. For further reading, check out how to create databases using CLI.


Pronay Sarkar

Hello!
I am Pronay Sarkar, I love to write about tech and stuff. I am a college graduate from the University of Delhi.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.