Fix for MYSQL 8x installation failure on Ubuntu 20.04

Day 1

Getting the following every time I try to reinstall MySQL server

Failed to stop mysql.service: Unit mysql.service not loaded.
invoke-rc.d: initscript mysql, action "stop" failed.
dpkg: error processing package mysql-server-8.0 (--remove):
 installed mysql-server-8.0 package pre-removal script subprocess returned error exit status 1
Failed to stop mysql.service: Unit mysql.service not loaded.
invoke-rc.d: initscript mysql, action "stop" failed.
Failed to preset unit: File mysql.service: Link has been severed
/usr/bin/deb-systemd-helper: error: systemctl preset failed on mysql.service: No such file or directory
Failed to start mysql.service: Unit mysql.service not found.
invoke-rc.d: initscript mysql, action "start" failed.
Unit mysql.service could not be found.
dpkg: error while cleaning up:
 installed mysql-server-8.0 package post-installation script subprocess returned error exit status 1
Errors were encountered while processing:
 mysql-server-8.0

Found out there’s open bug in Ubuntu’s #launchpad & a discussion is MySQL official forum. The solution didn’t works out to me.

Removed couple of scripts responsbile for pre/post installations.

# remove post/pre install scripts
sudo rm -rf /var/lib/dpkg/info/mysql-server-8.0.preinst
sudo rm -rf /var/lib/dpkg/info/mysql-server-8.0.postinst
sudo rm -rf /var/lib/dpkg/info/mysql-*

Still no luck 🙁


Day 2 – Research & learn

Another search leads to #CopyProgramming which provides me a hint why mysql is listed as running during installtion while it is not.

Maria – sister of MySQL conflicts with possible services/configs.

sudo dpkg --get-selections | grep maria
    mariadb-client-10.1                             deinstall
    mariadb-common                                  deinstall
    mariadb-server-10.1                             deinstall

And I removed them all.


Day 3 – Final fix !

Meanwhile some how I landed on this #AskUbuntu which leads me to validate the service path.

Upon checking, /etc/systemd/system/mysql.service linked to broken mariadb.service which may be possible culprit. I tried mariadb long back and it may root cause now.

#link broken
ls -l /etc/systemd/system/mysql*
/etc/systemd/system/mysql.service ⇒ /lib/systemd/system/mariadb.service
/etc/systemd/system/mysqld.service ⇒ /lib/systemd/system/mariadb.service

I unlinked them, correct the link & rebooted the device

#correct target
ls -l /lib/systemd/system/my*
/lib/systemd/system/mysql.service
/lib/systemd/system/mysql@.service

#link fixed
ls -l /etc/systemd/system/mysql*
/etc/systemd/system/mysql.service ⇒ /lib/systemd/system/mysql.service
/etc/systemd/system/mysqld.service ⇒ /lib/systemd/system/mysql.service

Did a fresh attempt of reinstalling the mysql-server with others.

# prepare
sudo apt --fix-broken install
sudo apt autoclean 
sudo apt autoremove

# make sure mysql8 is selected
sudo dpkg -i mysql-apt-config_0.8.12-1_all.deb

# cleaning everything
sudo rm -rf /etc/apparmor.d/abstractions/mysql /etc/apparmor.d/cache/usr.sbin.mysqld /etc/mysql /var/lib/mysql /var/log/mysql* /var/log/upstart/mysql.log* /var/run/mysqld 

# fresh install
sudo apt install mysql-server-8.0 mysql-client mysql-common mysql-server-core-8.0 mysql-client-core-8.0 

Yes! It works. Finally I’m able to install and run MySQL after long time.

Outcome:

dpkg -l | grep mysql
ii  mysql-apt-config                                            0.8.12-1                                     all          Auto configuration for MySQL APT Repo.
ii  mysql-client                                                8.0.36-0ubuntu0.20.04.1                      all          MySQL database client (metapackage depending on the latest version)
ii  mysql-client-8.0                                            8.0.36-0ubuntu0.20.04.1                      amd64        MySQL database client binaries
ii  mysql-client-core-8.0                                       8.0.36-0ubuntu0.20.04.1                      amd64        MySQL database core client binaries
ii  mysql-common                                                5.8+1.0.5ubuntu2                             all          MySQL database common files, e.g. /etc/mysql/my.cnf
ii  mysql-server-8.0                                            8.0.36-0ubuntu0.20.04.1                      amd64        MySQL database server binaries and system database setup
ii  mysql-server-core-8.0                                       8.0.36-0ubuntu0.20.04.1                      amd64        MySQL database server binaries
0 ✓ success ^_^  

sudo mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 8.0.36-0ubuntu0.20.04.1 (Ubuntu)

Copyright (c) 2000, 2024, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> ^DBye

Update : 4/3/2024

The future me!

Yes! The future is here. Encountered similar issue again after a month (exactly). Possibly this is due to dir /var/lib/mysql/ mounted on other disk or other partition and AppArmor prevent mysql accessing the dir.

There’s bug on this at MySQL. Bug #82281 – mysqld –initialize reports “Permission denied”, regardless of permissions

Workaround: Disable the mysqld profile for AppArmor
sudo aa-disable /usr/sbin/mysqld

Appendix

Some use full commands:


#key config files/path
ls -laF /lib/systemd/system/mysql.service   # path of mysql.service

# fixing deps / broken packages
sudo apt install -f                 # install the deps / broken packages
sudo apt install --fix-broken
sudo dpkg --configure -a            # does the above with more on config missing services

sudo apt install --reinstall mysql-server-8.0   #re-install the package

sudo systemctl disable mysql.service        # disable the service
sudo systemctl enable mysql.service         # enable the service

# mysql related packages
dpkg -l | awk  '{print $2}' | grep -i mysql | grep -v lib   # list packages
sudo dpkg -P  --force-all $(dpkg -l | awk  '{print $2}' | grep -i mysql | grep -v lib)  # purge them

# remove post/pre install scripts
sudo rm -rf /var/lib/dpkg/info/mysql-*
sudo dpkg --remove --force-remove-reinstreq mysql-server-8.0

#beta zone
sudo apt-get --dry-run install initscripts

sudo dpkg --remove --force-remove-reinstreq mysql-server-8.0
sudo apt purge apparmor 

# another batch
sudo -i
service mysql stop
killall -KILL mysql mysqld_safe mysqld
apt-get --yes purge mysql-server mysql-client
apt-get --yes autoremove --purge
apt-get autoclean
deluser --remove-home mysql
delgroup mysql
rm -rf /etc/apparmor.d/abstractions/mysql /etc/apparmor.d/cache/usr.sbin.mysqld /etc/mysql /var/lib/mysql /var/log/mysql* /var/log/upstart/mysql.log* /var/run/mysqld
updatedb
sudo apt update
sudo apt upgrade
wget -c https://dev.mysql.com/get/mysql-apt-config_0.8.15-1_all.deb
sudo dpkg -i mysql-apt-config*
apt update
sudo apt update
sudo apt upgrade
sudo apt install mysql-server


# mysql abstraction - fix this else you may encounter ERROR for future aa-complain commands
$cat /etc/apparmor.d/abstractions/mysql

# ------------------------------------------------------------------
#
#    Copyright (C) 2002-2006 Novell/SUSE
#    Copyright (C) 2013 Christian Boltz
#
#    This program is free software; you can redistribute it and/or
#    modify it under the terms of version 2 of the GNU General Public
#    License published by the Free Software Foundation.
#
# ------------------------------------------------------------------

   /var/lib/mysql{,d}/mysql{,d}.sock rw,
   /{var/,}run/mysql{,d}/mysql{,d}.sock rw,
   /usr/share/{mysql,mysql-community-server,mariadb}/charsets/ r,
   /usr/share/{mysql,mysql-community-server,mariadb}/charsets/*.xml r,
This entry was posted in gnu, linux, technology and tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


8 + three =