我正在尝试使用JDBC连接到托管在云中的MongoDB。 但是,身份验证失败。
My development environment: Mac OSX Eclipse Drivers: junit-3.8.1.jar mongodb-driver-3.2.2.jar mongodb-driver-core-3.2.2.jar bson-3.2.2.jar I'm using the driver as suggested by the below url: http://mongodb.github.io/mongo-java-driver/?_ga=1.221045400.1622521490.1456732063 Actually, the drivers are updated by the below dependency declaration, by maven build: org.mongodb mongodb-driver 3.2.2
这是我的Java代码:
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 log INFO: 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 log INFO: 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 out Mar 17, 2016 7:15:33 PM com.mongodb.diagnostics.logging.JULLogger log INFO: Exception in monitor thread while connecting to server ds019468.mlab.com:19468 com.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 more Exception 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尝试失败。
有人可以帮忙吗,这里有什么问题?
答案 0 :(得分:1)
创建客户端连接时,需要添加身份验证详细信息。
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");
答案 1 :(得分:0)
问题由mLab支持整理出来。我使用的是mLab的“web界面用户ID密码”。我被建议创建一个“单独的数据库用户ID /密码”来连接。它使用新创建的数据库用户ID /密码。
答案 2 :(得分:0)
对我有用的是将application.properties
中的URL更改为:
spring.data.mongodb.uri=mongodb://username:password@host:port/database_name
收件人:
spring.data.mongodb.uri=mongodb+srv://username:password@host:port/database?retryWrites=true&w=majority
我了解到**MongoDB**
的默认数据库是test
。
在我的应用中,我将host:port
替换为从 mongodb.com 获得的URI,尽管URL不包含{{1 }}。