Issue while connecting JMeter to MongoDb: com.mongodb.CommandFailureException … Authentication failed

匆匆过客 提交于 2020-06-29 04:24:27

问题


This is how I am connecting successfully from Spring Data to MongoDb running on my local Docker:

aplication.yml:

spring:
  data:
    mongodb:
      host: localhost
      port:  27017
      database: demodb
      authentication-database: admin
      username: root
      password: rootpassword

Unfortunatelly I am stuck to connect from JMeter to same MongoDb. After several searches around I reached this Grovy Code (it is my first time using Grovy):

import com.mongodb.*
import com.mongodb.BasicDBObject
import org.bson.*

MongoCredential coreCredential = MongoCredential.createCredential("root", "demodb", "rootpassword".toCharArray());
MongoClient coreMongoClient = new MongoClient(new ServerAddress("localhost", 27017), Arrays.asList(coreCredential));
DB coreDB = coreMongoClient.getDB("demodb");
DBCollection coll = coreDB.getCollection("transfer");

BasicDBObject document = new BasicDBObject("id", "3")
.append("origem", "Jimis")
.append("destino", "drpc")
.append("status", 1);

coreDB.getCollection('transfer').insert(document);

Complete error:

-06 18:59:39,561 INFO o.a.j.t.JMeterThread: Thread started: Thread Group 1-1
2020-04-06 18:59:39,588 ERROR o.a.j.p.j.s.JSR223Sampler: Problem in JSR223 script JSR223 Sampler, message: javax.script.ScriptException: com.mongodb.CommandFailureException: { "serverUsed" : "localhost:27017" , "ok" : 0.0 , "errmsg" : "Authentication failed." , "code" : 18 , "codeName" : "AuthenticationFailed"}
javax.script.ScriptException: com.mongodb.CommandFailureException: { "serverUsed" : "localhost:27017" , "ok" : 0.0 , "errmsg" : "Authentication failed." , "code" : 18 , "codeName" : "AuthenticationFailed"}
    at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:324) ~[groovy-all-2.4.16.jar:2.4.16]
    at org.codehaus.groovy.jsr223.GroovyCompiledScript.eval(GroovyCompiledScript.java:72) ~[groovy-all-2.4.16.jar:2.4.16]
    at javax.script.CompiledScript.eval(CompiledScript.java:89) ~[java.scripting:?]
    at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:223) ~[ApacheJMeter_core.jar:5.2.1]
    at org.apache.jmeter.protocol.java.sampler.JSR223Sampler.sample(JSR223Sampler.java:71)

PS.: I read other stackoverflow answer I should update JMeter MongoDb jar so I did (now is mongo-java-driver-2.13.2.jar)

*** Edited trying third Dmitri's suggestion

import com.mongodb.*
MongoClient mongoClient = MongoClients.create('mongodb://root:rootpassword@localhost/?authSource=admin')

BasicDBObject document = new BasicDBObject("id", "5")
.append("origem", "Jimis")
.append("destino", "drpc")
.append("status", 1);

coreDB.getCollection('transfer').insert(document);

*** Added after taking care of first Dmitri's suggestion:

docker-compose.yml

version: '3.7'

services:
  mongo-express:
    image: mongo-express
    ports:
      - 8081:8081
    environment:
      ME_CONFIG_MONGODB_SERVER: mongo-db
      ME_CONFIG_BASICAUTH_USERNAME: admin
      ME_CONFIG_BASICAUTH_PASSWORD: q
      ME_CONFIG_MONGODB_PORT: 27017
      ME_CONFIG_MONGODB_ADMINUSERNAME: root
      ME_CONFIG_MONGODB_ADMINPASSWORD: rootpassword
    depends_on:
      - mongo-db      

    networks:
      - mongo-compose-network

  mongo-db:
    image: mongo:latest
    environment:
      MONGO_INITDB_DATABASE: demodb  
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: rootpassword
    ports:
      - "27017:27017"
    volumes:
      - mongodb_data_container:/data/db
    networks:
      - mongo-compose-network

networks: 
    mongo-compose-network:
      driver: bridge

volumes:
  mongodb_data_container: 

*** FINAL SOLUTION

I guess I had done these groups of mistakes or one of them:

  • I did import especifically the class I want use. It seems import com.mongodb.* isn't working as I expected.
  • I didn't add the MongoDb Java Driver on lib/ext. Now I added in both lib and lib/ext folders
  • ssl = true probably is the most significant mistake since I am using Docker and I didn't open ssl

BTW, here is my solution for future readers:

import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import java.util.Arrays;

//MongoClient mongoClient = MongoClients.create("mongodb://root:rootpassword@localhost:27017/?authSource=userdb&ssl=false");
//MongoClient mongoClient = MongoClients.create("mongodb://root:rootpassword@localhost:27017/?ssl=false");
MongoClient mongoClient = MongoClients.create("mongodb://root:rootpassword@localhost:27017");

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

//Document document = new Document("firstName", "Expert")
//.append("lastName", "Protocolson")
//.append("age", 37)
//.append("occupation", "DevOps")
//.append("skills", Arrays.asList("System Administration", "Linux"))
//.append("address", new Document("city", "Systemberg")
//.append("street", "Data Line")
//.append("house", 42));

Document document = new Document("_id", 7)
.append("origem", "Jimis")
.append("destino", "drpc")
.append("valor", "999")
.append("status", 3);

collection.insertOne(document);

回答1:


Not knowing the details of your MongoDB docker image it is not possible to provide a comprehensive answer, so far I can only think of the following next steps:

  1. Ensure that your MongoDB Java Driver matches the version of your MongoDB server
  2. You might need to explicitly add the user to your database in the Mongo Shell like:

    db.createUser(
            {
                user: "root",
                pwd: "rootpassword",
                roles: [ { role: "dbOwner", db: "demodb" } ]
            }
    )
    
  3. You can try out alternative approach of passing the credentials by embedding it into the MongoDB Connection String like

    MongoClient mongoClient = MongoClients.create('mongodb://root:rootpassword@localhost/?authSource=admin')
    

More information: MongoDB Performance Testing with JMeter



来源:https://stackoverflow.com/questions/61074361/issue-while-connecting-jmeter-to-mongodb-com-mongodb-commandfailureexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!