无法在 Ubuntu 20.04 上删除 mysql-server-8.0

无法在 Ubuntu 20.04 上删除 mysql-server-8.0

dist 将 Ubuntu 升级到 Ubuntu 20.04 后,mysql 不起作用。我试图删除它,我的目标是重新安装它。问题是我无法删除mysql-server-8.0

sudo dpkg -r --force-all mysql-server-8.0
(Reading database ... 210243 files and directories currently installed.)
Removing mysql-server-8.0 (8.0.28-0ubuntu0.20.04.3) ...
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
openhabvm@OpenHABVM:~$ sudo dpkg -r --force-all mysql-server-8.0
(Reading database ... 210243 files and directories currently installed.)
Removing mysql-server-8.0 (8.0.28-0ubuntu0.20.04.3) ...
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

答案1

错误信息表明软件包的安装后脚本尝试启用 MySQL 的服务文件,但发现它已经关闭。您可以尝试按如下方式将其删除:

sudo service mysqld start
sudo dpkg -r --force-all mysql-server-8.0

如果出现相同的错误,请按照以下方法之一操作。

用于强制清除包。

  1. 由于删除软件包可能会造成破坏,因此请打印将要删除的软件包列表:

    dpkg -l | awk  '{print $2}' | grep -i mysql | grep -v lib
    

    检查是否有任何重要软件包被删除。如果一切正常,请继续执行步骤 2。

  2. 清除包:

    sudo dpkg -P  --force-all $(dpkg -l | awk  '{print $2}' | grep -i mysql | grep -v lib)
    

这应该会完全删除 MySQL。然后运行强制安装:

sudo apt -f install

如果上述操作失败并出现相同错误,请按照以下步骤操作:

  1. 删除软件包的安装后脚本:
    sudo rm /var/lib/dpkg/info/mysql-server-8.0.postinst
    
  2. 清除包:
    sudo dpkg -P  --force-all $(dpkg -l | awk  '{print $2}' | grep -i mysql | grep -v lib)
    

运行强制安装:

sudo apt -f install

清除包后,要重新安装它,请运行:

sudo apt update
sudo apt install mysql-server-8.0

答案2

我从 Ubuntu 20.04 切换到 22.04 后遇到了同样的问题(即无法安装 mariadb-server 或 mysql-server),通过删除以下内容然后重复安装过程可以解决这个问题:

  1. sudo rm /var/lib/dpkg/info/mysql-server-8.0.postinst
  2. sudo rm /var/lib/dpkg/info/mysql-server-8.0.preinst
  3. sudo rm /var/lib/dpkg/info/mysql-server-8.0.prerm

尝试一下,看看它是否适合你。

答案3

最近遇到了同样的问题。使用以下提示修复

  • 检查是否有任何损坏的服务链接 ( /etc/systemd/system/mysql.service)
  • 删除所有 MariaDB 内容 ( sudo dpkg --get-selections | grep maria)
  • 找到详细信息这里

相关内容