LinuxTutorialsUbuntu

How To Completely Uninstall MySQL Server on Ubuntu

Uninstall MySQL Server on Ubuntu

In this tutorial, we will show you how to completely uninstall the MySQL server on Ubuntu. Are you facing issues with your MySQL server installation on Ubuntu? Perhaps you’re planning to upgrade to a newer version or switch to a different database management system altogether. In such cases, it’s crucial to completely uninstall the existing MySQL setup to avoid conflicts and wasted disk space.

This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo‘ to the commands to get root privileges. I will show you the step-by-step completely remove MySQL on Ubuntu. You can follow the same instructions for Ubuntu 22.04, and any other Debian-based distribution like Linux Mint.

Prerequisites

  • A server running one of the following operating systems: Ubuntu Linux and any other Debian-based distribution like Linux Mint or elementary OS.
  • SSH access to the server (or just open Terminal if you’re on a desktop).
  • A non-root sudo user or access to the root user. We recommend acting as a non-root sudo user, however, as you can harm your system if you’re not careful when acting as the root.

Completely Uninstall MySQL Server on Ubuntu

Step 1. First, make sure that all your system packages are up-to-date by running the following apt commands in the terminal.

sudo apt update
sudo apt upgrade

Step 2. Backup All MySQL Databases.

Before proceeding with the uninstallation process, ensure that you have the MySQL server installed on your Ubuntu machine. Additionally, you’ll need sudo or root access to execute the necessary commands. It’s highly recommended to create a backup of your databases using the mysqldump utility before proceeding, as the uninstallation process will permanently delete all data.

mysqldump -u root -p --all-databases > backup.sql

This command will prompt you for the root password and create a backup file named “backup.sql” containing all your databases.

Step 3. Uninstall MySQL Server on Ubuntu.

Stopping the MySQL service is crucial before attempting to uninstall the server. A running service can interfere with the removal process and potentially cause issues. To stop the MySQL service, run the following command:

sudo systemctl stop mysql
sudo systemctl status mysql

With the service stopped you can proceed to uninstall the MySQL server, client, and common packages using the apt package manager. For Ubuntu and Debian-based distributions, run the following commands:

sudo apt-get purge mysql-server mysql-client mysql-common
sudo apt-get autoremove
sudo apt-get autoclean

The purge command ensures that not only the packages are removed but also any configuration files associated with them. autoremove removes any unnecessary dependencies, while autoclean clears the package cache.

Even after uninstalling the MySQL packages, some data and configuration files may remain on your system. These files are typically located in the /etc/mysql and /var/lib/mysql directories. To completely remove them, run the following commands:

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

If you prefer to keep a backup of these directories, you can rename them instead of deleting them outright:

sudo mv /etc/mysql /etc/mysql.backup
sudo mv /var/lib/mysql /var/lib/mysql.backup

 Additionally, remove any log files created by MySQL:

sudo rm -rf /var/log/mysql*

Step 4. Remove Orphaned Dependencies.

During the uninstallation process, some dependencies may become orphaned – that is, they are no longer required by any installed package. To remove these orphaned dependencies, run the following command:

sudo apt-get autoremove

Step 5. Remove MySQL User and Group.

MySQL creates a dedicated user and group during installation. To ensure a complete uninstallation, you should remove these as well:

sudo deluser mysql
sudo delgroup mysql

Step 6. Remove AppArmor Profiles (Optional).

If you’re using AppArmor on your Ubuntu system, MySQL may have created an AppArmor profile during installation. To remove this profile, run the following commands:

sudo apt-get purge apparmor-utils
sudo rm -rf /etc/apparmor.d/cache/usr.sbin.mysqld

After completing all the steps above, you can verify that MySQL has been completely removed from your system by running the following command:

dpkg --list | grep -i mysql

Congratulations! You have successfully uninstalled MySQL. Thanks for using this tutorial to uninstall the MySQL server on your Ubuntu Linux system. For additional help or useful information, we recommend you check the official MySQL website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is an experienced Linux enthusiast and technical writer with a passion for open-source software. With years of hands-on experience in various Linux distributions, r00t has developed a deep understanding of the Linux ecosystem and its powerful tools. He holds certifications in SCE and has contributed to several open-source projects. r00t is dedicated to sharing her knowledge and expertise through well-researched and informative articles, helping others navigate the world of Linux with confidence.
Back to top button