java connection, com.mongodb.MongoCommandException:: 'Authentication failed

1,065 views
Skip to first unread message

Mich Talebzadeh

unread,
May 10, 2018, 7:51:06 PM5/10/18
to mongod...@googlegroups.com
Hi,

MongoDB 3.0.6 

I wrote a simple java program to connect to MongoDB. not a java expert!

Currently have mongo-java-driver-3.0.4.jar in my CLASSPATH.

The code is pretty basic

import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import static com.mongodb.client.model.Filters.*;
import com.mongodb.client.model.CreateCollectionOptions;
import com.mongodb.MongoClientURI;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;

public class MongoJdbcClient
{
 
  public static void main(String[] args) throws SQLException {
    boolean confirm = false;
   MongoClientURI uri = new MongoClientURI("mongodb://mydb_user:xxxxxxx@rhes564:60100/MYDB?authMechanism=SCRAM-SHA-1");
 
The uri fails with the following error

com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName='mydb_user', source='MYDB', password=<hidden>, mechanismProperties={}}
        at com.mongodb.connection.SaslAuthenticator.authenticate(SaslAuthenticator.java:61) ~[mongo-java-driver-3.0.4.jar:?]
        at com.mongodb.connection.InternalStreamConnectionInitializer.authenticateAll(InternalStreamConnectionInitializer.java:99) ~[mongo-java-driver-3.0.4.jar:?]
        at com.mongodb.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:44) ~[mongo-java-driver-3.0.4.jar:?]
        at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115) ~[mongo-java-driver-3.0.4.jar:?]
        at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:127) [mongo-java-driver-3.0.4.jar:?]
        at java.lang.Thread.run(Thread.java:745) [?:1.8.0_77]
Caused by: com.mongodb.MongoCommandException: Command failed with error 18: 'Authentication failed.' on server rhes564:60100. The full response is { "ok" : 0.0, "code" : 18, "errmsg" : "Authentication failed." }

However, the same connection on command line works!

mongo --host rhes564 --port 60100 --authenticationDatabase  MYDB -u MYDB_USER -p xxxxxx


Thanks,

Mich



Disclaimer: Use it at your own risk. Any and all responsibility for any loss, damage or destruction of data or any other property which may arise from relying on this email's technical content is explicitly disclaimed. The author will in no case be liable for any monetary damages arising from such loss, damage or destruction.

 

Jeff Yemin

unread,
May 11, 2018, 9:25:23 AM5/11/18
to mongodb-user
Try removing the "?authMechanism=SCRAM-SHA-1" from the connection string in the Java program, as it's possible that your user does not have SCRAM-SHA-1 credentials.  That's the main difference I see between the two cases.

Regards,
Jeff

Mich Talebzadeh

unread,
May 11, 2018, 11:59:13 AM5/11/18
to mongod...@googlegroups.com
Thanks Jeff,

I tried removing it and it did not work. Then I tried the following:


   MongoClientURI uri = new MongoClientURI("mongodb://"+_username+":"+_password+_mongoserver+_database+"?authMechanism=ScramSHA1");
 
It clearly expects some authentication so it came back with the following error:

Exception in thread "main" java.lang.IllegalArgumentException: Unsupported authMechanism: ScramSHA1

ScamsSHA1 is deliberate typo. It is supposed to be SCRAM-SHA-1

The thing is that this authentication SCRAM-SHA-1 works fine with Robo-3T as shown below



Not sure what is happening here?

Thanks

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: https://docs.mongodb.com/manual/support/
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user+unsubscribe@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/0e3e125b-9f6b-423f-8052-479620092279%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mich Talebzadeh

unread,
May 11, 2018, 12:10:27 PM5/11/18
to mongod...@googlegroups.com
update.

When I changed user to root it worked!!!
Reply all
Reply to author
Forward
0 new messages