Completely uninstall MySQL-server in 3 simple steps

Many times we face issues while starting the MYSQL server on Linux System. There may be a couple of reasons for it. Sometimes old configuration file already exists in the system and is not removed correctly while uninstalling.

In that case, we have to manually delete the MYSQL server and configuration file. After that, try to reinstall it again.

Today, we will explain how you can uninstall the MYSQL server and configuration file entirely and make a fresh install on your Linux System.

Note: Make sure to take a backup of your MySQL databases. After following the below procedure, you may lose data your data permanently.

Uninstall MYSQL Sever Package

First of all, we have to check whether MySQL is running in our system or not. If it is already running in our system, your foremost step is to stop MySQL, or the uninstaller may throw an unusual error.

To check the status, pass the following command:

$ sudo systemctl status mysql
Checking MySQL Status
Checking MySQL Status

In my case, the MYSQL server is running, so we have to stop it.

$ sudo systemctl stop mysql
$ sudo systemctl status mysql
Stopping and rechecking MySQL Status
Stopping and rechecking MySQL Status

After verifying the status, you can remove the MySQL server without any errors. Not only that, but you can also remove the dependencies that were installed.

For that, we have to use the apt-purge command, which will ensure that no dependencies should be left over on your system.

Read more about the list of apt command’s which many users still don’t know.

To uninstall the MYSQL server, pass the following command:

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

The above command will ensure that whatever type of MySQL you have installed in your system should be removed completely.

Backup or Remove the MYSQL Directory

Once you complete the above steps, we need to remove residual files which present in /etc and /var/lib/mysql directory.

I strongly recommend you to rename the directory instead of removing it. If you wish to recover any piece of information in the future, it will be easy for you.

I’m asking you to follow because some of the configuration or log files can be useful for your next installations.

To take a backup of the respective directory, pass the below command:

$ sudo mv /var/lib/mysql /var/lib/mysql_bk
$ sudo mv /etc/mysql /etc/mysql_bk

Or else, you can delete it by typing the below command in your terminal:

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

Auto Remove & Clean leftover dependencies

The last step is to clean all the packages that are installed while installing MYSQL in our system because this file is no longer needed:

$ apt autoremove
$ apt autoclean

That’s all to uninstall MySQL from Ubuntu.

Installation of MYSQL Package

After complete uninstallation and removing the configuration file from your system, you may proceed to install MySQL on your Ubuntu/Debian machines.

To install MySQL, pass the below command, and once the installation is completed, it will recreate the new files in /var/lib/mysql.

$ sudo apt install mysql-server

You may be intrested to read How to install MySQL properly in Linux?

Wrap-up

If you are still facing any difficulty while removing and reinstalling MySQL in your system. Feel free to ask your query in the comment section.

Leave a Reply