While
package management in Ubuntu, as in most Linux operating systems today, makes it extremely easy to install, update, and remove a piece of software, some programs are a bit more complex in nature and contain multiple configuration folders, etc
.
One of these complex programs is MySQL Server. Uninstalling MySQL Server is not the same as simply running ‘apt purge‘ as is normally done. There are some small steps you can follow to completely uninstall MySQL Server from your Ubuntu machines. They are as follows:
Backup all
MySQL databases
If you do not have any databases created in MySQL, you can skip this step. If you have, before removing MySQL Server from the system, be sure to back up 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 the command line.
Remove MySQL Server on Ubuntu Ubuntu packages for MySQL Server start with ‘mysql-server’
and you can use ‘apt purge‘ to remove all these packages. $ sudo apt purge mysql-server* Remove MySQL
As you can see, you have removed 3 packages that contain files for the server. The reason we use ‘purge‘ instead of ‘delete‘ is that the former also deletes the configuration files for the program, while the latter only deletes the binaries from the program.
Delete
MySQL database files and logs
The ‘apt purge‘ command removes binaries and configuration files, however, there are some more MySQL configuration files and database files that are not touched by any package manager
. Configuration files are present in /etc/mysql and security keys and other related files are stored in /var/lib/mysql. $ ls /etc/mysql
$ sudo ls /var/lib/mysql
=”https://www.linuxshelltips.com/wp-content/uploads/2021/02/MySQL-Configuration-Files.png” alt=”MySQL configuration files” /> MySQL configuration files Therefore, these MySQL configuration files and
database files must be deleted manually
. $ sudo rm -r /etc/mysql /var/lib/mysql
If you have enabled logging for MySQL, be sure to delete
the log files as well. $ sudo rm -r /var/log/mysql
Remove
unrequired 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 ‘Orphan Packs‘.
Run the following apt command to remove such packages.
$ sudo apt autoremove <img src="https://www.linuxshelltips.com/wp-content/uploads/2021/02/Remove-Packages-Installed-Automatically.png" alt="Remove automatically installed packages
Note that this will remove ALL orphaned packages, not just those orphaned by MySQL server purge. You can see from the output that the 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 on 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, in order to avoid data-related disasters at the personal or organizational level.
Thanks for reading and let us know your thoughts in the comments below!