Uninstall/Completely Remove MySQL Server From Ubuntu

mysql and ubuntu logos and a trash can icon

Package management in Ubuntu makes it fairly easy to install, upgrade, or remove applications from the system. But some pieces of software contain multiple configuration folders and require a bit of work.

One such software that is often used in Ubuntu is the MySQL Server. We can not utilize the usual process of apt purge to completely uninstall the MySQL server from the system. We need to follow certain steps to make sure that we completely remove MySQL and all its components from the Ubuntu system.

Note: Make sure that you backup all your MySQL Server databases before proceeding further. After compelling this process all the files and folders associated with MySQL Server will be deleted so if you want to keep your databases you need to create a backup first.

To completely remove MySQL Server and all its dependencies from your system follow the steps given below in the same succession.

Close MySQL Server

The first thing we need to do is to check whether the MySQL Server is running on our system or not. Because if we try to uninstall it while it is already running the uninstall process may result in an error.

To check the status of the MySQL server on your machine, run the following command.

sudo systemctl status mysql

If the output shows that MySQL Server is running on the system, we have to close it before proceeding further. Execute the following command to stop the process.

sudo systemctl stop mysql

You can execute the systemctl status mysql command to check if the service has been stopped. After making sure that the application is not running we can proceed with removing the MySQL Server without any errors. We can also uninstall any dependencies that were installed while setting up the server.

Remove MySQL Server

The next step in the uninstallation process is to remove MySQL Server packages. All the MySQL Server packages in Ubuntu start with mysql-server so we can purge them all together using the apt-purge command.

Execute the below command to remove MySQL packages from your system.

sudo apt purge mysql-server*

OR

You can execute this command to make sure that whichever type of MySQL is installed on your system, it gets removed.

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

We could have used the remove command to delete the MySQL package but the problem with using the remove command is that it only removes/deletes the program binaries whereas the purge command also deletes the configuration files for the program.

Remove MySQL Databases and Log Files

Even though we have deleted program binaries and configuration files using the purge command, there are still some databases, security keys, and configuration files that need to be deleted separately.

The configuration files that we need to delete are stored in /etc/mysql. The security keys that we need to delete are present at /var/lib/mysql.

We need to make sure that we remove these files completely or they will lie idly on your system and cause issues when you try to re-install MySQL Server on your machine.

Execute the below commands to check if there are files present in the above directories.

ls /etc/mysql
sudo ls /var/lib/mysql

To remove these configuration files, security keys, and database files execute the below command.

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

[powerkit_alert type=”info” dismissible=”false” multiline=”false”]
Note: We recommend renaming these directories instead of removing them as the data inside them can be of use in the future if you want to recover data.
[/powerkit_alert]

In case you had also enabled logging for MySQL Server, we will need to delete the log files too. Run the following command to delete any log files created by MySQL Server.

sudo rm -r /var/log/mysql

Remove Dependencies

When we install MySQL Server, the package manager installs multiple other dependency packages that are required to support the server. But since we have removed the main package i.e. MySQL Server, these dependencies are no longer needed and should be removed.

As their parent package has been removed and there is no use for these packages anymore, dependencies like these are also known as Orphaned Packages.

To remove all such dependencies run the following apt command.

sudo apt autoremove

A thing to remember here is that the autoremove command removes all the orphaned packages present on the system. So it will not only remove the leftover dependencies of MySQL Server but other orphaned packages will also be removed by execution of the above command.

We can also use the apt autoclean command to remove these left-over dependencies.

Conclusion

In this tutorial we learned how to completely remove/uninstall MySQL Server and all its related components from Ubuntu.

Operations related to databases should be performed with utmost care so as to avoid data loss. Creating backups of your databases is industry practice and can save you or your organization from major data loss.

0 Shares:
Subscribe
Notify of
guest
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)

0 Comments
Inline Feedbacks
View all comments
You May Also Like