我该如何修复“包处于非常糟糕的不一致状态”错误?

我该如何修复“包处于非常糟糕的不一致状态”错误?

最近,我尝试使用 Ubuntu 20.04 在我的计算机上安装软件包 mysql-server-8.0。我遇到了一些问题,于是决定完全卸载并重新安装 mysql。

因此,如果我尝试通过 卸载它sudo apt-get remove mysql-server-8.0,我将得到以下输出:

sudo apt-get remove mysql-server-8.0
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  linux-headers-5.4.0-54 linux-headers-5.4.0-54-generic linux-image-5.4.0-54-generic linux-modules-5.4.0-54-generic linux-modules-extra-5.4.0-54-generic mecab-ipadic mecab-ipadic-utf8 mecab-utils python3-imdbpy
Use 'sudo apt autoremove' to remove them.
The following packages will be REMOVED:
  mysql-server-8.0
0 upgraded, 0 newly installed, 1 to remove and 3 not upgraded.
2 not fully installed or removed.
After this operation, 1465 kB disk space will be freed.
Do you want to continue? [Y/n] y
dpkg: error processing package mysql-server-8.0 (--remove):
 package is in a very bad inconsistent state; you should
 reinstall it before attempting a removal
dpkg: too many errors, stopping
Errors were encountered while processing:
 mysql-server-8.0
Processing was halted because there were too many errors.
E: Sub-process /usr/bin/dpkg returned an error code (1)

输出建议重新安装该包,因此我尝试了这个:

sudo apt install --reinstall mysql-server-8.0
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  linux-headers-5.4.0-54 linux-headers-5.4.0-54-generic linux-image-5.4.0-54-generic linux-modules-5.4.0-54-generic linux-modules-extra-5.4.0-54-generic python3-imdbpy
Use 'sudo apt autoremove' to remove them.
Suggested packages:
  mailx tinyca
The following packages will be upgraded:
  mysql-server-8.0
1 upgraded, 0 newly installed, 0 to remove and 3 not upgraded.
2 not fully installed or removed.
Need to get 0 B/1261 kB of archives.
After this operation, 1024 B of additional disk space will be used.
Preconfiguring packages ...
(Reading database ... 751683 files and directories currently installed.)
Preparing to unpack .../mysql-server-8.0_8.0.22-0ubuntu0.20.04.3_amd64.deb ...
Failed to stop mysql.service: Unit mysql.service not loaded.
invoke-rc.d: initscript mysql, action "stop" failed.
dpkg: warning: old mysql-server-8.0 package pre-removal script subprocess returned error exit status 1
dpkg: trying script from the new package instead ...
Failed to stop mysql.service: Unit mysql.service not loaded.
invoke-rc.d: initscript mysql, action "stop" failed.
dpkg: error processing archive /var/cache/apt/archives/mysql-server-8.0_8.0.22-0ubuntu0.20.04.3_amd64.deb (--unpack):
 new 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:
 /var/cache/apt/archives/mysql-server-8.0_8.0.22-0ubuntu0.20.04.3_amd64.deb
E: Sub-process /usr/bin/dpkg returned an error code (1)

这里最有趣的部分是这一行:

Failed to stop mysql.service: Unit mysql.service not loaded.

没有运行mysql服务:

systemctl status mysql
Unit mysql.service could not be found.

似乎失败是因为没有运行 mysql 服务。我也无法启动它。

systemctl start mysql 
Failed to start mysql.service: Unit mysql.service not found.

现在我被困在这里,无法再次卸载/重新安装 mysql。我还尝试使用 synaptic 和 删除该软件包sudo dpkg --purge --force-all mysql-server-8.0,这是我在互联网上找到的。它们都输出一条消息,告诉我该软件包处于错误状态。

dpkg -l | egrep -v '^ii|rc'输出结果如下:

Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
+++-===========================================================-==========================================================-============-======================================================================================================
it  desktop-file-utils                                          0.24+linuxmint1                                            amd64        Utilities for .desktop files
it  gnome-menus                                                 3.36.0-1ubuntu1                                            amd64        GNOME implementation of the freedesktop menu specification
it  hicolor-icon-theme                                          0.17-2                                                     all          default fallback theme for FreeDesktop.org icon themes
it  man-db                                                      2.9.1-1                                                    amd64        tools for reading manual pages
it  mime-support                                                3.64ubuntu1                                                all          MIME files 'mime.types' & 'mailcap', and support programs
pU  mysql-client-8.0                                            8.0.22-0ubuntu0.20.04.3                                    amd64        MySQL database client binaries
pi  mysql-common                                                5.8+1.0.5ubuntu2                                           all          MySQL database common files, e.g. /etc/mysql/my.cnf
rFR mysql-server-8.0                                            8.0.22-0ubuntu0.20.04.2                                    amd64        MySQL database server binaries and system database setup
it  shared-mime-info                                            1.15-1                                                     amd64        FreeDesktop.org shared MIME database and spec
iU  xserver-common                                              2:1.20.8-2ubuntu2.6                                        all          common files used by various X servers
iU  xserver-xephyr                                              2:1.20.8-2ubuntu2.6                                        amd64        nested X server
iU  xserver-xorg-core                                           2:1.20.8-2ubuntu2.6                                        amd64        Xorg X server - core server
iU  xserver-xorg-legacy                                         2:1.20.8-2ubuntu2.6                                        amd64        setuid root Xorg server wrapper
iU  xwayland                                                    2:1.20.8-2ubuntu2.6                                        amd64        Xwayland X server

ls -al /etc/init.d/ | grep mysql给出:

-rwxr-xr-x   1 root root  5607 Nov  6  2019 mysql

答案1

wget -c http://security.ubuntu.com/ubuntu/pool/main/m/mysql-8.0/mysql-server-8.0_8.0.22-0ubuntu0.20.04.3_amd64.deb

直接使用 dpkg 安装包。

sudo dpkg -i mysql-server-8.0_8.0.22-0ubuntu0.20.04.3_amd64.deb

仅当没有错误时。

software-properties-gtk

并选择从主服务器下载。

sudo apt update && sudo dpkg --configure -a && sudo apt -f install

然后更新您的系统。

sudo apt full-upgrade

答案2

尽管这可能不是最干净的方法,但我还是设法修复了我的特定问题。由于 mysql 服务不存在导致删除脚本失败,因此我尝试了以下操作:

  1. 导航到 /var/lib/dpkg/info/

  2. 以 root身份打开文件mysql-server-8.0-prerm(例如使用 gedit),这是删除前的脚本

  3. 我已经评论了试图停止该服务的行:

    #if [ -x "/etc/init.d/mysql" ]; then #invoke-rc.d mysql stop || exit 1 #fi

  4. 然后我就跑了sudo dpkg --purge --force-all mysql-server-8.0

  5. 这给出了一些警告,但--force开关仍然强制卸载该包

  6. 删除软件包后,我现在安装了一个损坏的软件包(mysql-client-8.0),我可以使用 Synaptic 软件包管理器的 GUI 将其删除(apt-get remove 可能也可以工作)

这至少为我解决了这个问题,我可以再次安装其他软件包。由于状态不稳定,即使不需要 mysql,也无法安装新软件包。

非常感谢所有试图帮助我的人。很抱歉浪费了你们的时间,但也许其他人会觉得你的回答很有帮助!

答案3

要删除 MySQL,首先运行以下命令:

sudo apt remove -y mysql-*

进而:

sudo apt purge -y mysql-*

现在,要mysql再次安装,更新软件包列表并使用命令

sudo apt update
sudo apt install mysql-server

答案4

创建符号链接/etc/systemd/system/multi-user.target.wants/mysql.service/lib /systemd/system/mysql服务。

我想如果你创建指向该文件的符号链接它就会修复它,我自己也遇到过这个错误我按照前一个人的做法清除了除 mysql-common 之外的所有内容(如果你删除它将会卸载你一半的操作系统)。

并在重新安装时注意到符号链接的创建。

或许可以避免一些人用头撞桌子。

相关内容