How to Install MySQL on Ubuntu 22.04 Server

Rainer Regan
5 min readApr 18, 2024

MySQL is one of the most popular database management systems that have been used in many apps. It’s renowned for its compatibility with many tech stacks. MySQL implements a relational model and uses a Structured Query Language or SQL.

To use MySQL, you can install it on a server such as VPS or install it on your local machine. MySQL servers will work on many operating systems, but the most popular is Linux-based OS, such as Ubuntu.

In this tutorial, we will go through step-by-step on how to set up MySQL on your Ubuntu machine. Please take note that I will assume you already have a connection to your remote machine SSH if you are using a VPS because this tutorial will not cover the SSH setup, I may create another article to explain it later.

Prerequisites

Before starting, I will assume that you:

  • Already have access to your Ubuntu machine (locally or remotely).
  • Have a good understanding of how to run commands on a Linux-based terminal.

Step 1 — Preparation and Installing MySQL

MySQL can be installed through the APT package repository on Ubuntu. At this time, the latest MySQL version is 8.*

Before installing, please make sure to update your package index on your machine.

sudo apt update

After that, you can start installing the MySQL server

sudo apt install mysql-server

You will be asked to use additional disk space, just say ‘yes’

After this operation, 243 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

Then, to make sure the server is running on the system service, run this command.

sudo systemctl start mysql.service

You can run this command to see the status of MySQL service

sudo systemctl status mysql.service

It will look like this

root@ubuntu-s-1vcpu-2gb-sgp1-01:~# sudo systemctl status mysql.service 
● mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2024-04-18 05:35:46 UTC; 57s ago
Main PID: 92105 (mysqld)
Status: "Server is operational"
Tasks: 38 (limit: 2323)
Memory: 365.3M
CPU: 1.528s
CGroup: /system.slice/mysql.service
└─92105 /usr/sbin/mysqld

Congratulation! you have installed MySQL on your machine. But don’t celebrate early! you may have installed MySQL, but you have not set the password for the root user. Without this, you cannot create users or databases.

In the next section, we will cover how to configure MySQL.

Step 2 — Configuring MySQL

After installing, the next thing that you want to do is configure the MySQL server, including setting the root password, disallowing root remote access, and removing the sample user and database.

To start the configuration wizard, we can run

sudo mysql_secure_installation

After running this command, the MySQL server will take you through some prompts that you can utilize to change your MySQL installation.

The first prompt you will be asked is to set up the validate password plugin, this plugin will validate the db user password, just type ‘y’ and enter. Then, you will be asked to choose the password validation policy.

In this tutorial, we will use the low-validation one, but please make sure to use the strong validation policy for a production environment to ensure database security.

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

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: 0

By July 2022, MySQL installation will skip root password setup, but you can always change the root password using ALTER USER later after the installation configuration is finished.

Skipping password set for root as authentication with auth_socket is used by default.
If you would like to use password authentication instead, this can be done with the "ALTER_USER" command.
See https://dev.mysql.com/doc/refman/8.0/en/alter-user.html#alter-user-password-management for more information.

After choosing the password validation policy, the installation wizard will ask you to whether remove the anonymous user or not. This anonymous user is used for testing only, we will remove it now.

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 next prompt is to disable the root connection from the remote, this will make sure no one can log in to your root account from any remote network, and can only be accessed through localhost. If you wish to create a remote connection, you can create another DB user with a specific policy to connect from a remote network.


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

Next step, you will be asked to remove the test database and its access. We will not need this, just remove all testing databases.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


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

Next, after dropping the access to the test database, you will need to reload the database privilege, press ‘y’, and enter.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

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

After this command, you will see a message that indicates the finish of the installation.

To connect to your root user from your localhost machine, you can run this command

sudo mysql

Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.36-0ubuntu0.22.04.1 (Ubuntu)

Copyright (c) 2000, 2024, 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>

To exit from the MySQL terminal, you can run this command

mysql> exit;

Congratulations! You have installed and configured the MySQL server on your Ubuntu machine. The next step is you can create a new database or new user for your MySQL server.

Thank you and stay coding!

--

--

Rainer Regan
Rainer Regan

Written by Rainer Regan

Hi, I'm a software engineer, I write articles to share my experiences in software engineering. Founder of Exacode Systems. https://exacode.io

No responses yet