Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.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
Mongodb 无法从java驱动程序连接到MongoLab_Mongodb_Mlab_Mongo Java Driver - Fatal编程技术网

Mongodb 无法从java驱动程序连接到MongoLab

Mongodb 无法从java驱动程序连接到MongoLab,mongodb,mlab,mongo-java-driver,Mongodb,Mlab,Mongo Java Driver,我有一个Mongolab帐户,该帐户中只有一个用户 我可以使用MongoChef工具连接到DB,但是当我尝试使用java驱动程序3.2.0从Eclipse中连接到该DB时,我不断地遇到身份验证失败。我得到以下错误。我在Java7上 编辑 用于连接MongoLab托管的MongoDB实例的Java代码。 String mongoURL = new StringBuilder().append("mongodb://").append(mongoUserName).append(":").a

我有一个Mongolab帐户,该帐户中只有一个用户

我可以使用MongoChef工具连接到DB,但是当我尝试使用java驱动程序3.2.0从Eclipse中连接到该DB时,我不断地遇到身份验证失败。我得到以下错误。我在Java7上

编辑 用于连接MongoLab托管的MongoDB实例的Java代码。

    String mongoURL = new StringBuilder().append("mongodb://").append(mongoUserName).append(":").append(mongoPassword).append("@").append(mongoServer).append(":").append(mongoPort).append("/").append(dbName).toString();
    System.out.println(" The Mongo URL is " +mongoURL);
    MongoClientURI uri = new MongoClientURI(mongoURL);
    MongoClient mongo = new MongoClient(uri);
 com.mongodb.MongoTimeoutException: Timed out after 30000 ms while
 waiting for a server that matches
 ReadPreferenceServerSelector{readPreference=primary}. Client view of
 cluster state is {type=UNKNOWN,
 servers=[{address=ds061974.mongolab.com:61974, type=UNKNOWN,
 state=CONNECTING, exception={com.mongodb.MongoSecurityException:
 Exception authenticating MongoCredential{mechanism=null,
 userName='xxxx', source='hidden', password=<hidden>,
 mechanismProperties={}}}, caused by
 {com.mongodb.MongoCommandException: Command failed with error 18:
 'Authentication failed.' on server ds061974.mongolab.com:61974. The
 full response is { "ok" : 0.0, "code" : 18, "errmsg" : "Authentication
 failed." }}}]
尝试连接时收到异常。

    String mongoURL = new StringBuilder().append("mongodb://").append(mongoUserName).append(":").append(mongoPassword).append("@").append(mongoServer).append(":").append(mongoPort).append("/").append(dbName).toString();
    System.out.println(" The Mongo URL is " +mongoURL);
    MongoClientURI uri = new MongoClientURI(mongoURL);
    MongoClient mongo = new MongoClient(uri);
 com.mongodb.MongoTimeoutException: Timed out after 30000 ms while
 waiting for a server that matches
 ReadPreferenceServerSelector{readPreference=primary}. Client view of
 cluster state is {type=UNKNOWN,
 servers=[{address=ds061974.mongolab.com:61974, type=UNKNOWN,
 state=CONNECTING, exception={com.mongodb.MongoSecurityException:
 Exception authenticating MongoCredential{mechanism=null,
 userName='xxxx', source='hidden', password=<hidden>,
 mechanismProperties={}}}, caused by
 {com.mongodb.MongoCommandException: Command failed with error 18:
 'Authentication failed.' on server ds061974.mongolab.com:61974. The
 full response is { "ok" : 0.0, "code" : 18, "errmsg" : "Authentication
 failed." }}}]
com.mongodb.MongoTimeoutException:30000毫秒后超时,而
正在等待匹配的服务器
ReadPreferenceServerSelector{readPreference=primary}。的客户端视图
群集状态为{type=UNKNOWN,
servers=[{address=ds061974.mongolab.com:61974,type=UNKNOWN,
状态=正在连接,异常={com.mongodb.MongoSecurityException:
验证MongoCredential{mechanism=null的异常,
用户名='xxxx',源='hidden',密码=,
mechanismProperties={},由
{com.mongodb.MongoCommandException:命令失败,出现错误18:
服务器ds061974.mongolab.com:61974上的“身份验证失败”。验证失败
完整响应为{“确定”:0.0,“代码”:18,“errmsg”:“身份验证”
失败。“}}]
尝试以下代码:

ServerAddress addr = new ServerAddress(mongoServer, mongoPort);
MongoCredential credential = MongoCredential.createMongoCredential(
        mongoUserName, mongoPassword, dbName);

MongoClient mongoClient = new MongoClient(addr, Arrays.asList(credential), null);

您可以分享您在何处创建凭据吗?在问题主体中添加了用于连接MongoDB实例的java代码。您的用户位于何处?在admin db或您连接到的db中?@Sagar Reddy,它位于我连接到的db中,我使用的URI格式与MongoLab提到的相同。请发电子邮件给我们在support@mlab.com我们很乐意帮你解决这个问题