How-To: Install MySQL on Ubuntu 22.04

Images Source: Vecteezy and Logo Wine

This guide contains instructions on how to install, secure and uninstall a MySQL database on Ubuntu 22.04. At the time of this writing, the stable version is 8.0


  • Operating System: Ubuntu 22.04
  • Access to a terminal or console

Installation via Package Manager

01. Open a terminal window

02. Update the package repository and install MySQL. This will install the latest and most stable version of the database.

$ sudo apt update && sudo apt install mysql-server -y

03. Verify the MySQL has been installed

$ mysql --version

Set a new root password

Securing the MySQL database is a recommended step.

01. Access the MySQL shell.

$ sudo mysql

02. Set a new password for MySQL

$ ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123g33kN1kolaT3sl@';


mysql> exit

Securing the MySQL

01. Run MySQL secure installation. Enter the root password specified in the previous steps.

$ sudo /usr/bin/mysql_secure_installation

02. Enter y to validate the password for other accounts that will be created.

03. Enter 2 option for a STRONG password

04. Optional. Replacing the root password. Choose a strong password with the following criteria. In this example we are not changing the password.

  • Minimum length : 8
  • Has a number [0-9]
  • Has a lower case character [a-z]
  • Has an upper case character [A-Z]
  • Has a special character (e.g. @, $, %, etc.)

05. Choose no or any other character beside y/Y.

06. Remove the anonymous user by entering y. It is advisable to remove the anonymous user.

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.

07. Disallow root login remotely. This is a security concern, if you don’t want the root user to be accessible remotely. Enter y

If root access is really required, we recommend disallowing root login remotely and use an SSH tunnel instead to connect.

08. Remove the test database, enter y.

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.

09. Enter y to reload the privilege tables. 

10. Setup to secure is now complete.

11. Test the MySQL config, by login using the root account.

$ sudo mysql -u root -p
# Enter the root password

Uninstalling MySQL Server

Change to the root user. To avoid typing sudo multiple times.

$ sudo su -

Uninstall the mysql-server package. Review the packages to be removed and accept it with y.

$ apt-get remove mysql-server

Run autoremove to remove packages that are not needed anymore. Packages that have no more dependencies.

$ apt-get autoremove

Remove the installation directory for MySQL.

# rm -rf /var/lib/mysql

Verify that MySQL has been uninstalled by checking the MySQL version.

# mysql

For enquiries, product placements, sponsorships, and collaborations, connect with us at We'd love to hear from you!