当前位置: 首页 > 面试题库 >

MongoLab / PyMongo连接错误

章威
2023-03-14
问题内容

如果我在外壳中运行:

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: Authentication failed.

我很确定它在进入insert_one()调用之前会失败,但是我不确定。

谢谢!

编辑:通过请求,这是完整的回调:

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:身份验证失败。


问题答案:

我想到了。你可以从python文件中执行此操作,它将起作用:

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


 类似资料:
  • 我正在尝试使用pymongo连接到MAC中的mongo。我得到以下错误- 回溯(最近一次调用):文件“”,第1行,从pymongo导入MongoClient导入错误:无法导入名称“MongoClient” 我也试过连接。但它给出了同样的错误。有什么帮助吗?

  • 问题内容: 我正在尝试运行与pymongo的简单连接,但它不断返回连接被拒绝 这是我尝试过的: 这就是我得到的 我该如何解决? 问题答案: 拆下内部 然后重新启动服务即可。例如,在我的Ubuntu安装中,重新启动服务器是这样的:

  • 我想通过pymongo阅读集合,但我得到以下错误: 回溯(最近的调用为last):文件“C://users/gsingh/pycharmprojects/mc/quizQuestionAnswers.py”,第21行,pprint.pprint(posts.find_one())文件“C:\users\gsingh\appdata\local\continuum\anaconda2\lib\sit

  • 问题内容: 当我使用MongoChef连接远程mongo数据库时,我使用下一个参数: 服务器 服务器: 本地主机 端口: 27017 SSH隧道 SSH地址: 10.1.0.90 端口: 25 SSH用户 名 : 用户名 SSH密码: 密码 当我与Pymongo连接时,我有以下代码: 但是我有下一个错误: 拜托,您能帮我解决这个问题吗?我做错了什么? 问题答案: 适用于我的解决方案。

  • 2021-04-18T19:25:28.390166+00:00App[Worker.1]:忽略on_ready中的异常 2021-04-18T19:25:28.392098+00:00应用程序[工作人员1]:回溯(最近一次调用): 2021-04-18T19:25:28.392173+00:00app[worker.1]:对于await bot.config.get_all()中的文档: 202

  • 我在namesheap服务器上有NodeJs应用程序。Mongo db托管在MongoLab上。尝试使用mongoose连接到数据库时,我遇到以下错误:mongoose连接“错误”事件引发:{MongoError:replicaset中找不到primary MONGO_URI=mongoDB://用户名:password@cluster0-shard-00-00-0dwia.mongodb.net