Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用JDBC连接到云中托管的MongoDB时出错_Java_Mongodb_Authentication_Jdbc - Fatal编程技术网

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

Java 使用JDBC连接到云中托管的MongoDB时出错,java,mongodb,authentication,jdbc,Java,Mongodb,Authentication,Jdbc,我正在尝试使用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.

我正在尝试使用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 我的开发环境: MacOSX 日食 司机: junit-3.8.1.jar mongodb-driver-3.2.2.jar mongodb-driver-core-3.2.2.jar bson-3.2.2.jar 我正在使用以下url建议的驱动程序: http://mongodb.github.io/mongo-java-driver/?_ga=1.221045400.1622521490.1456732063 实际上,驱动程序是由以下依赖项声明(由maven build)更新的: org.mongodb mongodb驱动程序 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();
}
private静态无效测试(){
字符串url=”mongodb://user:pwd@主持人:19468/heroku_dbname”;
MongoClient MongoClient=新的MongoClient(新的MongoClient(url));
//获取“heroku_dbname”数据库的句柄
MongoDatabase=mongoClient.getDatabase(“heroku_dbname”);
//掌握“书籍”收藏
MongoCollection collection=database.getCollection(“book”);
//制作文档并插入它
文档文档=新文档(“标题”、“良好习惯”)
.附加(“作者”、“阿克巴”);
托收。插入通(doc);
//得到它(因为它是我们之前放弃其余部分后唯一的一个)
Document myDoc=collection.find().first();
System.out.println(myDoc.toJson());
//释放资源
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) 2016年3月17日下午7:15:31 com.mongodb.diagnostics.logging.JULLogger log 信息:使用以下设置创建的群集{hosts=[ds019468.mlab.com:19468],mode=SINGLE,requiredClusterType=UNKNOWN,serverSelectionTimeout='30000ms',maxWaitQueueSize=500} 2016年3月17日下午7:15:31 com.mongodb.diagnostics.logging.JULLogger log 信息:WritableServerSelector没有从群集描述ClusterDescription{type=UNKNOWN,connectionMode=SINGLE,all=[ServerDescription{address=ds019468.mlab.com:19468,type=UNKNOWN,state=Connection}]中选择服务器。在超时之前等待30000毫秒 2016年3月17日下午7:15:33 com.mongodb.diagnostics.logging.JULLogger log 信息:连接到服务器ds019468时监视器线程出现异常。mlab.com:19468 com.mongodb.MongoSecurityException:MongoCredential{mechanism=null,userName='db_userName',source='heroku_dbName',password=,mechanismProperties={}验证异常 位于com.mongodb.connection.SaslAuthenticator.authenticate(SaslAuthenticator.java:61) 在com.mongodb.connection.DefaultAuthenticator.authenticate上(DefaultAuthenticator.java:32) 位于com.mongodb.connection.InternalStreamConnectionInitializer.authenticateAll(InternalStreamConnectionInitializer.java:99) 在com.mongodb.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:44)上 位于com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115) 位于com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunName.run(DefaultServerMonitor.java:128) 运行(Thread.java:745) 原因:com.mongodb.MongoCommandException:命令失败,在服务器ds019468.mlab.com:19468上出现错误18:“身份验证失败”。完整响应为{“确定”:0.0,“代码”:18,“errmsg”:“身份验证失败”。} 位于com.mongodb.connection.CommandHelper.createCommandFailureException(CommandHelper.java:170) 位于com.mongodb.connection.CommandHelper.receiveCommandResult(CommandHelper.java:123) 位于com.mongodb.connection.CommandHelper.executeCommand(CommandHelper.java:32) 位于com.mongodb.connection.SaslAuthenticator.sendsalstart(SaslAuthenticator.java:95) 位于com.mongodb.connection.SaslAuthenticator.authenticate(SaslAuthenticator.java:45) ... 还有6个 线程“main”com.mongodb.MongoTimeoutException中出现异常:等待与WritableServerSelector匹配的服务器时,30000毫秒后超时。群集状态的客户端视图为{type=UNKNOWN,servers=[{address=ds019468.mlab.com:19468,type=UNKNOWN,state=CONNECTING,exception={com.mongodb.MongoSecurityException:exception authentication mongoredential{mechanism=null,userName='db_userName',source heroku_齌dbName',password=,mechanismProperties={}}},由{com.mongodb.MongoCommandException:命令失败,错误为18:'身份验证失败。' 位于com.mongodb.connection.BaseCluster.createTimeoutException(BaseCluster.java:369) 位于com.mongodb.connection.BaseCluster.selectServer(BaseCluster.java:101) 位于com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource。(ClusterBinding.java:75) 位于com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource。(ClusterBinding.java:71) 位于com.mongodb.binding.ClusterBinding.getWriteConnectionSource(ClusterBinding.java:68) 位于com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:219) 在com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:168)上 在com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:74)上 位于com.mongodb.Mongo.execute(Mongo.java:781) 位于com.mongodb.Mongo$2.execute(Mongo.java:764) 位于com.mongodb.MongoCollectionImpl.executeSingleWriteRequest(MongoCollectionImpl.java:515) 位于com.mongodb.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:306) 位于com.mongodb.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:297) 位于com.chozhan.test.mongodb.MongoJdbcRemote.test(MongoJdbcRemote.java:64) 位于com.chozhan.test.mongodb.MongoJdbcRemote.main(MongoJdbcRemote.java:43) 我能够通过mLab web界面以相同的用户id和密码成功登录,并且工作正常

但是,只有JDBC尝试失败


有人能帮忙吗,这里有什么问题吗?

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

MongoCredential credential = MongoCredential.createCredential(user, heroku_dbname, pwd);
MongoClient mongoClient = new MongoClient(new ServerAddress(), Arrays.asList(credential));
或者只需更新您的URL:

MongoClientURI uri = new MongoClientURI("mongodb://user1:pwd1@host1/?authSource=db1");
问题是
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