linux极简小知识:12、centos下推荐使用rpm查询软件包,及卸载移除某所有软件包和依赖包,对比yum list、yum search命令

2,280 阅读4分钟

小知识,大挑战!本文正在参与“程序员必备小知识”创作活动。

推荐使用 rpm -qa 查询软件包

之所以推荐使用 rpm 查询软件,是因为有些情况下,使用yum无法准确的查找。比如软件安装一半或者损坏、软件不是通过yum命令安装等,都可能导致yum查找不到。

如下,查询mysql相关的软件包,可以看到,rpm查询命名已经安装,但是yum下则无法查询到:

# rpm -qa|grep mysql
mysql-community-client-8.0.26-1.el7.x86_64
mysql-community-common-8.0.26-1.el7.x86_64
mysql80-community-release-el7-3.noarch
mysql-community-libs-8.0.26-1.el7.x86_64
mysql-community-client-plugins-8.0.26-1.el7.x86_64
mysql-community-server-8.0.26-1.el7.x86_64

而yum list则提示没有匹配的包:

# yum list mysql*
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Error: No matching Packages to list

当安装了某个包名的软件后,再次只用yum查询,似乎就不支持通配符了。

如下测试,yum list,只有使用包全名是才能列出来:

# yum list installed mysql-community-server-8.0.26-1.el7.x86_64
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Installed Packages
mysql-community-server.x86_64                                      8.0.26-1.el7                                      @mysql80-community
# yum list mysql-community-server-8.0.26-1.el7.x86_64
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Installed Packages
mysql-community-server.x86_64                                      8.0.26-1.el7                                      @mysql80-community

# yum list installed mysql*
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Error: No matching Packages to list
# yum list mysql*
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Error: No matching Packages to list

借助rpm和grep实现卸载包含某名称的所有软件包【推荐】

如下,通过 for i in $(rpm -qa|grep mysql);do rpm -e $i --nodeps;done 命令,卸载所有包含 mysql 的软件包。

# rpm -qa|grep mysql
mysql-community-client-8.0.26-1.el7.x86_64
mysql-community-common-8.0.26-1.el7.x86_64
mysql80-community-release-el7-3.noarch
mysql-community-libs-8.0.26-1.el7.x86_64
mysql-community-client-plugins-8.0.26-1.el7.x86_64
mysql-community-server-8.0.26-1.el7.x86_64
# for i in $(rpm -qa|grep mysql);do rpm -e $i --nodeps;done
# rpm -qa|grep mysql                           
# 

可以看到,所有 mysql 均已被删除。

for i in $(rpm -qa|grep mysql);do sudo yum remove -y $i;done命令,也是推荐的。

yum使用通配符(yum remove使用通配符卸载包含某名称的所有软件包)

看到一个借助 软件包名+通配符(*) 的形式,删除多个有同样字符名的软件的方式。

通过测试确实可以一次卸载多个软件。具体如下:

比如,卸载 mysql- 开头的所有软件

yum -y remove mysql-*

卸载过程如下截图所示:

可以看到,所有的 mysql- 均已卸载。

查看mysql的rpm软件:

[root@VM_0_15_centos ~]# rpm -qa|grep mysql
mysql80-community-release-el7-3.noarch
[root@VM_0_15_centos ~]#

同理,其他的 yum 命令也可以使用 * 通配符来查找匹配

如,yum list mysql* 列出所有可安装的mysql软件包,这是在卸载了所有包含 mysql 的软件报名之后的查询结果,否则查询将提示没有匹配的包

# yum list mysql*
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Available Packages
MySQL-python.x86_64                     1.2.5-1.el7                         os
MySQL-zrm.noarch                        3.0-17.el7                          epel
mysql++.x86_64                          3.1.0-12.el7                        epel
mysql++-devel.x86_64                    3.1.0-12.el7                        epel
mysql++-manuals.x86_64                  3.1.0-12.el7                        epel
mysql-connector-java.noarch             1:5.1.25-3.el7                      os
mysql-connector-odbc.x86_64             5.2.5-8.el7                         os
mysql-connector-python.noarch           1.1.6-1.el7                         epel
mysql-mmm.noarch                        2.2.1-15.el7                        epel
mysql-mmm-agent.noarch                  2.2.1-15.el7                        epel
mysql-mmm-monitor.noarch                2.2.1-15.el7                        epel
mysql-mmm-tools.noarch                  2.2.1-15.el7                        epel
mysql-proxy.x86_64                      0.8.5-2.el7                         epel
mysql-proxy-devel.x86_64                0.8.5-2.el7                         epel
mysql-utilities.noarch                  1.3.6-1.el7                         epel
mysqlreport.noarch                      3.5-11.el7                          epel
mysqltuner.noarch                       1.7.21-1.git.4e0a8b3.el7            epel
#

关于yum中【可能不支持使用通配符的问题】

不知是何种操作,后面对于执行 yum -y remove mysql-*yum list mysql*等软件包名通配符的形式,均不支持。即使在使用rpm能查询到的情况下,yum也会提示没有匹配的包。

# yum list mysql*
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Error: No matching Packages to list
# yum list updates mysql
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Error: No matching Packages to list
# yum list installed mysql*
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Error: No matching Packages to list

比如上面同样的移除命令,即使rpm -q查询存在,仍提示没有匹配的包。

# yum -y remove mysql-*
Loaded plugins: fastestmirror, langpacks
No Match for argument: mysql-community-server-8.0.26-1.el7.x86_64.rpm
No Packages marked for removal
# rpm -qa|grep mysql
mysql-community-common-8.0.26-1.el7.x86_64
mysql-community-libs-8.0.26-1.el7.x86_64
mysql-community-client-plugins-8.0.26-1.el7.x86_64
mysql-community-server-8.0.26-1.el7.x86_64
mysql-community-client-8.0.26-1.el7.x86_64
mysql80-community-release-el7-3.noarch
#

注:这个问题,至少在 mysql* 中遇到了。其他的软件包并为一一测试,不过测试 pam* 似乎没问题。

不知道是否是和执行过yum remove、安装了mysql软件等有关。

关于yum listyum list installed命令

这两个软件用于列出yum仓库包含的所有软件包列出已安装的所有软件包

指定包全名

yum不支持通配符,但是可以在yum listyum list installed命令后,指定包全名查看,此时,对于已安装的包,两者命令是相同的:

# yum list installed mysql-community-server-8.0.26-1.el7.x86_64
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Installed Packages
mysql-community-server.x86_64                                      8.0.26-1.el7                                      @mysql80-community

# yum list mysql-community-server-8.0.26-1.el7.x86_64
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Installed Packages
mysql-community-server.x86_64                                      8.0.26-1.el7                                      @mysql80-community

yum listyum list installed列出所有可安装和已安装的软件包

  • yum list —— 列出所有可安装的软件包,包括已安装和未安装的。

  • yum list installed —— 列出所有已安装的软件包。

直接执行这两个命令即可。

结合grep命令

# yum list installed | grep mysql
mysql-community-client.x86_64           8.0.26-1.el7                   @mysql80-community
mysql-community-client-plugins.x86_64   8.0.26-1.el7                   @mysql80-community
mysql-community-common.x86_64           8.0.26-1.el7                   @mysql80-community
mysql-community-libs.x86_64             8.0.26-1.el7                   @mysql80-community
mysql-community-server.x86_64           8.0.26-1.el7                   @mysql80-community
mysql80-community-release.noarch        el7-3                          installed

如下,可以看到,yum list | grep mysql包含yum list installed | grep mysql的已安装的软件包: