[![enter image description here][1]][1]Hi im trying to connect to mongoDB via a python code
client = MongoClient("mongodb://username:mypassword@localhost/mydatabase-1")
db = client['mydatabase-1']
and or
connection = MongoClient('localhost', 27017)
db = connection['mydatabase-1']
db.authenticate('username', 'mypassword')
uri = "mongodb://username:[email protected]/mydatabase-1"
client = MongoClient(uri)
but i keep getting the same error without fail
ConfigurationError: command SON([('authenticate', 1), ('user', u'CEI'), ('nonce', u'950483ef6634f14'), ('key', u'76f0102585a7571d96dace50d7aca5a6')]) failed: auth failed
I am using pymongo 2.7 with python 2.7. I guess this means I'm working with mechanism='MONGODB-CR'
I am able to use these credentials to connect to the MongoDB database using MongoChef (a GUI) and it works perfectly.
I have tried to update the pymongo, and it is at version 2.7 i cant use a higher version because I have python 2.7. I cant find any more solutions online. From what i read on the mongoDB website it should work with mechanism='MONGODB-CR' but not for mechanism='SCRAM-SHA-1' due to being v2.7
have mongodb running using
E:\MongoDB\bin\mongod.exe --auth --port 27017 --dbpath E:\Data\db
and set up the user using
use admin
db.createUser(
{
user: "username",
pwd: "mypassword",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
UPDATE!!!!!!!!!!!!!!!!!!!!!!!!!!
managed to get pymongo 3.4 working using pip install. But still getting this message
runfile('C:/Users/mdb_emr_syn/untitled0.py', wdir='C:/Users/mdb_emr_syn')
Traceback (most recent call last):
File "<ipython-input-19-2ecc66a5cadd>", line 1, in <module>
runfile('C:/Users/mdb_emr_syn/untitled0.py', wdir='C:/Users/mdb_emr_syn')
File "C:\Program Files\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
execfile(filename, namespace)
File "C:\Program Files\Anaconda2\lib\site-packages\spyder\utils\site\sitecustomize.py", line 87, in execfile
exec(compile(scripttext, filename, 'exec'), glob, loc)
File "C:/Users/mdb_emr_syn/untitled0.py", line 11, in <module>
connection.yo.authenticate("CEI","Syn12345.")
File "C:\Program Files\Anaconda2\lib\site-packages\pymongo\database.py", line 1048, in authenticate
connect=True)
File "C:\Program Files\Anaconda2\lib\site-packages\pymongo\mongo_client.py", line 505, in _cache_credentials
sock_info.authenticate(credentials)
File "C:\Program Files\Anaconda2\lib\site-packages\pymongo\pool.py", line 523, in authenticate
auth.authenticate(credentials, self)
File "C:\Program Files\Anaconda2\lib\site-packages\pymongo\auth.py", line 470, in authenticate
auth_func(credentials, sock_info)
File "C:\Program Files\Anaconda2\lib\site-packages\pymongo\auth.py", line 450, in _authenticate_default
return _authenticate_scram_sha1(credentials, sock_info)
File "C:\Program Files\Anaconda2\lib\site-packages\pymongo\auth.py", line 201, in _authenticate_scram_sha1
res = sock_info.command(source, cmd)
File "C:\Program Files\Anaconda2\lib\site-packages\pymongo\pool.py", line 419, in command
collation=collation)
File "C:\Program Files\Anaconda2\lib\site-packages\pymongo\network.py", line 116, in command
parse_write_concern_error=parse_write_concern_error)
File "C:\Program Files\Anaconda2\lib\site-packages\pymongo\helpers.py", line 210, in _check_command_response
raise OperationFailure(msg % errmsg, code, response)
OperationFailure: Authentication failed.
any ideas as to why????? thanks guys
.
(dot) in your URI password value. If it's a typo, please update your question. Thanks. – Wan Bachtiar