gpt4 book ai didi

java - 使用 JDBC 连接到云中托管的 MongoDB 时出错

转载 作者:可可西里 更新时间:2023-11-01 10:42:26 30 4
gpt4 key购买 nike

我正在尝试使用 JDBC 连接到云中托管的 MongoDB。但是,认证失败。

My development environment:Mac OSXEclipseDrivers:junit-3.8.1.jarmongodb-driver-3.2.2.jarmongodb-driver-core-3.2.2.jarbson-3.2.2.jarI'm using the driver as suggested by the below url:http://mongodb.github.io/mongo-java-driver/?_ga=1.221045400.1622521490.1456732063Actually, the drivers are updated by the below dependency declaration, by maven build:         org.mongodb     mongodb-driver     3.2.2     

Here is my Java code:

private static void test() {


String url = "mongodb://user:pwd@host:19468/heroku_dbname";
MongoClient mongoClient = new MongoClient(new MongoClientURI(url));


// get handle to "heroku_dbname" database
MongoDatabase database = mongoClient.getDatabase("heroku_dbname");


// get a handle to the "book" collection
MongoCollection<Document> collection = database.getCollection("book");

// make a document and insert it
Document doc = new Document("title", "Good Habits")
.append("author", "Akbar");

collection.insertOne(doc);

// get it (since it's the only one in there since we dropped the rest earlier on)
Document myDoc = collection.find().first();
System.out.println(myDoc.toJson());

// release resources
mongoClient.close();
}

当我执行时,出现以下异常:

Mar 17, 2016 7:15:31 PM com.mongodb.diagnostics.logging.JULLogger logINFO: Cluster created with settings {hosts=[ds019468.mlab.com:19468], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}Mar 17, 2016 7:15:31 PM com.mongodb.diagnostics.logging.JULLogger logINFO: No server chosen by WritableServerSelector from cluster description ClusterDescription{type=UNKNOWN, connectionMode=SINGLE, all=[ServerDescription{address=ds019468.mlab.com:19468, type=UNKNOWN, state=CONNECTING}]}. Waiting for 30000 ms before timing outMar 17, 2016 7:15:33 PM com.mongodb.diagnostics.logging.JULLogger logINFO: Exception in monitor thread while connecting to server ds019468.mlab.com:19468com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=null, userName='db_userName', source='heroku_dbName', password=, mechanismProperties={}}    at com.mongodb.connection.SaslAuthenticator.authenticate(SaslAuthenticator.java:61)    at com.mongodb.connection.DefaultAuthenticator.authenticate(DefaultAuthenticator.java:32)    at com.mongodb.connection.InternalStreamConnectionInitializer.authenticateAll(InternalStreamConnectionInitializer.java:99)    at com.mongodb.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:44)    at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115)    at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:128)    at java.lang.Thread.run(Thread.java:745)Caused by: com.mongodb.MongoCommandException: Command failed with error 18: 'Authentication failed.' on server ds019468.mlab.com:19468. The full response is { "ok" : 0.0, "code" : 18, "errmsg" : "Authentication failed." }    at com.mongodb.connection.CommandHelper.createCommandFailureException(CommandHelper.java:170)    at com.mongodb.connection.CommandHelper.receiveCommandResult(CommandHelper.java:123)    at com.mongodb.connection.CommandHelper.executeCommand(CommandHelper.java:32)    at com.mongodb.connection.SaslAuthenticator.sendSaslStart(SaslAuthenticator.java:95)    at com.mongodb.connection.SaslAuthenticator.authenticate(SaslAuthenticator.java:45)    ... 6 moreException in thread "main" com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches WritableServerSelector. Client view of cluster state is {type=UNKNOWN, servers=[{address=ds019468.mlab.com:19468, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=null, userName='db_userName', source='heroku_dbName', password=, mechanismProperties={}}}, caused by {com.mongodb.MongoCommandException: Command failed with error 18: 'Authentication failed.' on server ds019468.mlab.com:19468. The full response is { "ok" : 0.0, "code" : 18, "errmsg" : "Authentication failed." }}}]    at com.mongodb.connection.BaseCluster.createTimeoutException(BaseCluster.java:369)    at com.mongodb.connection.BaseCluster.selectServer(BaseCluster.java:101)    at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.(ClusterBinding.java:75)    at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.(ClusterBinding.java:71)    at com.mongodb.binding.ClusterBinding.getWriteConnectionSource(ClusterBinding.java:68)    at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:219)    at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:168)    at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:74)    at com.mongodb.Mongo.execute(Mongo.java:781)    at com.mongodb.Mongo$2.execute(Mongo.java:764)    at com.mongodb.MongoCollectionImpl.executeSingleWriteRequest(MongoCollectionImpl.java:515)    at com.mongodb.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:306)    at com.mongodb.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:297)    at com.chozhan.test.mongodb.MongoJdbcRemote.test(MongoJdbcRemote.java:64)    at com.chozhan.test.mongodb.MongoJdbcRemote.main(MongoJdbcRemote.java:43)

我能够通过 mLab 网络界面使用相同的用户 ID 和密码成功登录,并且工作正常。

但是,只有 JDBC 尝试失败。

谁能帮忙,这是什么问题?

最佳答案

当您创建客户端连接时,您需要添加身份验证详细信息。

MongoCredential credential = MongoCredential.createCredential(user, heroku_dbname, pwd);
MongoClient mongoClient = new MongoClient(new ServerAddress(), Arrays.asList(credential));

或者只是更新您的网址:

MongoClientURI uri = new MongoClientURI("mongodb://user1:pwd1@host1/?authSource=db1");

关于java - 使用 JDBC 连接到云中托管的 MongoDB 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36062894/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com