-1

I am trying to connect to my mongodb in mlab.com using flask but when I run my flask script I am getting Authentication failed error, please help.

my code:

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})

Error:

  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.

Please help to resolve this.

Pyd
  • 6,017
  • 18
  • 52
  • 109

3 Answers3

0

You can use this as an alternative to sign in to your database. This is tested and works :

connection = pymongo.MongoClient(HOST, PORT)
db = connection[databasename]
db.authenticate(database_user, database_pass)
  • Thanks for you suggestion, actually it's working with another dB username and password. Can you pls answer my another question https://stackoverflow.com/questions/49843914/how-to-return-documents-which-are-contains-the-specific-keywords-in-the-keys-fro – Pyd Apr 15 '18 at 17:47
  • While I would love to answer the question, it has been marked duplicate. – Aniruddh Chandratre Apr 15 '18 at 17:49
  • you can comment the answer – Pyd Apr 15 '18 at 17:53
  • 1
    ```framework.find({'brand_name' : {'$regex' : 'your_text', '$options' : 'i'}})``` should do the trick. (I don't have enough rep to comment on that thread) – Aniruddh Chandratre Apr 15 '18 at 17:54
0

I created a new user with password for my db, and worked fine.

Pyd
  • 6,017
  • 18
  • 52
  • 109
0

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

This Works fine. For me connecting via URI string from mLab database doesn't work as well.

Peter.k
  • 1,475
  • 23
  • 40