In this article, I'll show you how to completely remove MySQL (or MariaDB) from Ubuntu 24.04, whether you're facing storage issues, have forgotten your password, or need to reinstall it.
All the steps mentioned are recommended ones, and each step is explained in detail in its respective section. So, let's beginβ¦
How to Uninstall MySQL from Ubuntu 24.04
If you have access to the MySQL console, it's always advisable to take a backup of your database before beginning any of the steps involved in MySQL uninstallation.
Step 1: Take MySQL Database Backup
The first step is to enter the MySQL console and execute the following command to list all MySQL databases.
MySQL> show databases;
Take note of the MySQL database you wish to export, then exit the MySQL console and use the following command, specifying the MySQL username, password, and database for exporting in SQL format.
$ sudo mysqldump -u [user] -p [db_name] > [export_the_db.sql]
Once you have exported the MySQL database, you can then proceed to fully remove MySQL from your Ubuntu system.
Step 2: Stop MySQL Service
First, make sure to stop the MySQL service by running:
$ sudo systemctl stop mysql
Step 3: Remove MySQL Packages
Then remove all MySQL-related packages.
$ sudo apt purge mysql-server mysql-client mysql-common mysql-server-core-* mysql-client-core-*
During removal, it may ask you to approve the removal of all databases; select "Yes" to proceed.
Step 4: Delete MySQL-Related Files
Now that we have completely removed all MySQL packages, make sure to delete the remaining MySQL related files and data by running:
π Note
If you've modified the database location in your MySQL configuration during installation, adjust "/var/lib/mysql" accordingly.
$ sudo rm -rf /etc/mysql /var/lib/mysql /var/log/mysql
Step 5: Clean Unneeded Packages
During MySQL installation, some dependencies are installed along with it that are no longer needed after its removal, so you can remove them by running:
$ sudo apt autoremove && sudo apt autoclean
Step 6: Remove MySQL Configurations
Finally, make sure to remove all the remaining MySQL configuration by running:
$ sudo apt remove dbconfig-mysql
That's all it takes to completely get rid of MySQL from Ubuntu 24.04. If you are facing any difficulties, feel free to reach out to us via comment.