MongoLab / PyMongo连接错误 [英] MongoLab/PyMongo connection error

查看:788
本文介绍了MongoLab / PyMongo连接错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在shell中运行:

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

它可以让我连接到数据库并提取信息。但是,如果我在我的Python应用程序(Flask),并运行:

  import pymongo 

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

db = client [dbname]

db.users.insert_one({
user1:hello
})



它给了我一个:

  pymongo.errors.OperationFailure:身份验证失败。 

我很确定它在insert_one()调用之前失败了,但是我不完全确定。

谢谢!

编辑:
根据要求,这里是完整的回调:

pre $ code $ Traceback最近一次调用最后一次
在<模块>
从应用程序导入应用程序
文件/Users/Derek/Documents/programming/shenalum/app/__init__.py,第6行,在< module>
从应用程序导入视图
文件/Users/Derek/Documents/programming/shenalum/app/views.py,第4行,在< module>
从数据导入get_posts,get_user_info
文件/Users/Derek/Documents/programming/shenalum/app/data.py,第9行,在<模块>
user1:hello
文件/usr/local/lib/python2.7/site-packages/pymongo/collection.py,第622行,在insert_one
with self ._socket_for_writes()as sock_info:
文件/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/contextlib.py,第17行,在__enter__
中返回self.gen.next()
在_get_socket $ b $中的文件/usr/local/lib/python2.7/site-packages/pymongo/mongo_client.py,第718行b用server.get_socket(self .__ all_credentials)作为sock_info:
文件/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/contextlib。 py,第17行,在__enter__
返回self.gen.next()
文件/usr/local/lib/python2.7/site-packages/pymongo/server.py,第152行,在get_socket
中用self.pool.get_socket(all_credentials,checkout)作为sock_info:
文件/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/ lib / python2.7 / contextlib.py,第17行,在__enter__
返回self.gen.next()
文件/ usr / local / lib /python2.7/site-packages/pymongo/pool.py,行541,在get_socket
sock_info.check_auth(all_credentials)
文件/ usr / local / lib / python2.7 / site-packages / pymongo / pool.py,第306行,在check_auth
auth.authenticate(credentials,self)
文件/usr/local/lib/python2.7 /site-packages/pymongo/auth.py,第436行,进行身份验证
auth_func(credentials,sock_info)
文件/usr/local/lib/python2.7/site-packages/pymongo/ auth.py,第416行,在_authenticate_default
返回_authenticate_scram_sha1(credentials,sock_info)
文件/usr/local/lib/python2.7/site-packages/pymongo/auth.py,行188,在_authenticate_scram_sha1
res = sock_info.command(source,cmd)
文件/usr/local/lib/python2.7/site-packages/pymongo/pool.py,第213行,in命令
read_concern)
文件/usr/local/lib/python2.7/site-packages/pymongo/network.py,第99行,命令
helpers._check_command_response(response_doc,无,allowable_errors)
文件/ usr / l在$ _b





$ b $ /
$ / $ b $ / $ /



pymongo.errors.OperationFailure:Authentication failed。

解决方案

<我知道了。你可以从python文件中做到这一点,它可以工作:

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


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.

解决方案

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)

这篇关于MongoLab / PyMongo连接错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆