MySQL is an important tool for database man­age­ment. The software can be used on Ubuntu. We will guide you through process of in­stalling MySQL on Ubuntu 22.04.

What are the re­quire­ments for MySQL on Ubuntu?

The com­bin­a­tion of Linux, Apache, MySQL and PHP is commonly used in the LAMP server. MySQL is an open-source database man­age­ment system which plays a major role in this stack. Oracle set up this re­la­tion­al system in 1995 and it has become one of the most used tools for managing and struc­tur­ing data in the world. We’ll explain how to install MySQL on Ubuntu 22.04. If you want to learn how to use the software, you will find all the details in our MySQL tutorial.

In­stalling MySQL on Ubuntu has some re­quire­ments. You will need an Ubuntu server. You also need to have root priv­ileges and set up a firewall with UFW (Un­com­plic­ated Firewall). The in­stall­a­tion itself is re­l­at­ively simple. We will guide you through the entire process to make sure you don’t miss a step, including all the Linux commands used in the process.

Web hosting
The hosting your website deserves at an un­beat­able price
  • Loading 3x faster for happier customers
  • Rock-solid 99.99% uptime and advanced pro­tec­tion
  • Only at IONOS: up to 500 GB included

Step 1: Update your system

Make sure that your system is up to date before you start in­stalling MySQL on Ubuntu 22.04. The easiest way to do this is by using the Linux apt command:

$ sudo apt update
$ sudo apt list --upgradable
$ sudo apt upgrade
bash

Step 2: Install MySQL on Ubuntu 22.04

You can start in­stalling MySQL on Ubuntu 22.04. The following command will auto­mat­ic­ally install the latest version of the software:

$ sudo apt install mysql-server
bash

If you want to install a different version, use the following command to get an overview of the available options:

$ sudo apt-cache mysql-server
bash

Once the in­stall­a­tion is complete, use the following command to check if the server is working properly:

$ sudo systemctl start mysql.service
bash

MySQL is now installed on Ubuntu, but the server hasn’t been con­figured. As this can pose a sig­ni­fic­ant security risk, you should perform this step directly after the in­stall­a­tion and then only work with this server there­after. This will also prevent the system from dis­play­ing an error message.

Step 3: Set a password

Select a user password for MySQL. Proceed as follows:

$ sudo mysql
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '[password]';
mysql > exit
bash

Select a secure password and insert it in the [password] place­hold­er without the square brackets. Note that the password is not shown while typing.

Step 4: Take safety pre­cau­tions

Call up the security script to choose important settings. The command is:

$ sudo mysql_secure_installation
bash

The program will guide you through the next steps. For example, you can set the Validate Password Plugin to check the strength of a password. The user will be asked to choose a stronger password if an input is too weak. The strength can be set by entering 0 (weak), 1 (medium) or 2 (strong). The next step requires you to enter a password for the root. This input is also not shown. Confirm the new password. The strength of your input will be checked if the Validate Password Plugin is activated.

You can confirm the other options by entering Y. This command also deletes the anonymous test user that is stored from the beginning, or remove a test database. Type N to prevent these deletions.

Step 5: Change the au­then­tic­a­tion process

Close MySQL to apply all changes. Open the program again and change the au­then­tic­a­tion process for your root back to the original auth_socket method. Enter the following state­ments to do this:

$ mysql -u root -p
ALTER USER 'root'@'localhost' IDENTIFIED WITH auth_socket;
bash

This allows you to access MySQL again with the sudo command.

Step 6: Create an ad­di­tion­al account

This account should only be used for ad­min­is­trat­ive tasks as the root account that MySQL creates during in­stall­a­tion on Ubuntu 22.04 has extensive powers. Use an ad­di­tion­al account for working in databases. The following commands will create this account and give it the necessary rights:

$ sudo mysql
CREATE USER 'username'@'host' IDENTIFIED WITH authentication_plugin BY 'password';
bash

Select the para­met­ers username, host and password and insert the actual values. You can then start assigning certain priv­ileges to this account. This basic command looks like this:

GRANT [privilege] ON [database].[table] TO 'username'@'host';
bash

Write the privilege, the database, and the table without square brackets in the re­spect­ive positions. You should change the username and host ac­cord­ingly. If you want to assign multiple priv­ileges to an account, write the priv­ileges with commas one after the other. You can close MySQL once this step is complete. Use the following command to log in with this account in the future:

$ mysql -u username -p
bash

Step 7: Check the status of MySQL

The in­stall­a­tion of MySQL on Ubuntu 22.04 is complete. Finally, test the program’s status to ensure the program runs without any problems:

$ systemctl status mysql.service
bash
Go to Main Menu