解决【Could not retrieve mirrorlist】问题及YUM镜像源配置

前言

使用yum安装软件时始终无法找到镜像源,导致下载失败

1
2
Error downloading packages:
keepalived-1.3.5-19.el7.x86_64: [Errno 256] No more mirrors to try.

尝试更改镜像源,结果不生效而且恢复配置文件后仍提示如下错误

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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

处理措施

正常情况下,我们不需要单独配置yum镜像源,因为yum在安装时会自动检测距离我们最近的镜像节点

对于 CentOS 系统,你可以通过编辑 /etc/yum.repos.d/ 目录下的 .repo 文件来更改或添加软件包下载的镜像站点。以下是如何配置镜像站点的具体步骤:

1. 备份现有的 .repo 文件

在进行任何修改之前,建议先备份现有的仓库配置文件。

1
sudo cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak

2. 编辑 .repo 文件

使用你喜欢的文本编辑器(如 vinano)打开仓库配置文件。这里以 CentOS-Base.repo 为例:

1
sudo vi /etc/yum.repos.d/CentOS-Base.repo

3. 更改或添加镜像源

在文件中,你会看到多个 [repository] 段落,每个段落代表一个不同的仓库。你需要为每个需要更改的仓库指定一个新的 baseurlmirrorlist

使用 baseurl

如果你知道一个具体的镜像站点,可以直接设置 baseurl。例如,使用阿里云的镜像:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[base]
name=CentOS-$releasever - Base
# mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra
baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

[updates]
name=CentOS-$releasever - Updates
# mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates&infra=$infra
baseurl=http://mirrors.aliyun.com/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

[extras]
name=CentOS-$releasever - Extras
# mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras&infra=$infra
baseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

使用 mirrorlist

如果你希望系统自动选择最快的镜像站点,可以使用 mirrorlist。这通常是一个包含多个镜像站点 URL 的列表,YUM 会从中选择最快的站点。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[base]
name=CentOS-$releasever - Base
mirrorlist=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

[updates]
name=CentOS-$releasever - Updates
mirrorlist=http://mirrors.aliyun.com/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

[extras]
name=CentOS-$releasever - Extras
mirrorlist=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

4. 清理缓存并更新仓库

保存并关闭编辑器后,清理 YUM 缓存并更新仓库信息。

1
2
sudo yum clean all
sudo yum makecache

5. 重新安装软件包

现在你可以尝试重新安装 keepalived 或其他软件包,看看问题是否已经解决。

1
sudo yum install -y keepalived

如果一切正常,你应该能够成功安装 keepalived 而不会遇到之前的错误。如果仍然有问题,请检查网络连接和防火墙设置,确保没有阻止访问新的镜像站点。

说明:我修改之后发现不生效,当然可能是我配置文件哪里粘错了,这个步骤整体来说是正确的,我尝试恢复默认配置时依旧提示原来的错误,这种情况下不应该用mirrorlist,而应该用baseurl,我是通过配置阿里云镜像解决的。

配置阿里云镜像

1、修改/etc/yum.repos.d/CentOS-Base.repo文件,修改前最好备份原始配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[base]
name=CentOS-$releasever - Base - Aliyun
baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

#released updates
[updates]
name=CentOS-$releasever - Updates - Aliyun
baseurl=http://mirrors.aliyun.com/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras - Aliyun
baseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus - Aliyun
baseurl=http://mirrors.aliyun.com/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

2、清理缓存并更新仓库

1
2
sudo yum clean all
sudo yum makecache

参考

详细图解解决 CentOS7 yum出现“Could not retrieve mirrorlist”的问题

liunx镜像配置