Jmeter MongoDb становится несанкционированным - PullRequest
0 голосов
/ 27 января 2020

Я пытаюсь вставить данные (нагрузочный тест) из 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";

1 Ответ

1 голос
/ 27 января 2020

Вы создаете экземпляр MongoCredential , но не передаёте его в 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();

Ознакомьтесь со следующими материалами:

В дальнейшем было бы неплохо включить точную версию вашего Пн go Java Драйвер , так как API может меняться от выпуска к выпуску и инструкции действительны для версии драйвера 3.6 не будет работать для версии драйвера 4.0 и наоборот

...