This is a frustrating issue regarding my own personal negligence. I had forgotten I had an instance of MariaDB installed when I installed mysql-server-8.0 onto my laptop. Unfortunately, it seems this has completely broken apt. apt update works as expected, but apt install, apt autoremove, apt remove, and more return something similar to below.
root@xxx:/var/cache# sudo apt autoremove
Reading package lists... Done
Building dependency tree
Reading state information... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
1 not fully installed or removed.
After this operation, 0 B of additional disk space will be used.
Setting up mysql-server-8.0 (8.0.29-0ubuntu0.20.04.3) ...
Failed to stop mysql.service: Unit mysql.service not loaded.
invoke-rc.d: initscript mysql, action "stop" failed.
update-alternatives: error: alternative path /etc/mysql/mysql.cnf doesn't exist
dpkg: error processing package mysql-server-8.0 (--configure): installed mysql-server-8.0 package post-installation script subprocess returned error exit status 2
Errors were encountered while processing: mysql-server-8.0
E: Sub-process /usr/bin/dpkg returned an error code (1)mysql-server is clearly installed, but the output of autoremove says that the service cannot be found. Which means stopping, starting, restarting, and reloading the service does nothing, because it's simply not there.
At this point I'm not sure what to do. Is there a way to manually remove the package without apt? Any help would be appreciated.
21 Answer
Sorry, didn't read the question as careful as I should.
The apt-get autoremove does not try to remove mysql-server-8.0. After it found nothing to remove, it tries to configure the packages in state "config", in your case mysql-server-8.0. Now it depends which state exactly.
As always, make sure you have a backup and a plan to recover your database, just in case!
In most cases this command should help: dpkg -P mysql-server-8.0
If this does not do the job, the installation is in a phase where the preinst and/or postinst scripts are being executed. In this case the explanation below can be used, but with the /var/lib/dpkg/info/mysql-server-8.0.preinst and /var/lib/dpkg/info/mysql-server-8.0.postinst scripts.
Sorry, currently I think this is not for this question, but I will keep it in case or somebody else comes by and needs it.
Without deeper knowledge, all you do could raise more problems. And without detailed knowledge of your system I can only give you some hints, where some might not be optimal. But lets try ...
You have to satisfy 'dpkg' to get no errors from the (un)install scripts. This can be done in several ways.
Best would be to get the system in a state that the script runs without error. This is difficult, because it would go step by step.
Look in the prerm and postrm scripts of the package and try to understand the steps, especially the ones failing. Put everything in place that the script runs without error. In some cases the only way might be to remove/comment the line with the failing command (but make sure to keep the script bash-syntax correct).
See this for help in debugging debian packages:
The other "fast and easy" solution, which is not optimal and should be avoided whenever possible and only the last if nothing else helps:
Add exit 0 in the second line of the scripts (if they exist). In your case most probably:
/var/lib/dpkg/info/mysql-server-8.0.prerm/var/lib/dpkg/info/mysql-server-8.0.postrm
In some rare cases you have to do this in these scripts, too:
/var/lib/dpkg/info/mysql-server-8.0.preinst/var/lib/dpkg/info/mysql-server-8.0.postinst
See the flowcharts of maintainer scripts: Maintainer script flowcharts
Afterwards apt autoremove should run without error. BUT this will not remove everything as the developer of the package wants. It just satisfies dpkg to run without errors.