How to Uninstall MySQL in Ubuntu 20.04

Most of the time, MySQL works great without any issues but sometimes we may face issues that can be related to configuration files and many others. You can’t just remove your MySQL by sudo apt remove. It requires some steps.

Today, we will guide you through how you can manually remove MySQLfrom Ubuntu 20.04 or later including configuration files. And we will also reinstall it again.

What is MySQL and Why it is Used?

MySQL is a database management system based on SQL (Structured Query Language). It is used for a wide range of applications such as data warehousing, e-commerce, and many more but it is mainly used for web databases.

It is so powerful that you can use it from storing a single record of data to the entire inventory of products for an e-commerce store. It can be scaled to handle thousands of queries per second.

Uninstalling MySQL from Ubuntu 20.04

Unlike other uninstallation processes where you can remove packages by a single command, we are required here to perform several tasks to remove MySQL completely from Ubuntu so that we can reinstall it without worrying about any conflicts

First, we have to check whether MySQL is currently running or not. If it is in an inactive state, we have to stop MySQL. The reason behind this is we can’t remove actively running programs. It will get us an error.

To check the status of MySQL, use the following command:

 sudo systemctl status mysql 

1. sudo systemctl status mysql

As you can see, MySQL is in an inactive state so use the following command to make it inactive:

 sudo systemctl stop mysql 

2. sudo systemctl stop mysql

Let’s check the status again:

 sudo systemctl status mysql 

3. sudo systemctl status mysql

As you can see, it is inactive now so we can proceed further in the uninstallation process.

We are going to use purge so that we can remove all the packages including dependencies. To uninstall MySQL Server with all dependencies, use the following commands:

 sudo apt purge mysql-server mysql-client mysql-common mysql-server-core-* mysql-client-core-* 

4. sudo apt purge mysql-server mysql-client mysql-common mysql-server-core-_ mysql-client-core-

Backing up and Removing MySQL Directories

We recommend you top backup your directories because they might contain important information inside. These files are present in /etc and /var/lib/mysql directory. We are going to rename them so they won’t get deleted.

To take backup of your files, use following commands:

 sudo mv /var/lib/mysql /var/lib/mysql_bk 

This command has renamed the file present at /var/lib/ from mysql to mysql_bk (bk is used for indicating backup, you can make it as you want.)

 sudo mv /etc/mysql /etc/mysql_bk 

Similar to the prior process, we have also renamed the file located at /etc/ which was previously named as mysql has been renamed to mysql_bk.

5. backing up both directories

Deleting MySQL Directories

If you want to remove directories rather than backing up, you can do that by using a single command:

 sudo rm -r /etc/mysql /var/lib/mysql 

6. deleting directories

Cleaning up Leftovers

This is the last step of the uninstallation process where we are going to remove packages that are no longer required for our system. Use the given commands to remove them:

 sudo apt autoremove 

7. sudo apt autoremove

As you can see, we have released 82.3 MB of space by removing unnecessary packages.

 sudo apt autoclean 

8. sudo apt autoclean

Reinstallation of MySQL

After going through the procedure of uninstallation, our system is ready for a clean installation of MySQL. When you install a fresh copy of MySQl, it will generate new files at /var/lib/mysql.

To download MySQL, use the following commands:

 sudo apt install mysql-server 

9. sudo apt install mysql-server

MySQL has been installed on your Ubuntu system.

Frequently asked questions related to MySQL

How do you check MySQL is installed or not?

You can do it easily by checking the status of MySQL. Use command sudo systemctl status mysql. If it is installed, it will show you the status of Active or Inactive. If it is not installed, it will get you an error by saying Unit mysql.service could not be found.

Does uninstalling MySQL delete the database?

No, by deleting MySQL you are just removing the packages. If you want to remove its database, you are required to run this command: drop database <database_name>

Related Posts:

Conclusion:

MySQL is a great tool for managing your web database but some unavoidable situations arise where we are required to uninstall MySQL from Ubuntu 20.04 and reinstall it to overcome errors.

Default image
Sagar Sharma

Sagar always uses Linux to its core and loves to write the technical side of system administration! While he's not writing, you can always find him exploring new Linux distros!

Articles: 31