Ubuntu 20.10 无法安装或卸载 MySQL

Ubuntu 20.10 无法安装或卸载 MySQL

在升级到 21.04 LTS 之前尝试更新系统时,MySQL 给出无法更新或修复的错误,因此我使用以下命令清除了它:

sudo apt purge mysql-server-8.0
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages will be REMOVED:
  mysql-server-8.0*
0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded.
After this operation, 0 B of additional disk space will be used.
Do you want to continue? [Y/n] y
(Reading database ... 555934 files and directories currently installed.)
Purging configuration files for mysql-server-8.0 (8.0.17-0ubuntu2) ...
Processing triggers for systemd (246.6-1ubuntu1.3) ...

但是,现在无法重新安装。

sudo apt-get install mysql-client
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
 mysql-common : Conflicts: mysql-client-8.0 but 8.0.25-0ubuntu0.20.10.1 is to be installed
                Conflicts: mysql-client-core-8.0 but 8.0.25-0ubuntu0.20.10.1 is to be installed

显然有一些东西坏了,但不确定是什么,所以我该怎么办?

以下是根据评论得出的一些附加输出:

Sudo apt update
Hit:1 http://archive.ubuntu.com/ubuntu groovy InRelease
Hit:2 http://archive.ubuntu.com/ubuntu groovy-updates InRelease
Hit:3 http://archive.ubuntu.com/ubuntu groovy-security InRelease
Reading package lists... Done
Building dependency tree       
Reading state information... Done
All packages are up to date.
sudo apt clean

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.

sudo apt --fix-broken install
Reading package lists... Done
Building dependency tree       
Reading state information... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

sudo apt update && sudo apt upgrade
Hit:1 http://archive.ubuntu.com/ubuntu groovy InRelease
Hit:2 http://archive.ubuntu.com/ubuntu groovy-updates InRelease
Hit:3 http://archive.ubuntu.com/ubuntu groovy-security InRelease
Reading package lists... Done
Building dependency tree       
Reading state information... Done
All packages are up to date.
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Calculating upgrade... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

sudo dpkg --configure -a

sudo apt install -f
Reading package lists... Done
Building dependency tree       
Reading state information... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

然后尝试安装 mysql-client,尽管没有安装,但 mysql-client 仍然出现错误:

sudo apt install mysql-client
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
 mysql-common : Conflicts: mysql-client-8.0 but 8.0.25-0ubuntu0.20.10.1 is to be installed
                Conflicts: mysql-client-core-8.0 but 

8.0.25-0ubuntu0.20.10.1 is to be installed

E: Unable to correct problems, you have held broken packages.

其他尝试:

sudo apt install mysql-client
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
 mysql-common : Conflicts: mysql-client-8.0 but 8.0.25-0ubuntu0.20.10.1 is to be installed
                Conflicts: mysql-client-core-8.0 but 8.0.25-0ubuntu0.20.10.1 is to be installed
E: Unable to correct problems, you have held broken packages.

sudo apt purge mysql-client-8.0
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Package 'mysql-client-8.0' is not installed, so not removed
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

sudo apt purge mysql-client-core-8.0
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Package 'mysql-client-core-8.0' is not installed, so not removed
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

它似乎告诉我 MySQL 未安装,但其安装存在冲突!我该如何解决这个难题?

答案1

有时只需稍微清理一下即可解决这个问题apt。在终端中,尝试以下操作:

sudo apt update
sudo apt clean
sudo apt autoremove

现在尝试安装 MySQL 客户端。如果错误仍然存​​在,那么您可能需要“修复”安装:

sudo apt --fix-broken install
sudo apt update && sudo apt upgrade
sudo dpkg --configure -a
sudo apt install -f

现在您应该可以安装客户端了:

sudo apt install mysql-client

笔记:虽然您已经删除了 MySQL 服务器,但仍有一些文件残留在 中/var/lib/mysql。这些文件是卸载后故意留下的。如果您不需要任何以前可用的数据库,请随意删除此目录。您可能还会/etc/mysql保留一些文件,如果您不再需要配置文件,也可以将其删除。


从 Ubuntu(18.04 及更高版本)清除 MySQL

如果 MySQL 拒绝正常运行,那么您可能需要按照以下步骤将其从系统中彻底清除:

  1. 打开终端(如果尚未打开)
  2. 确保 MySQL 进程已停止(即使它没有运行):
    sudo systemctl stop mysqld
    
  3. 从系统中清除 MySQL 软件包:
    sudo apt purge mysql-server mysql-common mysql-server-core-* mysql-client-core-*
    
    并且,为了完整起见,让我们确保系统上没有安装 MariaDB:
    sudo apt purge mariadb-server 
    
  4. 检查是否有任何剩余的包:
    sudo dpkg -l | grep mysql
    
    理想情况下,您将获得零结果。但是,如果仍安装有任何东西,您可能会看到类似以下内容:
    ii  libmysqlclient21:amd64                     8.0.25-0ubuntu0.20.04.1                    amd64        MySQL database client library
    ii  php-mysql                                  2:7.4+75                                   all          MySQL module for PHP [default]
    ii  php7.4-mysql                               7.4.3-4ubuntu2.4                           amd64        MySQL module for PHP
    
    如果您确实看到了值,apt purge请将它们从系统中移除:
    sudo apt purge php-mysql php7.4-mysql libmysqlclient21
    
    对 MariaDB 执行相同操作:
    sudo dpkg -l | grep mariadb
    
  5. 清理 MySQL 目录的文件系统(MariaDB 也使用这个目录):
    sudo rm -rf /var/lib/mysql/
    sudo rm -rf /etc/mysql/
    sudo rm -rf /var/log/mysql
    
    仔细检查并清理这些文件:
    sudo find / -iname 'mysql*' -exec rm -rf {} \;
    
    重要的:此命令将从mysql您的系统中删除任何以 开头的文件,而无需确认。请务必小心谨慎地使用此命令。
  6. 删除 MySQL 用户帐户和组:
    sudo deluser --remove-home mysql
    sudo delgroup mysql
    
    如果您无法删除该组,请检查是否有其他用户帐户属于 MySQL 组:
    less /etc/passwd
    
    如果发现,则将该用户从组中删除,然后delgroup再一次。
  7. 删除可能用于安装特定 MySQL 版本的任何第三方 PPA
  8. 更新您的源列表:
    sudo apt autoremove -y
    sudo apt autoclean
    
  9. 喝杯咖啡,因为该休息一下了☕️

只要您的机器上没有配置某种 XAMPP 系统,这应该会完全消除您机器中的数据库引擎。

相关内容