Jmeter MongoDb 获得未授权

Jmeter MongoDb Getting Unauthorised

我正在尝试将数据(负载测试)从 jmeter 插入到 mongodb 出现未经授权的错误,但它已连接到数据库。 连接代码:

String mongoUser = "user"
String userDB = "mysyt"
char[] password = "password".toCharArray();

MongoCredential credential = MongoCredential.createCredential(mongoUser, userDB, password);

    MongoClientSettings settings = MongoClientSettings.builder()
        .applyToClusterSettings {builder -> 
            builder.hosts(Arrays.asList(new ServerAddress("xxx.xx.xxx.xx",27017)))}
        .build();

    MongoClient mongoClient = MongoClients.create(settings);

    MongoDatabase database = mongoClient.getDatabase("mysyt");
    MongoCollection<Document> collection = database.getCollection("user");

    vars.putObject("collection", collection);


Error:

Response code:500 Response message:Exception: com.mongodb.MongoCommandException: Command failed with error 13 (Unauthorized): 'command insert requires authentication' on server xx.xxx.xx.xxx:27017. The full response is {"operationTime": {"$timestamp": {"t": 1580126230, "i": 1}}, "ok": 0.0, "errmsg": "command insert requires authentication", "code": 13, "codeName": "Unauthorized", "$clusterTime": {"clusterTime": {"$timestamp": {"t": 1580126230, "i": 1}}, "signature": {"hash": {"$binary": "j7ylgmDSaPsZQRX/SwPTo4ZSTII=", "$type": "00"}, "keyId": {"$numberLong": "6785074748788310018"}}}}

如果我这样配置

MongoClient mongoClient = MongoClients.create("mongodb://user:password@xx.xxx.xx.xxx:27017/?authSource=mysyt&ssl=true");

    //MongoClient mongoClient = MongoClients.create(settings);

    MongoDatabase database = mongoClient.getDatabase("mysyt");
    MongoCollection<Document> collection = database.getCollection("user");

    vars.putObject("collection", collection);

错误:

Response message:Exception: com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=xx.xxx.xx.xxx:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketWriteException: Exception sending message}, caused by {javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake}, caused by {java.io.EOFException: SSL peer shut down incorrectly}}]

插入代码:

集合名称已在用户定义的变量(TestPlan)中配置

 MongoCollection<Document> collection = vars.getObject("collection");

    Document document = new Document("userId", "user1").append("userName", "kohli");
    collection.insertOne(document);

    return "Document inserted";

您正在创建 MongoCredential but not passing it to the MongoClientSettings.Builder 的实例,正确的代码应该是这样的:

MongoClientSettings settings = MongoClientSettings.builder()
        .applyToClusterSettings { builder ->
            builder.hosts(Arrays.asList(new ServerAddress("xxx.xx.xxx.xx", 27017)))
        }
        .credential(credential) // this line is essential
        .build();

查看以下内容material:

展望未来,最好包括您的 Mongo Java Driver 的确切版本,因为 API 可能会因版本而异,并且对驱动程序版本 3.6 有效的说明不适用于该驱动程序版本4.0 反之亦然