相关文章推荐
知识渊博的帽子  ·  scrapy ...·  3 周前    · 
知识渊博的帽子  ·  Anonymous user has no ...·  7 月前    · 
知识渊博的帽子  ·  开源C# ...·  8 月前    · 
知识渊博的帽子  ·  Project Converter ...·  8 月前    · 
知识渊博的帽子  ·  [Solved] Chicken and ...·  9 月前    · 
知识渊博的帽子  ·  The Basics — CVX ...·  9 月前    · 
俊秀的手套  ·  Qt | 显示网络图片 ...·  1小时前    · 
失恋的青椒  ·  20.5 OpenSSL ...·  1小时前    · 
爱旅游的帽子  ·  20.1 OpenSSL ...·  1小时前    · 
爱健身的跑步鞋  ·  机电产品进口管理办法·  2 小时前    · 
飘逸的马克杯  ·  【GIT SourceTree】_<git ...·  2 小时前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm currently trying to connect to a mongoDB database via the java driver and I'm given the exceptions listed below when I run the code above it. Not sure what to do or what's wrong. If it appears to be an import or JRE problem please list the part to import if you can.

Code in Java:

MongoClient mongoClient = new MongoClient(new 
MongoClientURI("mongodb+srv://wendulem:MYPASSWORD@cluster0 
ugj9q.mongodb.net/test?retryWrites=true"));
    MongoDatabase database = mongoClient.getDatabase("GradeU");
    MongoCollection<Document> collection = 
    database.getCollection("profiles");
    Document myDoc;
    myDoc = collection.find(eq("username", "admin")).first();
    System.out.println(myDoc.toJson());

Console Logs:

Jul 06, 2018 12:49:55 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Cluster created with settings {hosts=[cluster0-shard-00-02-ugj9q.mongodb.net:27017, cluster0-shard-00-00-ugj9q.mongodb.net:27017, cluster0-shard-00-01-ugj9q.mongodb.net:27017], mode=MULTIPLE, requiredClusterType=REPLICA_SET, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500, requiredReplicaSetName='Cluster0-shard-0'}
Jul 06, 2018 12:49:55 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Adding discovered server cluster0-shard-00-02-ugj9q.mongodb.net:27017 to client view of cluster
Jul 06, 2018 12:49:55 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Adding discovered server cluster0-shard-00-00-ugj9q.mongodb.net:27017 to client view of cluster
Jul 06, 2018 12:49:55 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Adding discovered server cluster0-shard-00-01-ugj9q.mongodb.net:27017 to client view of cluster
Jul 06, 2018 12:49:56 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: No server chosen by com.mongodb.client.internal.MongoClientDelegate$1@11438d26 from cluster description ClusterDescription{type=REPLICA_SET, connectionMode=MULTIPLE, serverDescriptions=[ServerDescription{address=cluster0-shard-00-02-ugj9q.mongodb.net:27017, type=UNKNOWN, state=CONNECTING}, ServerDescription{address=cluster0-shard-00-01-ugj9q.mongodb.net:27017, type=UNKNOWN, state=CONNECTING}, ServerDescription{address=cluster0-shard-00-00-ugj9q.mongodb.net:27017, type=UNKNOWN, state=CONNECTING}]}. Waiting for 30000 ms before timing out
Jul 06, 2018 12:49:56 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Opened connection [connectionId{localValue:1, serverValue:4355}] to cluster0-shard-00-02-ugj9q.mongodb.net:27017
Jul 06, 2018 12:49:56 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Opened connection [connectionId{localValue:3, serverValue:4703}] to cluster0-shard-00-01-ugj9q.mongodb.net:27017
Jul 06, 2018 12:49:56 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Opened connection [connectionId{localValue:2, serverValue:7669}] to cluster0-shard-00-00-ugj9q.mongodb.net:27017
Jul 06, 2018 12:49:56 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Monitor thread successfully connected to server with description ServerDescription{address=cluster0-shard-00-00-ugj9q.mongodb.net:27017, type=REPLICA_SET_PRIMARY, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 6, 5]}, minWireVersion=0, maxWireVersion=6, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=21097463, setName='Cluster0-shard-0', canonicalAddress=cluster0-shard-00-00-ugj9q.mongodb.net:27017, hosts=[cluster0-shard-00-00-ugj9q.mongodb.net:27017, cluster0-shard-00-01-ugj9q.mongodb.net:27017, cluster0-shard-00-02-ugj9q.mongodb.net:27017], passives=[], arbiters=[], primary='cluster0-shard-00-00-ugj9q.mongodb.net:27017', tagSet=TagSet{[]}, electionId=7fffffff0000000000000001, setVersion=1, lastWriteDate=Fri Jul 06 12:49:50 EDT 2018, lastUpdateTimeNanos=75088686541038}
Jul 06, 2018 12:49:56 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Monitor thread successfully connected to server with description ServerDescription{address=cluster0-shard-00-01-ugj9q.mongodb.net:27017, type=REPLICA_SET_SECONDARY, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 6, 5]}, minWireVersion=0, maxWireVersion=6, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=28398802, setName='Cluster0-shard-0', canonicalAddress=cluster0-shard-00-01-ugj9q.mongodb.net:27017, hosts=[cluster0-shard-00-00-ugj9q.mongodb.net:27017, cluster0-shard-00-01-ugj9q.mongodb.net:27017, cluster0-shard-00-02-ugj9q.mongodb.net:27017], passives=[], arbiters=[], primary='cluster0-shard-00-00-ugj9q.mongodb.net:27017', tagSet=TagSet{[]}, electionId=null, setVersion=1, lastWriteDate=Fri Jul 06 12:49:50 EDT 2018, lastUpdateTimeNanos=75088689801046}
Jul 06, 2018 12:49:56 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Monitor thread successfully connected to server with description ServerDescription{address=cluster0-shard-00-02-ugj9q.mongodb.net:27017, type=REPLICA_SET_SECONDARY, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 6, 5]}, minWireVersion=0, maxWireVersion=6, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=22170448, setName='Cluster0-shard-0', canonicalAddress=cluster0-shard-00-02-ugj9q.mongodb.net:27017, hosts=[cluster0-shard-00-00-ugj9q.mongodb.net:27017, cluster0-shard-00-01-ugj9q.mongodb.net:27017, cluster0-shard-00-02-ugj9q.mongodb.net:27017], passives=[], arbiters=[], primary='cluster0-shard-00-00-ugj9q.mongodb.net:27017', tagSet=TagSet{[]}, electionId=null, setVersion=1, lastWriteDate=Fri Jul 06 12:49:50 EDT 2018, lastUpdateTimeNanos=75088684046959}
Jul 06, 2018 12:49:56 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Setting max election id to 7fffffff0000000000000001 from replica set primary cluster0-shard-00-00-ugj9q.mongodb.net:27017
Jul 06, 2018 12:49:56 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Setting max set version to 1 from replica set primary cluster0-shard-00-00-ugj9q.mongodb.net:27017
Jul 06, 2018 12:49:56 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Discovered replica set primary cluster0-shard-00-00-ugj9q.mongodb.net:27017
Jul 06, 2018 12:49:56 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Closed connection [connectionId{localValue:4}] to cluster0-shard-00-00-ugj9q.mongodb.net:27017 because there was a socket exception raised by this connection.
Exception in thread "main" com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName='wendulem', source='admin', password=<hidden>, mechanismProperties={}}
    at com.mongodb.internal.connection.SaslAuthenticator.wrapException(SaslAuthenticator.java:173)
    at com.mongodb.internal.connection.SaslAuthenticator.access$300(SaslAuthenticator.java:40)
    at com.mongodb.internal.connection.SaslAuthenticator$1.run(SaslAuthenticator.java:70)
    at com.mongodb.internal.connection.SaslAuthenticator$1.run(SaslAuthenticator.java:47)
    at com.mongodb.internal.connection.SaslAuthenticator.doAsSubject(SaslAuthenticator.java:179)
    at com.mongodb.internal.connection.SaslAuthenticator.authenticate(SaslAuthenticator.java:47)
    at com.mongodb.internal.connection.InternalStreamConnectionInitializer.authenticateAll(InternalStreamConnectionInitializer.java:151)
    at com.mongodb.internal.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:64)
    at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:127)
    at com.mongodb.internal.connection.UsageTrackingInternalConnection.open(UsageTrackingInternalConnection.java:50)
    at com.mongodb.internal.connection.DefaultConnectionPool$PooledConnection.open(DefaultConnectionPool.java:390)
    at com.mongodb.internal.connection.DefaultConnectionPool.get(DefaultConnectionPool.java:106)
    at com.mongodb.internal.connection.DefaultConnectionPool.get(DefaultConnectionPool.java:92)
    at com.mongodb.internal.connection.DefaultServer.getConnection(DefaultServer.java:85)
    at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.getConnection(ClusterBinding.java:114)
    at com.mongodb.client.internal.ClientSessionBinding$SessionBindingConnectionSource.getConnection(ClientSessionBinding.java:111)
    at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:455)
    at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:401)
    at com.mongodb.operation.FindOperation.execute(FindOperation.java:695)
    at com.mongodb.operation.FindOperation.execute(FindOperation.java:83)
    at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:179)
    at com.mongodb.client.internal.FindIterableImpl.first(FindIterableImpl.java:198)
    at gradebook.grademain.main(grademain.java:39)
Caused by: com.mongodb.MongoCommandException: Command failed with error 8000 (AtlasError): 'bad auth Authentication failed.' on server cluster0-shard-00-00-ugj9q.mongodb.net:27017. The full response is { "ok" : 0, "errmsg" : "bad auth Authentication failed.", "code" : 8000, "codeName" : "AtlasError" }
    at com.mongodb.internal.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:179)
    at com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:293)
    at com.mongodb.internal.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:255)
    at com.mongodb.internal.connection.CommandHelper.sendAndReceive(CommandHelper.java:83)
    at com.mongodb.internal.connection.CommandHelper.executeCommand(CommandHelper.java:33)
    at com.mongodb.internal.connection.SaslAuthenticator.sendSaslContinue(SaslAuthenticator.java:134)
    at com.mongodb.internal.connection.SaslAuthenticator.access$200(SaslAuthenticator.java:40)
    at com.mongodb.internal.connection.SaslAuthenticator$1.run(SaslAuthenticator.java:67)
                Can you add the code that configures the connection to MongoDB?  The error message looks like the authentication is incorrect:        "errmsg" : "bad auth Authentication failed."
– Jared Stewart
                Jul 6, 2018 at 17:13
                The best way to get good formatting is just to edit your original post and add it there :)
– Jared Stewart
                Jul 6, 2018 at 17:34
                Are you able to connect via mongo shell with  mongo cluster0-ugj9q.mongodb.net/test -u wendulem -p MYPASSWORD?
– Jared Stewart
                Jul 6, 2018 at 17:40

If credentials are correct and MongoDB Atlas Cloud details are correct, Then It is problem of mismatch version of mongoDB driver and connection string.

First try with latest

mongodb+srv://

Or else try with below connection string if still a problem

"mongodb://

2nd driver string is working for me.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

 
推荐文章