7

I am not sure how to connect to a mongodb database that uses an authentication database with mongoengine.

On the command prompt I need to do mongo hostname:27017/myApp -u "test" -p "test" --authenticationDatabase admin, but I don't see where I'd pass this as an argument to mongoengine so I use the admin database for auth but connect to the myApp database for my models?

I believe this is where it's explained in the PyMongo guide:

https://api.mongodb.com/python/current/examples/authentication.html

>>> from pymongo import MongoClient
>>> client = MongoClient('example.com')
>>> db = client.the_database
>>> db.authenticate('user', 'password', source='source_database')

and I found the pull request that added this to mongoengine:

https://github.com/MongoEngine/mongoengine/pull/590/files

It looks like you just add authentication_source as an argument to connect like connect(authentication_source='admin'). It'd be nice if it was better documented.

http://docs.mongoengine.org/apireference.html?highlight=authentication_source

4 Answers 4

9

According to the mongoengine connecting guide, the connect() method support URI style connections. i.e.

connect(
   'project1'
   host='mongodb://username:password@host1:port1/databaseName'
)

In that sense, you can also specify the authentication source database as below:

"mongodb://username:password@host1:port1/database?authSource=source_database"

See also MongoDB connection string URI for more MongoDB URI examples. Also Authentication options through connection string

Sign up to request clarification or add additional context in comments.

2 Comments

this isn't what I'm asking, the authentication source needs to be explicitly stated and so what you sent won't work
@Rob updated. I've extracted the information out of the link which let's you specify authentication source via the URI.
5

The API has been updated, so this is the right way to do it now:

connect('mydb',
        host="localhost",
        username="admin",
        password="secret",
        authentication_source='your_auth_db')

1 Comment

I believe the API has been updated since I asked this question back in 2016, could you indicate that in your answer?
2

The solution suggested doesn't work for me. What does work: just add a authSource argument to the connect method as you would do with pymongo MongoClient method. Example:

connect('database_name', host='host', username="username", 
password="password",authSource='authentication_database_name') 

Comments

1

Here is an easy solution that worked for me.

connect(db="database_name", host="localhost", port=27017, username="username", 
password="password", authentication_source="admin")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.