I am trying to connect my code to atlas mongo db but i get the error below,this is my code :
from pymongo import MongoClient
client = MongoClient("mongodb+srv://username:test#cluster0.yntdf.mongodb.net/test?
retryWrites=true&w=majority")
db = client["test"]
collection = db["test"]
collection.insert_one({"_id":0, "name": "hello", "score": 5})
i got the error:
ConfigurationError: A DNS label is empty.
Anyone know how to handle this error? I installed dnspython and pymongo
Your connection string is wrong. Double check it is correct and also the IP of the calling server is whitelisted.
I got the same issue, the following code works for me with python 3.9.1
import pymongo
import urllib
MONGODB_USERNAME = urllib.parse.quote_plus('test')
MONGODB_PASSWORD = urllib.parse.quote_plus('tset#000')
MONGODB_DATABASE = 'sampledb'
MONGODB_URL = "mongodb://"+MONGODB_USERNAME+":"+MONGODB_PASSWORD+"#cluster0.wq5js.mongodb.net/"+MONGODB_DATABASE+"?retryWrites=true&w=majority"
client = pymongo.MongoClient(MONGODB_URL)
Related
I am trying to connect with mongodb atlas from my flask app using flask-mongoengine.
DB_URI = "mongodb+srv://flask_app_user:flask_app_user#cluster0.6jwadcx5g.mongodb.net/flask_app?retryWrites=true&w=majority"
def create_app():
app = Flask(__name__)
app.secret_key = os.environ.get('SECRET_KEY', 'replace_me_32437264278642')
app.config['MONGODB_SETTINGS'] = {
'host': os.environ.get('MONGODB_URI', DB_URI)
}
MongoEngine(app)
socketio.init_app(app)
SSLify(app)
return app
But I am getting an error,
pymongo.errors.InvalidURI: Invalid URI scheme: URI must begin with 'mongodb://'
How can I use mongo atlas with flask_mongoengine?
I don't want to stick with flask_mongoengine. I don't want to change that.
It worked correctly for me with the latest version flask_mongoengine-1.0.0 and pymongo-3.11.2
It seems you're using the host from MONGODB_URI env var...
What do you have in MONGODB_URI??
Could you share also which version are you using?
I'm currently working on a simple python CRUD script to check MongoDB out. Turns out I'm liking it a lot, but I have found myself being unable to work with MongoDB transactions. Everytime I try to start a transaction an exception is thrown saying:
This MongoDB deployment does not support retryable writes. Please add retryWrites=false to your connection string.
And, eventhough I've already added that option to my connection string:
self._client = MongoClient('mongodb://localhost/?retryWrites=false')
self._db = self._client.workouts
self._collection = self._db.workouts
That error is still popping up when running the following lines of code:
with self._client.start_session() as s:
with s.start_transaction():
self._collection.delete_one({'_id': id}, session=s)
next = self._collection.find_one({'_id': next_id}, session=s)
return next
What can I do?
I'm running python 3.7.3, pymongo 3.9.0 and MongoDB 4.0.12.
I do have a database named users but pymongo is looking for a db named as app is there any way to change to Pymongo connection method ?
this one is working if i crate a db named as app;
app = Flask(__name__)
mongo = PyMongo(app)
i want to connect like this but i do get an error for that ;
mongo = PyMongo('users')
I have found my answer;
client = MongoClient('localhost')
db = client['dbname']
collection = db.collection_name
app = Flask(__name__)
app.config['MONGO_DBNAME'] = 'dbname'
mongo = PyMongo(app)
This allows you to use flask_pymongo, more info here
I have a MongoDB database hosted on mlab and I would like to use PyMODM as my object modeling library.
This is my code so far:
from pymodm import connect, MongoModel, fields
connect = connect('mongodb://user:pass#ds119788.mlab.com/db')
class Test(MongoModel):
user = fields.CharField()
if __name__ == "__main__":
test = Test("test")
test.save()
But it gives me this error :
pymongo.errors.ServerSelectionTimeoutError: ds119788.mlab.com:27017: [Errno 61] Connection refused
Am I missing something?
You need to use the MongoDB URI provided by mlab for your account. The URI should contain the port number to connect to.
For example, it should look like :
connect = connect('mongodb://user:password#ds119788.mlab.com:63123/databaseName')
I created the following mongo replica sets by using mongo cli:
> config = { _id:"repset", members:[{_id:0,host:"192.168.0.1:27017"},{_id:1,host:"192.168.0.2:27017"},{_id:2,host:"192.168.0.3:27017"}]}
> rs.initiate(config);
All the mongo servers run properly.
>>> import pymongo
>>> from pymongo import MongoClient
>>> servers = ["192.168.0.1:27017", "192.168.0.2:27017", "192.168.0.3:27017"]
>>> MongoClient(servers)
>>> xc = MongoClient()
>>> print xc
MongoClient('localhost', 27017)
>>> print xc.database_names()
[u'test_repsets', u'local', u'admin', u'test']
After I kill the local mongodb server, it shows me connection timeout error:
pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused
It seems there is no auto fail over, although I defined the mongodb servers.
I am wondering if pymongo handles fail over automatically, or how this situation is handled properly?
Thank you in advance.
in Pymongo 3.x you may want to explicitly state what replica set you are connecting to. I know Pymongo 3.x switched up some of the ways it handles being given an array of servers. I got this off the Pymongo API about connections to replicas and auto failover
In your code :
MongoClient(servers)
Above line is not assigned to any variable. It should assign to variable (in your case you again created instance which causes error.)
Please add following line
>>> #MongoClient(servers) # remove this line
>>> #xc = MongoClient() # remove this line
>>> xc = MongoClient(servers) # add this line