我是 Mongo DB 数据库的新手,需要帮助建立 JDBC 连接,我已按照在线提供的说明进行操作,但出现了一些错误。
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.ServerAddress;
import com.mongodb.MongoCredential;
import com.mongodb.MongoClientOptions;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import java.util.Arrays;
import org.bson.Document;
public class myMongoDbCollection {
public static void main( String args[] ) {
MongoClient mongoClient = new MongoClient ( new MongoClientURI("mongodb://Sudhanva:<password>@cluster0-shard-00-00-angnd.mongodb.net:27017,cluster0-shard-00-01-angnd.mongodb.net:27017,cluster0-shard-00-02-angnd.mongodb.net:27017/test?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin&retryWrites=true&w=majority"));
// Creating a Mongo client
// Creating Credentials
String user="Sudhanva"; // the user name
String database="SACCIDANANDA_DB"; // the name of the database in which the user is defined
String passwordtemp = "sudhanva123";
char[] password =passwordtemp.toCharArray();
MongoCredential credential = MongoCredential.createCredential(user, database, password);
mongoClient = new MongoClient (new ServerAddress(),Arrays.asList(credential));
MongoClientOptions options = MongoClientOptions.builder().sslEnabled(true).build();
MongoDatabase database1 = mongoClient.getDatabase("SACCIDANANDA_DB");
MongoCollection<Document> coll = database1.getCollection("DEVICE_REGISTRY");
for (String name : database1.listCollectionNames()) {
System.out.println(name);
}
}
}
我收到此错误 -
Jun 14, 2019 4:47:22 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Cluster created with settings {hosts=[cluster0-shard-00-00-angnd.mongodb.net:27017, cluster0-shard-00-01-angnd.mongodb.net:27017, cluster0-shard-00-02-angnd.mongodb.net:27017], mode=MULTIPLE, requiredClusterType=REPLICA_SET, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500, requiredReplicaSetName='Cluster0-shard-0'}
Jun 14, 2019 4:47:22 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Adding discovered server cluster0-shard-00-00-angnd.mongodb.net:27017 to client view of cluster
Jun 14, 2019 4:47:22 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Adding discovered server cluster0-shard-00-01-angnd.mongodb.net:27017 to client view of cluster
Jun 14, 2019 4:47:22 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Adding discovered server cluster0-shard-00-02-angnd.mongodb.net:27017 to client view of cluster
Jun 14, 2019 4:47:22 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Cluster created with settings {hosts=[127.0.0.1:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
Jun 14, 2019 4:47:22 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Cluster description not yet available. Waiting for 30000 ms before timing out
Jun 14, 2019 4:47:22 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Opened connection [connectionId{localValue:4, serverValue:8}] to 127.0.0.1:27017
Jun 14, 2019 4:47:22 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Monitor thread successfully connected to server with description ServerDescription{address=127.0.0.1:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[4, 0, 10]}, minWireVersion=0, maxWireVersion=7, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=4062432}
Jun 14, 2019 4:47:22 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Closed connection [connectionId{localValue:5}] to 127.0.0.1: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='Sudhanva', source='SACCIDANANDA_DB', 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:156)
at com.mongodb.internal.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:63)
at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:129)
at com.mongodb.internal.connection.UsageTrackingInternalConnection.open(UsageTrackingInternalConnection.java:50)
at com.mongodb.internal.connection.DefaultConnectionPool$PooledConnection.open(DefaultConnectionPool.java:398)
at com.mongodb.internal.connection.DefaultConnectionPool.get(DefaultConnectionPool.java:115)
at com.mongodb.internal.connection.DefaultConnectionPool.get(DefaultConnectionPool.java:101)
at com.mongodb.internal.connection.DefaultServer.getConnection(DefaultServer.java:85)
at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.getConnection(ClusterBinding.java:124)
at com.mongodb.operation.ListCollectionsOperation$1.call(ListCollectionsOperation.java:234)
at com.mongodb.operation.ListCollectionsOperation$1.call(ListCollectionsOperation.java:231)
at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:463)
at com.mongodb.operation.ListCollectionsOperation.execute(ListCollectionsOperation.java:231)
at com.mongodb.operation.ListCollectionsOperation.execute(ListCollectionsOperation.java:86)
at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:181)
at com.mongodb.client.internal.MongoIterableImpl.execute(MongoIterableImpl.java:138)
at com.mongodb.client.internal.MongoIterableImpl.iterator(MongoIterableImpl.java:92)
at com.mongodb.client.internal.MappingIterable.iterator(MappingIterable.java:39)
at myMongoDbCollection.main(myMongoDbCollection.java:33)
Caused by: com.mongodb.MongoCommandException: Command failed with error 18 (AuthenticationFailed): 'Authentication failed.' on server 127.0.0.1:27017. The full response is {"ok": 0.0, "errmsg": "Authentication failed.", "code": 18, "codeName": "AuthenticationFailed"}
at com.mongodb.internal.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:187)
at com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:303)
at com.mongodb.internal.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:259)
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.sendSaslStart(SaslAuthenticator.java:130)
at com.mongodb.internal.connection.SaslAuthenticator.access$100(SaslAuthenticator.java:40)
at com.mongodb.internal.connection.SaslAuthenticator$1.run(SaslAuthenticator.java:54)
... 22 more
C:\Users\sudha\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 5 seconds)