1

I am doing Mongo DB Java side development and want to connect to Mongo DB instance. I read the authentication part from this link http://mongodb.github.io/mongo-java-driver/3.0/driver/reference/connecting/authenticating/. And it has below method to be used for authentication.

MongoCredential credential = MongoCredential.createCredential(user,
                                                              database,
                                                              password);

The above code works fine but I need to know the database name first. How to connect to Mongo DB without specifying database? I want to connect to it and return the list of databases for users to select.

2
  • 1
    The database here has nothing to do the database you want to work on. It's the source for authentication. "database - the database where the user is defined". Commented Nov 25, 2016 at 0:13
  • 1
    I am still not getting you. When trying to connect to Mongo DB, what is the value of database I should use? Commented Nov 25, 2016 at 4:44

3 Answers 3

1

As mentioned here, authentication in MongoDB is always does against a source - which can be external - but it is usually a database name.

Whenever you create a user, it is saved in a special collection called system.users in the admin db. Thus, the admin db is implicitly created when you create you first user.

When you want to list your databases, whether it's via a shell helper show dbs or database driver, the listDatabases command is being called behind the scenes.

If you enable authentication, in order to execute this command, you must be a user with a predefined (or custom) role which include the listDatabases privileged actions.

There are few builtin xxxAnyDatabase roles - such as readAnyDatabase role -which include the listDatabases privileged action.

If you want to create a user with any of the xxxAnyDatabase roles, it must be done in the admin database.

So, to make a long story short, if you want to list databases:

  1. Your only option is the admin database as the authentication source
  2. The user must have an adequate role: readAnyDatabase, readWriteAnyDatabase, dbAdminAnyDatabase (or a super user role such as root)
Sign up to request clarification or add additional context in comments.

Comments

1

You cannot connect without specifying a database, as users are DEFINED on a database. If you have not explicitly chosen a database where you created your user, the correct (default) database name is "admin".

Thus:

MongoCredential credential = MongoCredential.createCredential("myuser",
                                                          "admin",
                                                          "secret");

... should do the job.

Comments

-1
mvn archetype:generate -DgroupId=com.mkyong.core -DartifactId=mongodb -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

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.