In this article we are going to learn ” How to Install and Uninstall MySQL on Ubuntu 20.04/22.04 LTS? ” step-by-step.


MySQL is a powerful and popular open-source relational database management system used by many developers and businesses for data storage and management. If you’re running Ubuntu 20.04/22.04 LTS and need to set up MySQL or remove it from your system, this guide will walk you through both processes step by step.

Part 1: Install MySQL on Ubuntu 20.04/22.04 LTS

Step #1: Update Package List

Begin by updating the package list to ensure you have the latest information about available packages:

ubuntu@Rushi-InfoTech:~$ sudo apt-get update

Step #2: Install MySQL Server

Install MySQL Server using the following command:

ubuntu@Rushi-InfoTech:~$ sudo apt install mysql-server

Step #3: Start and Enable MySQL

MySQL should start automatically after installation. Confirm its status with:

ubuntu@Rushi-InfoTech:~$ sudo systemctl status mysql

If MySQL is not running, start it with:

ubuntu@Rushi-InfoTech:~$ sudo systemctl start mysql

To ensure MySQL starts on system boot, run:

ubuntu@Rushi-InfoTech:~$ sudo systemctl enable mysql
  • To verify the Installation of mysql presence on Ubuntu 20.04/22.04 LTS.
ubuntu@Rushi-InfoTech:~$ mysql --version
  • Enter into mysql database as a root user to set a root password.
ubuntu@Rushi-InfoTech:~$ sudo mysql
  • You need to run following command to set a root password:
mysql> alter user 'root'@'localhost' identified with mysql_native_password by 'Rushi@123';

In the place of “Rushi@123”, Please give your password to set as password.

  • If you want to exit from mysql database:
mysql> EXIT;
  • Secure MySQL 5.7 Installation on Ubuntu 20.04/22.04LTS.
ubuntu@Rushi-InfoTech:~$ sudo mysql_secure_installation


ubuntu@Rushi-InfoTech:~$ sudo mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root:

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: 2
Using existing password for root.

Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) : n

 ... skipping.
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

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y

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

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

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

 - Removing privileges on test database...

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

All done!
  • If you try to login to mysql as a root user you face an error now, after setting a password.
ubuntu@Rushi-InfoTech:~$ sudo mysql
  • So now you need to login as a root user with password. To go through root user password run the following command:
ubuntu@Rushi-InfoTech:~$ mysql -u root -p

You have successfully into mysql database now.

Part 2: Uninstall MySQL on Ubuntu 20.04/22.04 LTS

Step #1: Stop MySQL Service

Before uninstalling MySQL, you should stop the MySQL service:

ubuntu@Rushi-InfoTech:~$ sudo systemctl stop mysql

Step #2: Purge MySQL

To completely remove MySQL and its configuration files, use:

ubuntu@Rushi-InfoTech:~$ sudo apt purge mysql-server mysql-client mysql-common mysql-server-core-* mysql-client-core-*

Be cautious, as this action is irreversible and will delete your databases and data.

Step #3: Delete Configuration Files

Remove any residual MySQL configuration files:

ubuntu@Rushi-InfoTech:~$ sudo rm -rf /etc/mysql /var/lib/mysql

Step #4: Autoremove

Clean up any remaining MySQL-related packages:

ubuntu@Rushi-InfoTech:~$ sudo apt autoremove

Step #5: Update Package List

Finally, update the package list to ensure that all changes are recorded:

ubuntu@Rushi-InfoTech:~$ sudo apt-get update
  • Verify the mysql presence in server.
ubuntu@Rushi-InfoTech:~$ mysql --version


Whether you’re setting up MySQL for the first time or removing it from your Ubuntu 20.04/22.04 LTS system, this guide provides you with a comprehensive and easy-to-follow process. Always remember to back up critical data before uninstalling MySQL, as it will permanently erase your databases. MySQL is a valuable tool for managing data, and knowing how to install and uninstall it ensures you have full control over your database environment.

From this article we have learnt “How to Install and Uninstall MySQL on Ubuntu 20.04/22.04 LTS?” step-by-step.


To know more about MySQL please visit official website.

Any queries pls contact us

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *