0

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: 

enter image description here

*** 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 Answer 1

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

Sign up to request clarification or add additional context in comments.

5 Comments

Using your third suggestion, I got groovy.lang.MissingPropertyException: No such property: MongoClients for class. How do I import MongoClients (I past above how I am trying it).
Probably very silly question: what does 'mongodb' stans for in "... create('mongodb://root..." Is the database whichin my case is "demodb" or a specific fixed name standing for the protocol used? I believe I must replace it with my real database name. IN other words, in my case is "MongoClients.create('demodb://root:rootpassword@localhost/?authSource=admin')", right?
Regard your first recommendation, searching in maven directory I find as the latest version mvnrepository.com/artifact/org.mongodb/mongo-java-driver/3.12.2 (3.12.2). And this is hte exact one in my JMeter folder (apache-jmeter-5.2.1\lib\mongo-java-driver-2.13.2.jar). Nevertheless, looking at my MongoDb raised by docker-compose I see MONGO_VERSION 4.2.5. Would that really be an issue? I added above my docker compose and and its MongoDb version
According to mongodb.github.io/mongo-java-driver/4.0/upgrading, javadriver 3.12 should be compatible with MongoDb 4
I added above my solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.