我目前正在尝试通过Java驱动程序连接到mongoDB数据库,在运行上面的代码时,我得到了下面列出的异常。不知道该怎么办或出了什么问题。如果它似乎是导入或JRE问题,请尽可能列出要导入的零件。
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());
控制台日志:
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)
答案 0 :(得分:0)
请确保您的密码不是以$结尾,因为这就是我的问题,在这种情况下,无需引用。
答案 1 :(得分:0)
如果凭据正确且MongoDB Atlas Cloud详细信息正确,则可能是mongoDB驱动程序版本和连接字符串不匹配的问题。
首先尝试最新的
mongodb + srv://
否则,如果仍然有问题,请尝试使用下面的连接字符串
“ mongodb://
第二个驱动程序字符串为我工作。
答案 2 :(得分:0)
增加socketTimeoutMS并添加authSource参数可以解决此问题。
Ex: mongodb://<user>:<pwd>@<mongoServer>:<port>/<db_name>?socketTimeoutMS=1000000&authSource=<db_name>
需要任何帮助,请联系www.codingzap.com