2

Is it possible to login on mongo shell without declaring the authentication DB? Here's an example, i log in to our mongo server using this command

[localhost$] mongo servername:27117 -u username -p passwd admin

Is there a way to log in without explicitly adding the admin db on the command?

1 Answer 1

1

Each user is associated with an authentication database. So if your user is declared in admin database (and authorization activated, what MUST be done in all environment server), it's mandatory to add this param in your login command.

See authentication, and more generally security for more details and explanations.

EDIT In case of using Connection String URI Format, you can skip authentication database param, in this case 'admin' will be used by default for authentication database, and 'test' by default as target database.

Example : (your user is created in 'admin' database.)

Here's different behaviors :

authentication against 'admin', database targeted is 'test'

$ mongo --host mongodb://user:pwd@myhost:27000
$ mongo --host mongodb://user:pwd@myhost:27000/test?authSource=admin
$ mongo --host mongodb://user:pwd@myhost:27000/?authSource=admin

authentication against 'admin', database targeted is 'admin'

$ mongo --host mongodb://user:pwd@myhost:27000/admin
$ mongo --host mongodb://user:pwd@myhost:27000/admin?authSource=admin

authentication against 'test', database targeted is 'test' (will not succeed)

$ mongo --host mongodb://user:pwd@myhost:27000/test
$ mongo --host mongodb://user:pwd@myhost:27000/test?authSource=test

EDIT 2 As you really need to use mongo -h -u -p notation, create your user in your 'test' database, which will be used by default in this case :

this will authenticate against 'test' database, and target 'test' database

mongo -h 199.99.99.99:27000 -u user -p pwd
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your prompt response, so is it possible to create a user outside the admin database but still has full access? that way i can remove the authentication db paramater.
In fact you can create a user in any database (but with access to other db possible). But if you really want to remove this param in your connection command, keep it in admin, i'll edit my answer with a workaround.
my end goal here really is to log in using the command mongo --port 27117 -u admin -p passwd This is because a script we are running will not accept a mongo command with the authentication db param. appreciate you helping me
edited my answer with this format. But you need to move your users from admin to test.

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.