How To Completely Uninstall MySQL from Ubuntu

How To Completely Uninstall MySQL from Ubuntu

Published December 4, 2017

I own a desktop and a laptop. Both of them are running a modified version of Ubuntu called Kubuntu. It’s essentially Ubuntu with the KDE Plasma desktop installed. I recently updated both of my machines from Kubuntu 17.04 to Kubuntu 17.10. Everything seemed to be working fine, until the other day when I tried to work on a project that used MySQL. Whenever I tried to connect to one of my MySQL databases I would get some cryptic connection errors. So, I decided to just go ahead and reinstall MySQL. Easier said then done.

It would seem that just uninstalling via the package manger would work. The problem was, it didn’t. Whenever I tried reinstalling all of the dependencies would install correctly but mysql-server itself would produce a dpkg error on install. After several hours of googling and what seemed like hundreds of possible solutions, I finally found the correct incantation of commands that were needed to get MySQL reinstalled and working.

The first step was to get a list of all the MySQL packages that I had installed on my system.

sudo dpkg -l | grep mysql

The next step was to use my package manager to remove all of the packages the first command listed. If you have any databases setup with MySQL, the uninstall process will ask to remove them. I would recommend removing them but be sure to back them up first.

sudo apt-get --purge autoremove mysql-package-1 mysql-package2

This command will remove all of the packages listed and remove all of their dependencies that aren’t needed by other applications. Next, I used the find command to search for any folders that were not removed when MySQL was uninstalled and then removed them. Keep in mind that not ever folder that the find command found needs to be removed. For instance, there was a MySQL folder in my bash-completion folder that did not need to be removed.

sudo find / -name "mysql"
sudo rm -rf /path/to/mysql/folder/from/results

At this point, there should be no trace of MySQL left on our system. If there is then…try googling? The final part is to reinstall MySQL

sudo apt-get install -f mysql-server

If you had to delete any folders that the find command found, the MySQL install command may give you some messages about existing configuration files being overwritten by a user or scripts. It will ask you if you want to install the newest version provided by the MySQL package, which you should say yes. And there you have it. MySQL was completely removed and then reinstalled.