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)
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
Also as Neil Lunn said check the allowed ips
Looks like you have to install pymongo[srv] to get mongodb+srv URI to work.
Try installing it with pip3 install 'pymongo[srv]'
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]'
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
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:
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.
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".
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.
Try to use different version of connection string. You can change the version in cluster -> connect -> connect your application.