0

I tried to connect to mongo db version 3.2 using mongo java client version 3.2 and getting the following exception, any idea what went wrong here?

com.mongodb.MongoSecurityException: Exception authenticating
at com.mongodb.connection.NativeAuthenticator.authenticate(NativeAuthenticator.java:48)
at com.mongodb.connection.InternalStreamConnectionInitializer.authenticateAll(InternalStreamConnectionInitializer.java:99)
at com.mongodb.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:44)
at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115)
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:128)
at java.lang.Thread.run(Thread.java:745)
Caused by: com.mongodb.MongoCommandException: Command failed with error 18: 'auth failed' on server 10.100.5.41:27017. The full response is { "ok" : 0.0, "errmsg" : "auth failed", "code" : 18 }
at com.mongodb.connection.CommandHelper.createCommandFailureException(CommandHelper.java:170)
at com.mongodb.connection.CommandHelper.receiveCommandResult(CommandHelper.java:123)
at com.mongodb.connection.CommandHelper.executeCommand(CommandHelper.java:32)
at com.mongodb.connection.NativeAuthenticator.authenticate(NativeAuthenticator.java:46)
... 5 more

Following is the code I used to connect.

MongoClient mongoClient = null;

    try {
        MongoCredential credential = MongoCredential.createMongoCRCredential("admin", "mydatabase", "admin123".toCharArray());
        mongoClient = new MongoClient(new ServerAddress("10.100.5.41", 27017), Arrays.asList(credential));

        MongoDatabase database = mongoClient.getDatabase("mydatabase");
        System.out.println(database.getName());
        MongoCollection collection = database.getCollection("user");

        MongoCursor cursor = collection.find().iterator();
        while (cursor.hasNext()) {
            System.out.println(cursor.next());
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
5
  • Have you added the user on "mydatabase" or on "admin" (which is the default)? Or in other words, try "admin" as the database in createMongoCRCredential. Commented Dec 15, 2016 at 7:16
  • 1
    @Zeus the code seems correct. I'm doing the same and have no problem connecting to my servers. Check the logs on your server for errors during the authentication. docs.mongodb.com/manual/reference/system-users-collection Commented Dec 15, 2016 at 7:19
  • Please can someone close the question, after hours of debugging, db administrator told that, he has changed the username and password. At times I wonder why I become a Software Engineer. Commented Dec 15, 2016 at 7:35
  • @Zeus answer your question with what you just said and accept it Commented Dec 15, 2016 at 7:43
  • @neomega great, thanks for the tip. Commented Dec 15, 2016 at 8:25

1 Answer 1

13

After hours of debugging, db administrator told that, he has changed the username and password. At times I wonder why I become a Software Engineer.

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

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.