Deep Cleaning MySQL: Removing Remnants After Apt Purge Doesn't Work

Is MySQL still haunting your Debian/Ubuntu system even after a --purge? 😩 This guide shows techniques to completely remove MySQL remnants and get a clean start.

Deep Cleaning MySQL: Removing Remnants After Apt Purge Doesn't Work
AI generated

Still seeing MySQL ghosts after apt remove --purge? This guide provides techniques to completely remove MySQL remnants and ensure a clean install.

I was working on setting up the database on one of my servers and the application I was using called for MySQL. It would mostly work with MariaDB; however, I found that some functionality would throw SQL errors in the log because it just wasn’t completely compatible.

Luckily moving from MariaDB is as simple as writing the database out to a SQL file, installing MySQL, and reading it all back in. But I ran into an annoying issue while installing MySQL.

If you ever try to install MySQL and you notice the following error output during installation:

update-alternatives: using /etc/mysql/mysql.cnf to provide /etc/mysql/my.cnf (my.cnf) in auto mode
dpkg: error processing package mysql-community-server (--configure):
 installed mysql-community-server package post-installation script subprocess returned error exit status 1
dpkg: dependency problems prevent configuration of mysql-server:
 mysql-server depends on mysql-community-server (= 8.0.39-1debian12); however:
  Package mysql-community-server is not configured yet.

dpkg: error processing package mysql-server (--configure):
 dependency problems - leaving unconfigured
Processing triggers for man-db (2.11.2-2) ...
Processing triggers for libc-bin (2.36-9+deb12u7) ...
Errors were encountered while processing:
 mysql-community-server
 mysql-server
[master b632ad9] committing changes in /etc made by "apt install mysql-server"
 15 files changed, 153 insertions(+), 14 deletions(-)
 create mode 120000 alternatives/my.cnf
 create mode 100644 mysql/conf.d/mysql.cnf
 create mode 120000 mysql/my.cnf
 create mode 100644 mysql/my.cnf.fallback
 create mode 100644 mysql/mysql.cnf
 create mode 100644 mysql/mysql.conf.d/mysqld.cnf
Enumerating objects: 1470, done.
Counting objects: 100% (1470/1470), done.
Delta compression using up to 2 threads
Compressing objects: 100% (873/873), done.
Writing objects: 100% (1470/1470), done.
Total 1470 (delta 148), reused 1457 (delta 140), pack-reused 0
E: Sub-process /usr/bin/dpkg returned an error code (1)

This could be because you had a previous MySQL install. This is what happened to me. I probably installed MySQL at one point, then decide to switch to MariaDB. Or you attempted to install MySQL and it was left in a half-installed state. Either way, this helped me get a clean install— clean everything MySQL related up because even a purge doesn’t clean up the files.

First, you will need to run the following command over and over until all the MySQL packages are removed, ‘apt remove‘ didn’t work for me.

# List all packages related to MySQL and remove them
dpkg -l | grep -i 'mysql-' | awk '{print $2}' | xargs -n1 dpkg -P

I had to run this multiple times because there is an order that they need to be uninstalled, and I didn’t know what the order was. There is probably somewhere that documents that, but I had no interest in searching for it. Running it multiple times helps address the dependency order issues inherent in such scenarios. The loop will continue through the list and eventually get to the package that needs to be installed first. Then when you run it again, the package that failed last time will uninstall this time.

Next you will need to run this command if you don’t need the old MySQL config files.

# Remove existing MySQL configuration files. This ensures that the new installation defaults
# to clean settings and prevents conflicts.
rm -rf /etc/mysql
rm -rf /var/log/mysql*

There’s going to be config files in there that aren’t removed when you uninstall MySQL.

Lastly, I do these commands to get everything to a clean state

# Clean up apt package caches and remnants
apt remove mysql*
apt autoremove 
apt autoclean
apt update
apt upgrade

That’s all I had to do, hopefully this helps you too.