Home MySQL Completely Uninstall MySQL Server in Ubuntu

Completely Uninstall MySQL Server in Ubuntu

While package management in Ubuntu, as in most Linux operating systems nowadays, makes it extremely easy to install, upgrade and delete a piece of software, some software is a bit more complex in nature and contains multiple configuration folders, etc.

One such complex software is MySQL Server. Uninstalling MySQL Server does not simply amount to running ‘apt purge‘ as is done usually. There are few small steps you can follow to completely uninstall MySQL Server from your Ubuntu machines.

They are as follows:

Backup MySQL Databases in Linux

If you do not have any database created in MySQL, you can skip this step. If you have, before you remove MySQL Server from the system, make sure you take a backup of all your databases, so that when you reinstall it on another system or want to use the database on an existing server, you can simply restore them.

Learn how to backup databases in MySQL here – How to Backup All MySQL Databases from Command-Line.

Remove MySQL Server in Ubuntu

The Ubuntu packages for MySQL Server start with ‘mysql-server’ and you can use apt purge command to remove all these packages.

$ sudo apt purge mysql-server*
Remove MySQL Server in Ubuntu
Remove MySQL Server in Ubuntu

As you can see, it has removed 3 packages that contain files for the server. The reason we use ‘purge‘ instead of ‘remove‘ is that the former removes the configuration files for the program as well, whereas the latter only removes the program binaries.

Remove MySQL Database Files and Logs

The command ‘apt purge‘ does remove the binaries and the configuration files, however, there are some more MySQL configuration files and the database files which are not touched by any package manager.

The configuration files are present in /etc/mysql and the security keys and other related files are stored in /var/lib/mysql.

$ ls /etc/mysql
$ sudo ls /var/lib/mysql
MySQL Configuration Files
MySQL Configuration Files

Thus, these MySQL configuration files and database files need to be deleted manually.

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

If you have enabled logging for MySQL, make sure you delete the log files as well.

$ sudo rm -r /var/log/mysql

Remove Orphaned Packages Packages

Along with the MySQL Server packages installed by the package manager, there are some packages that are also installed as dependencies for the server. These are no longer required by the system, as the main package itself has been purged. They are also known as ‘Orphaned Packages’.

Run the following apt command to remove such packages.

$ sudo apt autoremove
Remove Packages Installed Automatically
Remove Packages Installed Automatically

Note that this will remove ALL orphaned packages, not only the ones orphaned by the purging of the MySQL Server. You can see in the output that MySQL Client packages are also being removed, as they are now useless without the server package.

Conclusion

We learned how to completely uninstall MySQL Server in Ubuntu in a few easy steps. Database deletions, upgrades, and installations should be handled with the utmost care, and data should be backed up from time to time, so as to prevent data-related disasters on a personal level or on an organizational level.

Thanks for reading and let us know your thoughts in the comments below!

Ravi Saive
I am an Experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies. Founder of TecMint.com, LinuxShellTips.com, and Fossmint.com. Over 150+ million people visited my websites.

Each tutorial at UbuntuMint is created by a team of experienced writers so that it meets our high-quality standards.

Was this article helpful? Please add a comment to show your appreciation and support.

3 thoughts on “Completely Uninstall MySQL Server in Ubuntu”

  1. Did not work because the system attempts to constantly re-install and then reports in a bad state.

    dpkg: warning: old mysql-server-8.0 package pre-removal script subprocess returned error exit status 1
    dpkg: trying script from the new package instead ...
    Failed to stop mysql.service: Unit mysql.service not loaded.
    invoke-rc.d: initscript mysql, action "stop" failed.
    dpkg: error processing archive /var/cache/apt/archives/mysql-server-8.0_8.0.27-0ubuntu0.20.04.1_amd64.deb (--unpack):
    new mysql-server-8.0 package pre-removal script subprocess returned error exit status 1
    Failed to stop mysql.service: Unit mysql.service not loaded.
    invoke-rc.d: initscript mysql, action "stop" failed.
    Failed to start mysql.service: Unit mysql.service not found.
    invoke-rc.d: initscript mysql, action "start" failed.
    Unit mysql.service could not be found.
    dpkg: error while cleaning up:
    installed mysql-server-8.0 package post-installation script subprocess returned error exit status 1
    Errors were encountered while processing:
    /var/cache/apt/archives/mysql-server-8.0_8.0.27-0ubuntu0.20.04.1_amd64.deb
    E: Sub-process /usr/bin/dpkg returned an error code (1)

    will not install any other updates on the system whilst this loop remains.

    Reply
    • @Sally,

      Try to a re-configure system with the following command will fix the error.

      $ sudo dpkg --configure -a
      

      If the above command is not fixed, try to force the package installation using the following command.

      $ sudo apt install -f
      OR
      $ sudo apt install --fix-broken
      
      Reply

Got something to say? Join the discussion.

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published or shared. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.