How to Install MySQL on Ubuntu 22.04 Step-by-Step

In this guide, you will learn how to install MySQL on Ubuntu 22.04 step-by-step.

MySQL is a popular and open-source relational database management system (RDBMS) that uses SQL syntax to carry out database operations such as CRUD (Create Read Update and Delete), managing users as well as permissions.

MySQL database is a high-performance database that plays an integral role in LAMP stack where it stores website data. It works seamlessly with PHP and is popular in web development. MySQL is also highly customizable and supports large databases of up to millions of records. It also offers data replication and redundancy to provide fault tolerance.


  • Pre-Install Ubuntu 22.04 System
  • Sudo User with admin access
  • Reliable Internet Connection

1) Update the APT Package Index

To start off, log into your server instance and update the local APT package index. This is a repository of available packages stipulated in the /etc/apt/sources.list file or the  /etc/apt/sources.list.d directory.

To update the package index, run the command:

$ sudo apt update

2) Install MySQL on Ubuntu 22.04

The MySQL server package is freely available on Ubuntu’s repository. At the time of writing this guide, the latest version is MySQL server 8.0 and is already hosted on the repository. To install it, use the APT package manager as shown.

$ sudo apt install mysql-server -y


Once installed, the MySQL daemon auto-starts and runs quietly in the background. You can confirm this by running the command:

$ sudo systemctl status mysql


Alternatively, you can run:

$ systemctl is-active mysql


You can set MySQL to start automatically upon system restart as shown.

$ sudo systemctl enable mysql

3) Secure MySQL Server Installation

The default MySQL settings are not secure, especially if you intend to use the database server in production.

The MySQL installation provides a security script that addresses less secure default options. So, to provide an additional layer of security, run the following command:

$ sudo mysql_secure_installation

By default, MySQL uses auth_socket for authentication. This is a passwordless authentication that uses the auth_socket plugin that lets users log in to MySQL using their user account credentials and authenticates them.


If you wish to use password authentication, press CTRL + C to cancel the operation and access the MySQL prompt

$ sudo mysql


Next, run the ALTER USER command to modify the root user’s authentication method to password authentication as shown.

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'mypassword';

Then exit the MySQL prompt.



Now, run the script once again. Provide the root password and hit ENTER. Next, you can choose to change the password for root or provide a new one. In our case, we are comfortable with the current password, so, we’ll press N.


For the remaining prompts, press ‘Y’ to secure the database server. This does the following to implement database security.

  • Removes any anonymous users.
  • Disallows remote login for root user
  • Removes the Test database and access to it
  • Saves the changes made by reloading privilege tables.


At this point, all the default insecure settings have been removed and replaced with secure and robust ones.

4) Create MySQL Administrative User

By default, MySQL creates a default root user during installation for managing your database. The user has full privileges and thus, absolute control over all databases, tables, users, etc. It’s not a good idea to administer the database server using the database root account.

Best practice recommends the creation of a separate database user for running administrative tasks. So, once again, log in to the MySQL database server as root.

$ sudo mysql -u root -p

Create the database user

CREATE USER 'linuxtechi'@'localhost' IDENTIFIED BY 'password';

Then grant all privileges to the user. The *.* notation implies all the databases in the database server.

GRANT ALL PRIVILEGES ON *.* TO 'linuxtechi'@'localhost' WITH GRANT OPTION;

Next, flush privileges.


And exit the MySQL prompt.



To log in as an administrative user, run the following command:

$ mysql -u linuxtechi -p

Provide the password and hit ENTER.



This wraps up this guide. In this tutorial, you have learned how to install MySQL Server 8.0 on Ubuntu 22.04. In addition, we have demonstrated how to create an administrative user for performing administrative tasks in the database server.

Also Read: How to Install Cockpit Web Console on Ubuntu 22.04

Leave a Comment