简体   繁体   English

连接到 mongodb Atlas 时出现此错误:“dns.exception.Timeout:DNS 操作在 30.000985383987427 秒后超时”

[英]i am getting this error while connecting to mongodb Atlas: "dns.exception.Timeout: The DNS operation timed out after 30.000985383987427 seconds"

i am connecting my Flask app to mongodb atlas using Flask-PyMongo, but i am getting this error.我正在使用 Flask-PyMongo 将我的 Flask 应用程序连接到 mongodb 图集,但出现此错误。

"dns.exception.Timeout: The DNS operation timed out after 30.000985383987427 seconds" “dns.exception.Timeout:DNS 操作在 30.000985383987427 秒后超时”

and after that it says: During handling of the above exception, another exception occurred:之后它说:在处理上述异常期间,发生了另一个异常:

"pymongo.errors.ConfigurationError: The DNS operation timed out after 30.000985383987427 seconds" “pymongo.errors.ConfigurationError:DNS 操作在 30.000985383987427 秒后超时”

Here's the code:这是代码:

from flask import Flask 
from flask_pymongo import PyMongo

app = Flask(__name__)

app.config['MONGO_DBNAME'] = 'FirstCluster'
app.config['MONGO_URI'] = 'mongodb+srv://username:password@firstcluster-bblvc.mongodb.net/test?retryWrites=true'


mongo = PyMongo(app)

@app.route('/connect')
def connect_to_mongo():
    return 'Connecting to Mongodb'

@app.route('/collections')
def adding():
    user = mongo.db.users
    user.insert({'name' : 'vatsalay'})
    return 'Added User!'


if __name__ == '__main__':
    app.run(debug=True)

Try using a different connection string.尝试使用不同的连接字符串。 I was having problems with the 3.6 or later string.我在使用 3.6 或更高版本的字符串时遇到了问题。 Simply used the 3.4 or later string and it connected instantly.只需使用 3.4 或更高版本的字符串即可立即连接。

if you insist on using a newer connection string, you need to install pymongo srv如果您坚持使用较新的连接字符串,则需要安装 pymongo srv

For Windows对于 Windows

pip install pymongo[srv]

For Mac对于 Mac

pip3 install pymongo[srv]

On zsh在 zsh 上

pip3 install 'pymongo[srv]'

Looks like you have to install pymongo[srv] to get mongodb+srv URI to work.看起来您必须安装 pymongo[srv] 才能使mongodb+srv URI 工作。
Try installing it with pip3 install 'pymongo[srv]'尝试使用pip3 install 'pymongo[srv]'安装它

Had the same issue, it worked for me by changing my dns from automatically to "8.8.8.8".有同样的问题,它通过将我的 dns 从自动更改为“8.8.8.8”对我有用。 How?如何? If you're on Windows, do the following如果您使用的是 Windows,请执行以下操作

  1. Open Network and internet setting打开网络和互联网设置
  2. Network and sharing center网络和共享中心
  3. Press on your internet connection Wi-fi(name of your router)按下您的互联网连接 Wi-fi(您的路由器名称)
  4. Go for proprieties去取礼
  5. Choose tcp/ipv4 and go to proprieties again选择tcp/ipv4,再次进入proprieties
  6. Choose this one and edit it "use the following dns server and choose" 8.8.8.8 (google dns)选择这个并编辑它“使用以下dns服务器并选择”8.8.8.8(google dns)

Also as Neil Lunn said check the allowed ips也正如尼尔伦所说的检查允许的 ips

It might be an error caused while your Mongo Client is unable to find the connection to the server.这可能是由于您的 Mongo 客户端无法找到与服务器的连接而导致的错误。 So, better check you internet connectivity and then run the program所以,最好检查你的互联网连接,然后运行程序

Same problem in Google App Engine standard. Google App Engine 标准中存在同样的问题。

In my case was solution to use older type of URL:就我而言,是使用旧类型 URL 的解决方案:

client = pymongo.MongoClient("mongodb://USERNAME:PASSWORD@CLUSTER...

SW:西南:

  • Ubuntu 18.04 Ubuntu 18.04
  • Python 2.7.17 / 2.7.12蟒蛇 2.7.17 / 2.7.12
  • Pymongo 3.11.1派蒙戈 3.11.1
  • Google Cloud SDK 319.0.0谷歌云 SDK 319.0.0

Connection from test file directly from os was OK, but same code run from Google SDK dev appserver2 faile.直接从 os 连接测试文件没问题,但从 Google SDK dev appserver2 运行相同的代码失败。

After change URL generated by cloud.mongodb.com Atlas in section Cluster -> Connect -> Choose a connection method -> Python - 3.4 or later connection was established.更改 cloud.mongodb.com Atlas 在 Cluster -> Connect -> Choose a connection method -> Python 部分生成的 URL 后 - 3.4 或更高版本的连接已建立。

I was using python 3.6.2, so I chose to use the connection string for "3.6 or later".我使用的是 python 3.6.2,所以我选择使用“3.6 或更高版本”的连接字符串。 It gave me this error.它给了我这个错误。 Later I tried it with the connection string for "3.4 or later".后来我用“3.4 或更高版本”的连接字符串尝试了它。 It worked perfectly fine.它工作得很好。

Try using the connection string for "3.4 or later".尝试使用“3.4 或更高版本”的连接字符串。

I had a similar issue.我有一个类似的问题。 I suddenly couldn't connect.突然连接不上了All my colleagues were using the same python version etc.我所有的同事都使用相同的 python 版本等。

A few days before another colleague had the timeout whilst using their phones wifi.几天前,另一位同事在使用手机 wifi 时超时。 They could connect when they switched to their normal wifi.当他们切换到正常的 wifi 时,他们可以连接。

After I rebooted my wifi I was able to connect.重新启动wifi后,我就可以连接了。

It's not a great solution but it's worth trying.这不是一个很好的解决方案,但值得一试。

Try to use different version of connection string.尝试使用不同版本的连接字符串。 You can change the version in cluster -> connect -> connect your application.您可以在集群 -> 连接 -> 连接您的应用程序中更改版本。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我收到此错误在 cygwin 而不是 windows 终端中运行我的应用程序时,DNS 操作在 20.000144004821777 秒后超时 - I am getting this error The DNS operation timed out after 20.000144004821777 seconds when running my app in cygwin instead of my windows terminal 从容器连接到 Atlas mongoDB 的超时错误 - Timeout error connecting to Atlas mongoDB from a container PyDNS和DNS超时错误 - PyDNS & DNS timeout error AWS Lambda 检查端口是否在 DNS 名称上打开时出现超时错误 - AWS Lambda timed out error for checking if a port is open on a DNS name 一个独特的URLError: <urlopen error timed out> 我得到的错误 - A unique URLError: <urlopen error timed out> Error I am getting 安装python模块时出现错误“socket.timeout:读取操作超时” - error "socket.timeout: The read operation timed out" while installing a python module 当我尝试回答我自己的 dns 请求时,为什么会出现此错误? - Why am i getting this error when i try to answer my own dns request? 在Jupyter Notebook中连接到SQL DB时出现错误 - I am getting an error while connecting to an sql DB in Jupyter Notebook 通过地图集连接到 mongodb 的 Pymongo 错误 - Pymongo error connecting to mongodb via atlas 我收到“执行超时”错误? - I'm getting an "Execution Timed Out" error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM