I get an OperationFailure, when I want to save something to the db using MongoEngine.
from mongoengine import connect, Document, StringField
connect(db, host="mongodb://" + username + ":" + password + "@localhost:" + str(port))
class Post(Document):
title = StringField()
p = Post(title="Test")
p.save()
File "test_db.py", line 16, in myfunction
p.save()
File ".../venv/lib/python3.6/site-packages/mongoengine/document.py", line 373, in save
self.ensure_indexes()
File ".../venv/lib/python3.6/site-packages/mongoengine/document.py", line 856, in ensure_indexes
collection = cls._get_collection()
File ".../venv/lib/python3.6/site-packages/mongoengine/document.py", line 190, in _get_collection
db = cls._get_db()
File ".../venv/lib/python3.6/site-packages/mongoengine/document.py", line 179, in _get_db
return get_db(cls._meta.get('db_alias', DEFAULT_CONNECTION_NAME))
File ".../venv/lib/python3.6/site-packages/mongoengine/connection.py", line 236, in get_db
db.authenticate(conn_settings['username'], conn_settings['password'], **auth_kwargs)
File ".../venv/lib/python3.6/site-packages/pymongo/database.py", line 1274, in authenticate
connect=True)
File ".../venv/lib/python3.6/site-packages/pymongo/mongo_client.py", line 614, in _cache_credentials
sock_info.authenticate(credentials)
File ".../venv/lib/python3.6/site-packages/pymongo/pool.py", line 688, in authenticate
auth.authenticate(credentials, self)
File ".../venv/lib/python3.6/site-packages/pymongo/auth.py", line 563, in authenticate
auth_func(credentials, sock_info)
File "..../venv/lib/python3.6/site-packages/pymongo/auth.py", line 542, in _authenticate_default
return _authenticate_mongo_cr(credentials, sock_info)
File ".../venv/lib/python3.6/site-packages/pymongo/auth.py", line 524, in _authenticate_mongo_cr
sock_info.command(source, query)
File ".../venv/lib/python3.6/site-packages/pymongo/pool.py", line 579, in command
unacknowledged=unacknowledged)
File ".../venv/lib/python3.6/site-packages/pymongo/network.py", line 150, in command
parse_write_concern_error=parse_write_concern_error)
File ".../venv/lib/python3.6/site-packages/pymongo/helpers.py", line 155, in _check_command_response
raise OperationFailure(msg % errmsg, code, response)
pymongo.errors.OperationFailure: auth failed
But if I do exactly the same thing using pymongo, I don't get any error.
from pymongo import MongoClient
client = MongoClient("mongodb://" + username + ":" + password + "@localhost:" + str(port))
collection = client[db].post
collection.insert_one({"title": "Test"})
I can't find out, what I am doing wrong.
This is the version of MongoDB:
$ mongod --version
db version v2.6.12
2018-07-30T11:10:08.130+0200 git version: d73c92b1c85703828b55c2916a5dd4ad46535f6a
An the version of MongoEngine
mongoengine==0.15.3
I had to append /?authSource=admin' to the end to the URI. Now the function call looks like that:
connect(db, host="mongodb://" + username + ":" + password + "@localhost:" + str(port) + '/?authSource=admin')
I think cause version of pymongo and mongoengine
I had to append
/?authSource=admin'to the end to the URI. Now the function call looks like that:connect(db, host="mongodb://" + username + ":" + password + "@localhost:" + str(port) + '/?authSource=admin')
Thanks for the tip - used this to resolve an issue I had with Celery using the Mongo backend. It seemed to attempt authenticating the user I provided against the 'admin' db.
I had to append
/?authSource=admin'to the end to the URI. Now the function call looks like that:connect(db, host="mongodb://" + username + ":" + password + "@localhost:" + str(port) + '/?authSource=admin')
You saved my day....! Thank you.! 馃檹
Most helpful comment
I had to append
/?authSource=admin'to the end to the URI. Now the function call looks like that:Reference