【问题标题】:authendication failed when connecting to mongodb in mlab在 mlab 中连接到 mongodb 时身份验证失败
【发布时间】:2018-09-25 08:11:00
【问题描述】:

我正在尝试使用烧瓶连接到 mlab.com 中的我的 mongodb,但是当我运行我的烧瓶脚本时,我收到 Authentication failed 错误,请帮助。

我的代码:

from flask import Flask, jsonify, request
from flask_pymongo import PyMongo

app = Flask(__name__)

app.config['MONGO_DBNAME'] = 'mydb'
app.config['MONGO_URI'] = 'mongodb://user:pwd@ds157799.mlab.com:57799/mydb'


mongo = PyMongo(app)

@app.route('/framework', methods=['GET'])
def get_all_frameworks():
    framework = mongo.db.framework 

    output = []

    for q in framework.find():
        output.append({'name' : q['name'], 'language' : q['language']})

    return jsonify({'result' : output})

错误:

  File "mongo.py", line 12, in <module>
    mongo = PyMongo(app)
  File "C:\Users\ELCOT\AppData\Local\Programs\Python\Python35\lib\site-packages\flask_pymongo\__init__.py", line 97, in __init__
    self.init_app(app, config_prefix)
  File "C:\Users\ELCOT\AppData\Local\Programs\Python\Python35\lib\site-packages\flask_pymongo\__init__.py", line 283, in init_app
    mechanism=auth_mechanism)
  File "C:\Users\ELCOT\AppData\Local\Programs\Python\Python35\lib\site-packages\pymongo\database.py", line 1167, in authenticate
    connect=True)
  File "C:\Users\ELCOT\AppData\Local\Programs\Python\Python35\lib\site-packages\pymongo\mongo_client.py", line 588, in _cache_credentials
    sock_info.authenticate(credentials)
  File "C:\Users\ELCOT\AppData\Local\Programs\Python\Python35\lib\site-packages\pymongo\pool.py", line 620, in authenticate
    auth.authenticate(credentials, self)
  File "C:\Users\ELCOT\AppData\Local\Programs\Python\Python35\lib\site-packages\pymongo\auth.py", line 486, in authenticate
    auth_func(credentials, sock_info)
  File "C:\Users\ELCOT\AppData\Local\Programs\Python\Python35\lib\site-packages\pymongo\auth.py", line 237, in _authenticate_scram_sha1
    res = sock_info.command(source, cmd)
  File "C:\Users\ELCOT\AppData\Local\Programs\Python\Python35\lib\site-packages\pymongo\pool.py", line 517, in command
    collation=collation)
  File "C:\Users\ELCOT\AppData\Local\Programs\Python\Python35\lib\site-packages\pymongo\network.py", line 125, in command
    parse_write_concern_error=parse_write_concern_error)
  File "C:\Users\ELCOT\AppData\Local\Programs\Python\Python35\lib\site-packages\pymongo\helpers.py", line 145, in _check_command_response
    raise OperationFailure(msg % errmsg, code, response)
pymongo.errors.OperationFailure: Authentication failed.

请帮助解决这个问题。

【问题讨论】:

    标签: python json mongodb flask mlab


    【解决方案1】:

    您可以使用它作为登录数据库的替代方法。这是经过测试并且有效的:

    connection = pymongo.MongoClient(HOST, PORT)
    db = connection[databasename]
    db.authenticate(database_user, database_pass)
    

    【讨论】:

    • 感谢您的建议,实际上它正在使用另一个 dB 用户名和密码。你能回答我的另一个问题吗stackoverflow.com/questions/49843914/…
    • 虽然我很想回答这个问题,但它已被标记为重复。
    • 你可以评论答案
    • framework.find({'brand_name' : {'$regex' : 'your_text', '$options' : 'i'}}) 应该可以解决问题。 (我没有足够的代表来评论那个帖子)
    【解决方案2】:

    我为我的数据库创建了一个带有密码的新用户,并且工作正常。

    【讨论】:

      【解决方案3】:

      https://www.reddit.com/r/flask/comments/5ftqvm/how_to_use_pymongo_with_hosted_mongodb_mlab/

      这很好用。对我来说,通过 mLab 数据库中的 URI 字符串连接也无法正常工作。

      【讨论】:

        猜你喜欢
        • 2016-07-05
        • 2018-01-19
        • 2018-11-27
        • 2021-01-18
        • 2015-08-29
        • 1970-01-01
        • 2019-06-22
        • 2018-07-10
        • 2015-07-09
        相关资源
        最近更新 更多