MongoDB:resolv.conf 连接到 MongoDB 的 DNS 问题

MongoDB: DNS issue of resolv.conf connecting to MongoDB

我想从 MongoDB Atlas 中导出一些数据。

如果我执行下面的命令,它会尝试连接 localhost 并导出数据。

mongoexport --uri="mongodb+srv://<username>:<password>@name-of-project-x2lpw.mongodb.net/test" --collection users --out /tmp/testusers.json

注意:如果你从WindowsCMD运行这个命令,

在研究问题并在用户的帮助下,一切似乎都指向 DNS 问题和相关的 resolv.conf 文件。

/etc/resolv.conf以下:

# This file is managed by man:systemd-resolved(8). Do not edit.
#
# This is a dynamic resolv.conf file for connecting local clients to the
# internal DNS stub resolver of systemd-resolved. This file lists all
# configured search domains.
#
# Run "systemd-resolve --status" to see details about the uplink DNS servers
# currently in use.
#
# Third party programs must not access this file directly, but only through the
# symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,
# replace this symlink by a static file or a different symlink.
#
# See man:systemd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.

nameserver 127.0.0.53
options edns0
search name.com

一开始导致连接失败,如下图:

但是,如果我根据 this post1.1.1.1 上的建议将该地址更改为以下 public 可用地址,则连接成功,请参见下文:

# This file is managed by man:systemd-resolved(8). Do not edit.
#
# This is a dynamic resolv.conf file for connecting local clients to the
# internal DNS stub resolver of systemd-resolved. This file lists all
# configured search domains.
#
# Run "systemd-resolve --status" to see details about the uplink DNS servers
# currently in use.
#
# Third party programs must not access this file directly, but only through the
# symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,
# replace this symlink by a static file or a different symlink.
#
# See man:systemd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.

nameserver 1.1.1.1
options edns0
search name.com

这导致连接成功,如下所示:

然而 问题是,它不会显式连接到 MongoDB 集群的名称,而是连接到 localhost,这是非常很奇怪,因为我成功地从真实连接中导出了我正在寻找的文件。 这意味着机器正确连接到数据库,但是通过localhost

在通过终端连接到 MongoDB 以导出集合时,一切似乎都导致了 this source and also here 的 DNS 问题。 现在从最后一个 post 开始,出于多种原因不建议手动更改此地址,因此在使用 DNS 1.1.1.1 成功导出数据后,我立即将其改回其原始 DNS 127.0.0.53。 但是,我认为这不应该是一种正确的行为,因为每次我需要导出数据时,我都必须不断地手动更改此地址。

这种奇怪行为的原因可能是什么?因此,如果不在 DNS 地址之间手动切换,什么是长期解决方案?

感谢您指出解决此问题的正确方向。

看来您已经在您提到的链接中找到了答案。我会summarize this:

安装resolvconf(对于Ubuntu apt install resolvconf),将行nameserver 8.8.8.8添加到/etc/resolvconf/resolv.conf.d/base,然后是运行 sudo resolvconf -u当然service resolvconf restart。 验证 运行 systemd-resolve --status.

您应该会在第一行看到您的 DNS 服务器,如下所示:

         DNS Servers: 8.8.8.8
          DNS Domain: sa-east-1.compute.internal
          DNSSEC NTA: 10.in-addr.arpa
                      16.172.in-addr.arpa

此解决方案在重新启动后仍然存在。