Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:[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)
like image 654
vatsalay Avatar asked Mar 24 '19 11:03

vatsalay


Video Answer


1 Answers

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]'
like image 50
unltd_J Avatar answered Sep 20 '22 20:09

unltd_J