Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connection to MongoDB with authentication fails

I am using a MongoDB in Version 3 and I created a database named 'logMonitor' and created a user like:

{
    "_id" : "logMonitor.log",
    "user" : "log",
    "db" : "logMonitor",
    "roles" : [
        {
            "role" : "readWrite",
            "db" : "logMonitor"
        }
    ]
}

when I connect to the database by shell with user "log", it returns success, just like this:

[jboss@chonggouapp mongodb]$ mongo logMonitor -u "log" -p "log"

MongoDB shell version: 3.0.6

connecting to: logMonitor

However connection via Java with the following code fails.

    ServerAddress addr = new ServerAddress("10.46.20.65", 27017);
    
    MongoCredential credential = MongoCredential.createMongoCRCredential(
            "log", "logMonitor", "log".toCharArray());
    
    MongoClientOptions options = MongoClientOptions.builder()
            .serverSelectionTimeout(1000)
            .build();
    MongoClient mongoClient = new MongoClient(addr, Arrays.asList(credential), options);
    
    MongoDatabase db = mongoClient.getDatabase("logMonitor");
    
    long c = db.getCollection("sysLog").count();

The following exception is raised:

Exception in thread "main" com.mongodb.MongoTimeoutException: Timed out after 1000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. Client view of cluster state is {type=UNKNOWN, servers=[{address=10.46.20.65:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSecurityException: Exception authenticating}, caused by {com.mongodb.MongoCommandException: Command failed with error 18: 'auth failed' on server 10.46.20.65:27017. The full response is { "ok" : 0.0, "errmsg" : "auth failed", "code" : 18 }}}]
    at com.mongodb.connection.BaseCluster.createTimeoutException(BaseCluster.java:370)
    at com.mongodb.connection.BaseCluster.selectServer(BaseCluster.java:101)
    at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.<init>(ClusterBinding.java:75)
    at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.<init>(ClusterBinding.java:71)
    at com.mongodb.binding.ClusterBinding.getReadConnectionSource(ClusterBinding.java:63)
    at com.mongodb.operation.CommandOperationHelper.executeWrappedCommandProtocol(CommandOperationHelper.java:65)
    at com.mongodb.operation.CountOperation.execute(CountOperation.java:172)
    at com.mongodb.operation.CountOperation.execute(CountOperation.java:43)
    at com.mongodb.Mongo.execute(Mongo.java:738)
    at com.mongodb.Mongo$2.execute(Mongo.java:725)
    at com.mongodb.MongoCollectionImpl.count(MongoCollectionImpl.java:167)
    at com.mongodb.MongoCollectionImpl.count(MongoCollectionImpl.java:147)
    at com.baosight.bsfc4.mn.lg.utils.Test.main(Test.java:34)

Can anyone tell me what is the problem? Is someting wrong with my java code?

Thanks.

like image 822
Yuanping Wu Avatar asked Dec 22 '15 11:12

Yuanping Wu


People also ask

Why is my MongoDB not connecting?

If you have created a user and are having trouble authenticating, try the following: Check that you are using the correct username and password for your database user, and that you are connecting to the correct database deployment. Check that you are specifying the correct authSource database in your connection string.

What does authentication failed mean?

If you receive this error message, that means that the username and/or password that you have entered is incorrect. The error message states “Authentication failed! Try again.” You may have locked your account after too many attempts and your account will need to be reset.


2 Answers

Use createMongoCredential() instead and it should create the correct kind of credentials for you.

like image 65
evanchooly Avatar answered Nov 14 '22 04:11

evanchooly


Solution:

Need to provide dbAdmin role to this user

db.grantRolesToUser("Username", [{ "role" : "dbAdmin", "db" : "DBName" }])
like image 38
Uzzal Basak Avatar answered Nov 14 '22 03:11

Uzzal Basak