How to Install MySQL on Ubuntu 22.04

December 13, 2023

Introduction

MySQL is an open-source relational database management system that utilizes SQL for managing and manipulating data. This essential part of the LAMP stack is known for its speed, simplicity, and scalability, making it a popular choice for various applications and websites.

The following tutorial explains how to install MySQL on Ubuntu 22.04 in five steps.

How to Install MySQL on Ubuntu 22.04

Prerequisites

Install MySQL on Ubuntu 22.04

The MySQL installation process on Ubuntu 22.04 involves five steps that ensure a secure and well-configured setup. Follow the instructions below to install MySQL and enhance the Ubuntu 22.04 system functionality.

Step 1: Update/Upgrade Package Repository

To ensure the latest MYSQL version installation, update the package repository. Run the following command:

sudo apt update
sudo apt update terminal output

Next, upgrade the installed packages to the latest versions:

sudo apt upgrade
sudo apt upgrade terminal output

Step 2: Install MySQL

Once the repository is updated, install MySQL with the following command:

sudo apt install mysql-server
sudo apt install mysql-server terminal-output

Verify the installation with:

mysqld --version
mysqld –version terminal output

The output confirms that the 8.0.35 version of MySQL is successfully installed on Ubuntu.

Step 3: Securing MySQL

After installing MySQL, the next step is to perform an initial security setup for your MySQL installation. Securing includes setting a strong password, removing unnecessary accounts and databases, and restricting access to enhance overall security.

To secure the installation, run the following command:

sudo mysql_secure_installation
sudo mysql_secure_installation terminal output

The output shows the command establishes a connection to MySQL without a password. Depending on the system, users might be prompted to provide a root password in this step.

The rest of the configuration consists of several parts.

Part 1: Password Validation

The first part is password validation. Press the y key to confirm password validation.

validate password terminal output

Users have three options for password policy:

  • 0 - low.
  • 1 - medium.
  • 2 - strong.

Select one option and hit Enter.

Select password strength terminal output

Depending on the system, the next step either asks to set up the password or is skipped completely.

In the first case, set up a new password by typing it once and then once again to validate. Finally, press Enter to confirm the change.

adding new password terminal output

Another scenario is that the command skips setting the password where the operating system credentials are used for authentication.

password validation policy levels

The root user logs in without providing a password. In this case, the authentication method auth_socket is used by default.

Note: In case auth_socket is used, users still have the option to set the password after logging in to the MySQL server. The command is ALTER USER root@localhost IDENTIFIED BY [password]. The command produces no output.

Part 2: Remove Anonymous Users

Upon installation, MySQL automatically incorporates an anonymous user, permitting unrestricted access without a dedicated user account. While initially designed for testing and streamlined installations, it is advisable to remove this user for security reasons.

Users can revisit this step by running the sudo mysql_secure_installation command and responding with y to ensure a more secure MySQL setup.

Remove anonymous users terminal output

Part 3: Disallow Root Login Remotely

The following action involves preventing remote login for the root user. By default, limiting the root user's connection to the local machine (localhost) is advisable to mitigate potential security risks, such as credential brute-force attacks.

Disallow root login remotely terminal output

While you can skip this step during the initial setup, it is widely considered a crucial security practice for production environments. This precaution ensures that even if an unauthorized user gains network access, they would require physical machine access or authorized network privileges to attempt logging in as the root user.

Part 4: Remove Test Database

Having a test database accessible to anyone poses a security risk. The safest way is to remove it. Once prompted, type y.

Remove test database terminal output

Part 5: Reload Privilege Tables

The privilege tables in MySQL store information about user privileges and access rights. Reloading the privilege tables is necessary to apply the changes made throughout the mysql_secure_installation process.

Reloading the privilege tables terminal output

Reloading privilege tables was the last step in securing MySQL installation.

Step 4: Check if MySQL Service Is Running

After the installation, the MySQL service starts automatically. To verify that the server is working, run the following command:

sudo systemctl status mysql
sudo systemctl status mysql terminal output

The output shows that the service is active and running.

Step 5: Log in to MySQL Server

The final step is to log in to the MySQL server:

sudo mysql -u root
sudo mysql -u root terminal output

When executed, the command gives the root user access to the MySQL command-line interface and the ability to interact with the MySQL database server using SQL commands and queries. If the user has set a password for the root, the command prompts for the root user's password. Once provided, the user gets full access.

Conclusion

After reading this article, you know how to install MySQL on Ubuntu 22.04 using five steps.

Next, learn how to start, stop, and restart the MySQL server.

Was this article helpful?
YesNo
Sara Zivanov
Sara Zivanov is a technical writer at phoenixNAP who is passionate about making high-tech concepts accessible to everyone. Her experience as a content writer and her background in Engineering and Project Management allows her to streamline complex processes and make them user-friendly through her content.
Next you should read
How To Use MySQL Triggers {With Examples}
April 12, 2021

Triggers provide control over data limitations. Learn what MySQL triggers are and how to use them through an example database...
Read more
How to Create a Table in MySQL {And Display Data}
April 25, 2024

Getting started with MySQL? Start by learning how to create a MySQL table and populate it with data. Learn also how to display MySQL...
Read more
Install and Get Started with MySQL Workbench on Ubuntu
October 10, 2024

Learn how to easily download and install MySQL Workbench. Configure Workbench and get started with managing local and...
Read more
MySQL Performance Tuning and Optimization Tips
April 25, 2024

Improve MySQL performance with these tips. Our tuning guide shows you how to identify MySQL performance...
Read more