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:[email protected]/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. 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]'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With