MongoDB Atlas-Java连接不起作用

时间:2019-03-20 19:44:07

标签: mongodb mongodb-atlas

我正在尝试从Java连接到mongodb Atlas集群,这会产生以下错误:

Caused by: com.mongodb.MongoCommandException: Command failed with error 8000 (AtlasError): 'Authentication failed.' on server cluster0-shard-00-00-wgrdw.gcp.mongodb.net:27017. The full response is {"ok": 0, "errmsg": "Authentication failed.", "code": 8000, "codeName": "AtlasError"}
at com.mongodb.internal.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:179)
at com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:299)
at com.mongodb.internal.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:255)
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)
... 23 more

我的地图集配置:

  • 用户:admin,密码:...
  • ip列入白名单:0.0.0.0/0(用于测试)
  • mongodb版本:4.0.6
  • java驱动程序:implementation (group: 'org.mongodb', name: 'mongo-java-driver', version: '3.10.1')

Java代码:

List<ServerAddress> seeds = new ArrayList<>();
        seeds.add(new ServerAddress("cluster0-shard-00-00-wgrdw.gcp.mongodb.net", 27017));
        seeds.add(new ServerAddress("cluster0-shard-00-01-wgrdw.gcp.mongodb.net", 27017));
        seeds.add(new ServerAddress("cluster0-shard-00-02-wgrdw.gcp.mongodb.net", 27017));

        MongoCredential credential = MongoCredential.createCredential(USER, "vehicle-data", PWD.toCharArray());

        MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
        //build the connection options
        builder.maxConnectionIdleTime(60000);//set the max wait time in (ms)
        builder.sslEnabled(true);
        MongoClientOptions opts = builder.build();

        MongoClient mongoClient = new MongoClient(seeds, credential, opts);

        MongoDatabase database = mongoClient.getDatabase("vehicle-data");
        MongoCollection<Document> segments = database.getCollection("road-segments");
        // Delete All documents from collection Using blank BasicDBObject
        segments.deleteMany(new BasicDBObject());

有人知道我想念什么吗?

1 个答案:

答案 0 :(得分:0)

(在地图集帮助中心的帮助下)解决了该问题:

将服务器/节点配置更改为:

MongoClientURI uri = new MongoClientURI(
                "" +
                        "mongodb://admin:<pwd>@" +
                        "cluster0-shard-00-00-wgrdw.gcp.mongodb.net:27017," +
                        "cluster0-shard-00-01-wgrdw.gcp.mongodb.net:27017," +
                        "cluster0-shard-00-02-wgrdw.gcp.mongodb.net:27017/vehicle-data?" +
                        "ssl=true&replicaSet=Cluster0-shard-0&authSource=admin&retryWrites=true" +
                        ""
        );
        MongoClient mongoClient = new MongoClient(uri);
        MongoDatabase database = mongoClient.getDatabase("vehicle-data");

现在可以使用