云计算、AI、云原生、大数据等一站式技术学习平台

网站首页 > 教程文章 正文

Could not retrieve mirrorlist http://mirrorlist.centos.org/?rele

jxf315 2024-12-07 13:37:42 教程文章 55 ℃

一、背景

在虚拟机中安装好了CentOS,准备通过yum配置系统运行环境,但是执行yum命令时报错了,导致后续的环境配置无法继续;

[root@localhost mysql]# yum clean all
已加载插件:fastestmirror
正在清理软件源: base extras updates
Cleaning up everything
Maybe you want: rm -rf /var/cache/yum, to also free up space taken by orphaned data from disabled or removed repos
[root@localhost mysql]# yum makecache
已加载插件:fastestmirror
Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=stock error was
14: curl#6 - "Could not resolve host: mirrorlist.centos.org; 未知的名称或服务"


 One of the configured repositories failed (未知),
 and yum doesn't have enough cached data to continue. At this point the only
 safe thing yum can do is fail. There are a few ways to work "fix" this:

     1. Contact the upstream for the repository and get them to fix the problem.

     2. Reconfigure the baseurl/etc. for the repository, to point to a working
        upstream. This is most often useful if you are using a newer
        distribution release than is supported by the repository (and the
        packages for the previous distribution release still work).

     3. Run the command with the repository temporarily disabled
            yum --disablerepo=<repoid> ...

     4. Disable the repository permanently, so yum won't use it by default. Yum
        will then just ignore the repository until you permanently enable it
        again or use --enablerepo for temporary usage:

            yum-config-manager --disable <repoid>
        or
            subscription-manager repos --disable=<repoid>

     5. Configure the failing repository to be skipped, if it is unavailable.
        Note that yum will try to contact the repo. when it runs most commands,
        so will have to try and fail each time (and thus. yum will be be much
        slower). If it is a very temporary problem though, this is often a nice
        compromise:

            yum-config-manager --save --setopt=<repoid>.skip_if_unavailable=true

Cannot find a valid baseurl for repo: base/7/x86_64

从这命令可以看出来,http://mirrorlist.centos.org/挂掉了,使用命令去访问一下,会发现服务一直报403错误,这是服务器的问题,可以理解为挂了。因为centOS在今年6.30后就不再维护了。

二、解决方案

既然国外的yum源不好用了,那就替换为国内的,我这里使用的是阿里的。

更改 CentOS 7 系统的软件仓库源,从官方的 CentOS 镜像源切换到阿里云的 CentOS 7 镜像源,以获得更快的下载速度和更好的稳定性。

三、解决步骤

注:我这里是已经配置好虚拟机网络,可以ping通百度(www.baidu.com)的网址了.,如果ping不通的需要将网络配置好了之后才能继续下一步,这里我就不说了,百度一下就好了。

1、/etc/yum.repos.d/ 文件下存放着系统的cent源文件,先来查看一下

2、打开看一下最核心的CentOS-Base.repo文件

可以看到,万恶之源就在开头的两行:

mirrorlist=http://mirrorlist.centos.org/?release=KaTeX parse error: Expected 'EOF', got '&' at position 11: releasever&?arch=basearch&repo=os&infra=KaTeX parse error: Expected 'EOF', got '#' at position 7: infra #?baseurl=http://…releasever/os/$basearch/

  • mirrorlist顾名思义,是一个列表,是一个动态的镜像列表,提供了多个镜像由系统调用。
  • baseurl后是一个静态的 URL 。

我们这里希望只用阿里的镜像yum源,因此,要修改的就是baseurl,而必须注释掉mirrorlist。

3、下载阿里云 CentOS 7 镜像源配置文件

命令:sudo curl -o /etc/yum.repos.d/Centos-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo

【注意】:这里只能使用curl,或者vi直接修改,不能使用wget,因为wget也需要yum来安装,现在的问题就是yum命令无法使用。

【注意】第一个才是我们阿里的,下面的是系统自带的。

下方为阿里云 CentOS 7 镜像源配置文件内容

4、删除除阿里外的所有cent源文件

文件夹中有很多CentOS-开头的文件,它们其实是把阿里镜像中的东西按照功能给切分了。虽然我们替换了base文件,但yum命令还是会使用到其他的文件,比如update,下载,保存等,都会使用到系统自带的源,因此依旧会出现无法连接的问题。

最简单的办法,就是删除所有的自带的源,只保留阿里的。

命令:find . -maxdepth 1 -type f -name 'CentOS-*' -exec rm {} \;

5、清除旧的 YUM 缓存

命令:sudo yum clean all

6、创建新的 YUM 缓存

命令:sudo yum makecache

这一步是为了让 YUM 根据新的仓库配置文件创建新的缓存,这样在执行 yum update 或 yum install 命令时,系统会从阿里云的镜像源下载软件包。

7、验证

命令:yum install wget

命令执行成功!!说明我们已经成功解决了。

最近发表
标签列表