MongoDB:连接到 MongoDB 的 resolv.conf 的 DNS 问题 [英] MongoDB: DNS issue of resolv.conf connecting to MongoDB

查看:29
本文介绍了MongoDB:连接到 MongoDB 的 resolv.conf 的 DNS 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 MongoDB Atlas 导出一些数据.

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

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

注意:如果您从 Windows CMD 运行此命令,到1.1.1.1连接成功,见下图:

# 这个文件由 man:systemd-resolved(8) 管理.不要编辑.## 这是一个动态的 resolv.conf 文件,用于将本地客户端连接到# systemd-resolved 的内部 DNS 存根解析器.该文件列出了所有# 配置的搜索域.## 运行systemd-resolve --status"查看上行DNS服务器的详细信息# 目前正在使用中.## 第三方程序不能直接访问这个文件,只能通过#/etc/resolv.conf 中的符号链接.以不同的方式管理 man:resolv.conf(5),# 用静态文件或不同的符号链接替换这个符号链接.## 有关支持的模式的详细信息,请参见 man:systemd-resolved.service(8)# 操作/etc/resolv.conf.名称服务器 1.1.1.1选项 edns0搜索名称.com

连接成功,如下图:

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

一切似乎都在领先,同样根据这个来源 以及 此处 到 DNS通过终端连接到 MongoDB 以导出集合时出现问题.现在从上一篇文章开始,出于多种原因,不建议手动更改此地址,因此在使用 DNS 1.1.1.1 成功导出数据后,我将其改回原来的 DNS 127.0.0.53.但是,我认为这不应该是一种正确的行为,因为每次我需要导出数据时,我都必须不断地手动更改此地址.

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

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

解决方案

在您提到的链接中,您似乎已经准备好了答案.我将总结一下:

安装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 服务器:8.8.8.8DNS 域:sa-east-1.compute.internalDNSSEC NTA:10.in-addr.arpa16.172.in-addr.arpa

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

I want to export some data from MongoDB Atlas.

If I execute the command below, It tries connect localhost and export the data.

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

Note: If you run this command from Windows CMD, it works fine

After researching the problem and with the help of a user, everything seems to point to a DNS problem and to the related resolv.conf file.

Below the original /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

At the beginning that resulted into a connection failure as shown below:

But if I would change that address into the following public available address according to what advised on this post to 1.1.1.1 the connection is successful, see below:

# 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

Which resulted into a connection success as shown below:

HOWEVER the problem is that instead of explicitly connecting to the name of the MongoDB cluster, it will connect to the localhost, which is very strange as I successfully exported the files I was looking for from the real connection. Which means that the machine was correctly connecting to the database but via localhost.

Everything seems to lead, also according to this source and also here to a DNS problem while connecting to MongoDB via terminal to export collections. Now from this last post it not advisable to manually change this address for several reasons, therefore right after successfully exporting the data using DNS 1.1.1.1 I changed it back to its original DNS 127.0.0.53. However I don't think this should be a proper behavior as every time I need to export data I will have to continuously and manually change this address.

What could be the reason for this strange behavior? And therefore what could be a long term solution without manually switching between DNS addresses?

Thanks for pointing to the right direction for solving this issue.

解决方案

It seems you all ready have the answer in the links you mentioned. I will summarize this:

Install resolvconf (for Ubuntu apt install resolvconf), add the line nameserver 8.8.8.8 to /etc/resolvconf/resolv.conf.d/base, then run sudo resolvconf -u and to be sure service resolvconf restart. To verify run systemd-resolve --status.

You should see on the first line your DNS server like here:

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

This solution persists between reboots.

这篇关于MongoDB:连接到 MongoDB 的 resolv.conf 的 DNS 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆