将JMeter连接到MongoDb时出现问题:com.mongodb.CommandFailureException ...身份验证失败 [英] Issue while connecting JMeter to MongoDb: com.mongodb.CommandFailureException ... Authentication failed
问题描述
这是我如何从Spring Data成功连接到在本地Docker上运行的MongoDb的方式:
This is how I am connecting successfully from Spring Data to MongoDb running on my local Docker:
aplication.yml:
aplication.yml:
spring:
data:
mongodb:
host: localhost
port: 27017
database: demodb
authentication-database: admin
username: root
password: rootpassword
不幸的是,我无法从JMeter连接到同一MongoDb.经过几次搜索后,我到达了此Grovy代码(这是我第一次使用Grovy):
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);
完全错误:
-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:我读了其他stackoverflow答案我应该这样更新JMeter MongoDb jar(现在是mongo-java-driver-2.13.2.jar)
PS.: I read other stackoverflow answer I should update JMeter MongoDb jar so I did (now is mongo-java-driver-2.13.2.jar)
***编辑尝试了第三个Dmitri的建议
*** 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);
***在照顾了第一个Dmitri的建议之后添加:
*** Added after taking care of first Dmitri's suggestion:
docker-compose.yml
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:
***最终解决方案
我想我犯了这些错误组或其中之一:
I guess I had done these groups of mistakes or one of them:
- 我确实专门导入了我要使用的类.似乎导入com.mongodb.*无法正常工作.
- 我没有在lib/ext上添加MongoDb Java驱动程序.现在我同时在lib和lib/ext文件夹中添加了
- ssl = true可能是最严重的错误,因为我使用的是Docker且未打开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);
推荐答案
不知道您的 MongoDB docker镜像无法提供全面的答案,到目前为止,我只能想到以下后续步骤:
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:
- 确保您的 MongoDB Java驱动程序与您的MongoDB服务器的版本匹配
-
您可能需要将用户明确添加到 Mongo Shell a>喜欢:
- Ensure that your MongoDB Java Driver matches the version of your MongoDB server
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" } ]
}
)
这篇关于将JMeter连接到MongoDb时出现问题:com.mongodb.CommandFailureException ...身份验证失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!