0

I'm calling db dev to insert data into its collection. But it's creating a new collection and inserting data into admin db.

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

@app.route('/')
@app.route('/index')
def index():
    return "Hello, World!"

app.config['MONGO_DBNAME'] = 'dev'
app.config['MONGO_AUTH_SOURCE'] = 'admin'
app.config['MONGO_URI'] = 'mongodb://<user>:<password>@<url>:27017/admin'

mongo = PyMongo(app)

@app.route('/mongo', methods=['GET'])
def get_all_docs():
  doc = mongo.db.abcd.insert({'abcd':'abcd'})
  return "Inserted"

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

Am I missing something here?

PS: I tried replacing admin with dev. It gave pymongo.errors.OperationFailure: Authentication failed. I guess thats because the authentication data is in admin db.

app.config['MONGO_URI'] = 'mongodb://<user>:<password>@<url>:27017' This, also, didn't work.

Anubhav
  • 147
  • 1
  • 13
  • Why not simply `'mongodb://:@:27017/dev'`? If you are reading posts telling you to set AUTH_SOURCE or "admin" as the database then those sources are really outdated. The driver looks at `"admin"` for authorization by "default", and you should only need the other options where a different namespace was specifically used intentionally. Since you are using the default, the other settings are not required. – Neil Lunn Aug 10 '17 at 09:13
  • @NeilLunn `pymongo.errors.OperationFailure: Authentication failed.` when I use dev in place of admin. I've mentioned this above. – Anubhav Aug 10 '17 at 09:22
  • What is actually being used for the connection? The question tagged with mongoengine. Are you actually using this or are you calling the raw pymongo driver in your own code not shown here? Because that `PyMongo(app)` call is not used by either AFAIK. Most importantly **are you possitive** that the account actually has access to the `dev` database? Have you actually tried connecting and working with the collection using that account from another source? – Neil Lunn Aug 10 '17 at 09:24
  • @NeilLunn I'm calling the pymongo driver. And yes, I have access to dev database. I have several other connections working fine. – Anubhav Aug 10 '17 at 09:28
  • Well `PyMongo` is not an export of the pymongo driver. You "should" be using `MongoClient` for the connection. Which leads me to think that there is your own code not shown in the question that is the actual source of the problem. Please actually show the code where you apply the connection details. – Neil Lunn Aug 10 '17 at 09:30
  • Code updated. I'm importing pymongo using `from flask_pymongo import PyMongo` – Anubhav Aug 10 '17 at 09:33

1 Answers1

2

Replacing admin with dev in MONGO_URI causes Authentication Error.
Adding authSource will authenticate with admin db.
To do this, replace admin with dev?authSource=admin

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

@app.route('/')
@app.route('/index')
def index():
    return "Hello, World!"

app.config['MONGO_URI'] = 'mongodb://<user>:<password>@<url>:27017/dev?authSource=admin'

mongo = PyMongo(app)

@app.route('/mongo', methods=['GET'])
def get_all_docs():
  doc = mongo.db.abcd.insert({'abcd':'abcd'})
  return "Inserted"

if __name__ == '__main__':
    app.run(debug=True)
Anubhav
  • 147
  • 1
  • 13