mysql.service is missing but shows up on list - fails install
Asked Answered
M

2

8

I'm on Ubuntu 17.04.

Long story short, after wrestling with MariaDB for a bit, I followed some questionable advice on how to completely remove both MySQL and MariaDB from my home server. Now I'm unable to reinstall mysql-server. I'm getting this error when I try:

Failed to stop mysql.service: Unit mysql.service not loaded.
invoke-rc.d: initscript mysql, action "stop" failed.
invoke-rc.d returned 5
There is a MySQL server running, but we failed in our attempts to stop it.
Stop it yourself and try again!

So I check for the service with sudo service --status-all | grep mysql, and sure enough...

[ - ]  mysql

Problem is, when I try sudo service mysql status, I get

Unit mysql.service could not be found.

I'd really like to get MySQL installed and working again without doing a clean install! Any help is appreciated.

Mera answered 3/8, 2017 at 21:24 Comment(2)
The process may have zombied? You may likely have to reboot and try again.Lanie
I thought so, too. I restarted a few times. Also, ps aux | grep mysql doesn't show anything.Mera
M
23

Looks like I got it. It doesn't make any sense, but I got it. First, I asked dpkg whether any mysql packages were still around:

sudo dpkg --get-selections | grep mysql

Nothing came up, so I checked for MariaDB:

sudo dpkg --get-selections | grep maria

This showed some packages marked for deinstallation.

mariadb-client-10.1                             deinstall
mariadb-common                                  deinstall
mariadb-server-10.1                             deinstall

Since apt wasn't cooperating, I uninstalled each one with dpkg -P. Once I did that and rebooted, mysql was no longer listed as a service and mysql-server installed perfectly. Hope this saves someone else some time.

Mera answered 3/8, 2017 at 22:7 Comment(2)
Thanks for posting your discovery!Lanie
thx for your answer ---> "I uninstalled each one with dpkg -P" ---> how do you do that?Transmontane
E
3

The dpkg -P answer didn't work for me, but the procedure below did. Summary:

There's some interaction between MariaDB and MySQL that prevents /etc/init.d/mysql being removed during an uninstall. This prevents MySQL uninstalling fully, or re-installing. If you manually remove this file, and then repeat the uninstall, things should work. However, it took me a while to reach this conclusion, and the system had changed, so I didn't try this directly. The full procedure that worked for me is below, for apt-based systems.

In /var/lib/dpkg/info you'll find the relevant packages:

# cd /var/lib/dpkg/info
# ll | grep -i mariadb
# ll | grep -i mysql

The packages have a set of suffixes, which are .preinst, .prerm, and so on. If there's a php8.1-mysql.prerm, for example, the package can be removed as

# dpkg --purge --force-all php8.1-mysql

Eventually, you're left with mysql-server-8.0, which can't be removed, and gives the error message in the question. This contains:

if [ -z "${DPKG_ROOT:-}" ] && [ "$1" = remove ] && [ -x "/etc/init.d/mysql" ] ; then
    invoke-rc.d mysql stop || exit 1
fi

The problem is that /etc/init.d/mysql wasn't removed by the uninstall. Remove this file manually, and the uninstall will now complete.

You can now install mysql-server. This completes, but may not install everything on the first attempt - you'll have to clear the apt cache, or similar - I don't remember the exact details.

Enounce answered 6/3, 2023 at 21:33 Comment(1)
That's correct for /etc/init.d/mysql wasn't removed by the uninstallBought

© 2022 - 2024 — McMap. All rights reserved.