flask_pymongo 连接Mongodb认证失败

发布于 2022-09-03 03:24:19 字数 3203 浏览 13 评论 0

在使用flask的flask_pymongo模块时,出现认证错误。
但可以确定用户名和密码没有错。

from flask import Flask
from flask_pymongo import PyMongo
app = Flask(__name__)

app.config['MONGO_URI'] = 'mongodb://root:aaa2016@localhost:27017/mongo_test'
mongo = PyMongo(app, config_prefix='MONGO')


@app.route('/')
def hello_world():
    mongo.db.user.insert({'username': "aaa"})
    return 'Hello World!'


if __name__ == '__main__':
    app.run()

访问127.0.0.1:5000时,报出Authentication failed.

raceback (most recent call last):
  File "C:\Python27\lib\site-packages\flask\app.py", line 1988, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Python27\lib\site-packages\flask\app.py", line 1641, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Python27\lib\site-packages\flask\app.py", line 1544, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Python27\lib\site-packages\flask\app.py", line 1639, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Python27\lib\site-packages\flask\app.py", line 1625, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "F:/PycharmProjects/flask_/flask_.py", line 11, in hello_world
    mongo.db.sitelist.insert({'ss': "ss"})
  File "C:\Python27\lib\site-packages\pymongo\collection.py", line 2203, in insert
    with self._socket_for_writes() as sock_info:
  File "C:\Python27\lib\contextlib.py", line 17, in __enter__
    return self.gen.next()
  File "C:\Python27\lib\site-packages\pymongo\mongo_client.py", line 718, in _get_socket
    with server.get_socket(self.__all_credentials) as sock_info:
  File "C:\Python27\lib\contextlib.py", line 17, in __enter__
    return self.gen.next()
  File "C:\Python27\lib\site-packages\pymongo\server.py", line 152, in get_socket
    with self.pool.get_socket(all_credentials, checkout) as sock_info:
  File "C:\Python27\lib\contextlib.py", line 17, in __enter__
    return self.gen.next()
  File "C:\Python27\lib\site-packages\pymongo\pool.py", line 541, in get_socket
    sock_info.check_auth(all_credentials)
  File "C:\Python27\lib\site-packages\pymongo\pool.py", line 306, in check_auth
    auth.authenticate(credentials, self)
  File "C:\Python27\lib\site-packages\pymongo\auth.py", line 436, in authenticate
    auth_func(credentials, sock_info)
  File "C:\Python27\lib\site-packages\pymongo\auth.py", line 416, in _authenticate_default
    return _authenticate_scram_sha1(credentials, sock_info)
  File "C:\Python27\lib\site-packages\pymongo\auth.py", line 188, in _authenticate_scram_sha1
    res = sock_info.command(source, cmd)
  File "C:\Python27\lib\site-packages\pymongo\pool.py", line 213, in command
    read_concern)
  File "C:\Python27\lib\site-packages\pymongo\network.py", line 99, in command
    helpers._check_command_response(response_doc, None, allowable_errors)
  File "C:\Python27\lib\site-packages\pymongo\helpers.py", line 196, in _check_command_response
    raise OperationFailure(msg % errmsg, code, response)
OperationFailure: Authentication failed.
127.0.0.1 - - [21/Jun/2016 20:23:19] "GET / HTTP/1.1" 500 -

求教。。。

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

对不⑦ 2022-09-10 03:24:19

flask_pymongo没用过,不过你可以参考用pymongo

import pymongo
client = pymongo.MongoClient('mongodb://root:aaa2016@localhost:27017/')
user = client.mongo_test['user']

user.insert({'username': "aaa"})
梦在深巷 2022-09-10 03:24:19

推荐直接使用pymongo或者flask-mongoalchemy

空城缀染半城烟沙 2022-09-10 03:24:19

自己找到了原因。
这里报错:Authentication failed.是因为我的登录用户没有相应数据库的操作权限。
查看系统的user表,db.getCollection('system.users').find({})

{
    "_id" : "admin.root",
    "user" : "root",
    "db" : "admin",
    "credentials" : {
        "SCRAM-SHA-1" : {
            "iterationCount" : 10000,
            "salt" : "*****************",
            "storedKey" : "*****************",
            "serverKey" : "*****************"
        }
    },
    "roles" : [ 
        {
            "role" : "userAdminAnyDatabase",
            "db" : "admin"
        }
    ]
}

可以看到这里只有admin数据库的权限,而没有mongo_test数据库的操作权限。可以在user表中添加相应的库的权限。
flask_pymongo, flask_mongoengine中都是这样,但为什么pymongo不需要响应数据库权限还是没搞明白,希望有谁知道的可以赐教。。。

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文