MongoLab/PyMongo connection error

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



MongoLab/PyMongo connection error



If I run in the shell:


mongo ds0219xx.mlab.com:219xx/dbname -u user -p pass



It works and allows me to connect to the database and pull information. But if I'm within my python application (Flask) and run this:


import pymongo

client = pymongo.MongoClient("mongodb://user:pass@ds0219xx.mlab.com:219xx/dbname")

db = client["dbname"]

db.users.insert_one(
"user1": "hello"
)



It gives me an:


pymongo.errors.OperationFailure: Authentication failed.



I'm pretty sure it's failing before it gets to the insert_one() call, but I'm not completely sure.



Thanks!



Edit:
By request, here is the full callback:


Traceback (most recent call last):
File "run.py", line 1, in <module>
from app import app
File "/Users/Derek/Documents/programming/shenalum/app/__init__.py", line 6, in <module>
from app import views
File "/Users/Derek/Documents/programming/shenalum/app/views.py", line 4, in <module>
from data import get_posts, get_user_info
File "/Users/Derek/Documents/programming/shenalum/app/data.py", line 9, in <module>
"user1": "hello"
File "/usr/local/lib/python2.7/site-packages/pymongo/collection.py", line 622, in insert_one
with self._socket_for_writes() as sock_info:
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/contextlib.py", line 17, in __enter__
return self.gen.next()
File "/usr/local/lib/python2.7/site-packages/pymongo/mongo_client.py", line 718, in _get_socket
with server.get_socket(self.__all_credentials) as sock_info:
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/contextlib.py", line 17, in __enter__
return self.gen.next()
File "/usr/local/lib/python2.7/site-packages/pymongo/server.py", line 152, in get_socket
with self.pool.get_socket(all_credentials, checkout) as sock_info:
File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/contextlib.py", line 17, in __enter__
return self.gen.next()
File "/usr/local/lib/python2.7/site-packages/pymongo/pool.py", line 541, in get_socket
sock_info.check_auth(all_credentials)
File "/usr/local/lib/python2.7/site-packages/pymongo/pool.py", line 306, in check_auth
auth.authenticate(credentials, self)
File "/usr/local/lib/python2.7/site-packages/pymongo/auth.py", line 436, in authenticate
auth_func(credentials, sock_info)
File "/usr/local/lib/python2.7/site-packages/pymongo/auth.py", line 416, in _authenticate_default
return _authenticate_scram_sha1(credentials, sock_info)
File "/usr/local/lib/python2.7/site-packages/pymongo/auth.py", line 188, in _authenticate_scram_sha1
res = sock_info.command(source, cmd)
File "/usr/local/lib/python2.7/site-packages/pymongo/pool.py", line 213, in command
read_concern)
File "/usr/local/lib/python2.7/site-packages/pymongo/network.py", line 99, in command
helpers._check_command_response(response_doc, None, allowable_errors)
File "/usr/local/lib/python2.7/site-packages/pymongo/helpers.py", line 196, in _check_command_response
raise OperationFailure(msg % errmsg, code, response)



pymongo.errors.OperationFailure: Authentication failed.





Could you include the whole traceback? That way we could be sure if it fails before the insert_one(...) call. Though the error is about auth and that would probably mean during MongClient creation :P
– Ilja Everilä
Mar 24 '16 at 12:33



insert_one(...)


MongClient





added that, thanks
– Derek Schuster
Mar 24 '16 at 12:38




2 Answers
2



I figured it out. You can do this from the python file and it will work:


connection = pymongo.MongoClient(ab123456.mlab.com, 123456)
db = connection[databasename]
db.authenticate(database_user, database_pass)





Please please please, mark it as correct. This is gem :)
– Nabin
May 13 '17 at 14:31





what does this mean? like what are you trying to convey in the first line: pymongo.MongoClient(ab123456.mlab.com, 123456). Does the port number appear as a second argument? or is the password but no username? i'm not sure how to interpret this
– frogeyedpeas
Jul 21 '17 at 0:27





I guess ideally phrase that in terms of the URL OP supplied and then it'll make more sense :)
– frogeyedpeas
Jul 21 '17 at 0:31





pymongo.errors.ConnectionFailure: timed out
– pyd
May 23 at 6:03


pymongo.errors.ConnectionFailure: timed out





One caveat here. The characters "ab1" are probably referring to the server name, whereas "23456" would be the port, not 123456. So, heads up.
– Jace
Jul 4 at 17:42



The answer given by @Derek Schuster was correct. Just to make it more clear.


DB_NAME = database_name
DB_HOST = dsxxxxxx.mlab.com
DB_PORT = 12345
DB_USER = user
DB_PASS = pass

connection = MongoClient(DB_HOST, DB_PORT)
db = connection[DB_NAME]
db.authenticate(DB_USER, DB_PASS)



Hope it helps. :)






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS