Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

1K
Views
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.

"dns.exception.Timeout: The DNS operation timed out after 30.000985383987427 seconds"

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"

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)
over 4 years ago · Santiago Trujillo
8 answers
Answer question

0

Had the same issue, it worked for me by changing my dns from automatically to "8.8.8.8". How? If you're on Windows, do the following

  1. Open Network and internet setting
  2. Network and sharing center
  3. Press on your internet connection Wi-fi(name of your router)
  4. Go for proprieties
  5. Choose tcp/ipv4 and go to proprieties again
  6. Choose this one and edit it "use the following dns server and choose" 8.8.8.8 (google dns)

Also as Neil Lunn said check the allowed ips

over 4 years ago · Santiago Trujillo Report

0

Looks like you have to install pymongo[srv] to get mongodb+srv URI to work.
Try installing it with pip3 install 'pymongo[srv]'

over 4 years ago · Santiago Trujillo Report

0

Try using a different connection string. I was having problems with the 3.6 or later string. Simply used the 3.4 or later string and it connected instantly.

if you insist on using a newer connection string, you need to install pymongo srv

For Windows

pip install pymongo[srv]

For Mac

pip3 install pymongo[srv]

On zsh

pip3 install 'pymongo[srv]'
over 4 years ago · Santiago Trujillo Report

0

It might be an error caused while your Mongo Client is unable to find the connection to the server. So, better check you internet connectivity and then run the program

over 4 years ago · Santiago Trujillo Report

0

Same problem in Google App Engine standard.

In my case was solution to use older type of URL:

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

SW:

  • Ubuntu 18.04
  • Python 2.7.17 / 2.7.12
  • Pymongo 3.11.1
  • Google Cloud SDK 319.0.0

Connection from test file directly from os was OK, but same code run from Google SDK dev appserver2 faile.

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.

over 4 years ago · Santiago Trujillo Report

0

I was using python 3.6.2, so I chose to use the connection string for "3.6 or later". It gave me this error. Later I tried it with the connection string for "3.4 or later". It worked perfectly fine.

Try using the connection string for "3.4 or later".

over 4 years ago · Santiago Trujillo Report

0

I had a similar issue. I suddenly couldn't connect. All my colleagues were using the same python version etc.

A few days before another colleague had the timeout whilst using their phones wifi. They could connect when they switched to their normal wifi.

After I rebooted my wifi I was able to connect.

It's not a great solution but it's worth trying.

over 4 years ago · Santiago Trujillo Report

0

Try to use different version of connection string. You can change the version in cluster -> connect -> connect your application.

enter image description here

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!