首页 > 解决方案 > MongoDB:resolv.conf 连接到 MongoDB 的 DNS 问题

问题描述

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

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

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

注意:如果您从 Windows CMD 运行此命令,它可以正常工作

在研究了问题并在用户的帮助下,一切似乎都指向 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

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

错误的

但是,如果我根据这篇文章中的建议将该地址更改为以下公共可用地址,1.1.1.1则连接成功,请参见下文:

# 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通过终端连接到导出集合时也出现了 DNS 问题。现在从最后一篇文章来看,出于多种原因,不建议手动更改此地址,因此在成功导出数据后,DNS 1.1.1.1我将其更改回原来的DNS 127.0.0.53. 但是我认为这不应该是一个正确的行为,因为每次我需要导出数据时,我都必须不断地手动更改这个地址。

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

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

标签: mongodbdnsubuntu-18.04

解决方案


看来你们都准备好在你提到的链接中找到答案了。我将总结这一点:

安装 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

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


推荐阅读