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

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);

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:
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" } ]
}
)
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

Related

Cannot create a mongo database with docker

I'm having trouble creating a mongo database using the docker-compose command. Docker desktop tells me that everything is up and running including the db, but all I get is the standard 'admin, config, local' not the db I want to create. Here's my docker-compose.yaml
version: '3'
services:
app:
build: ./
entrypoint: ./.docker/entrypoint.sh
ports:
- 3000:3000
volumes:
- .:/home/node/app
depends_on:
- db
db:
image: mongo:4.4.4
restart: always
volumes:
- ./.docker/dbdata:/data/db
- ./.docker/mongo:/docker-entrypoint-initdb.d
environment:
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=root
- MONGO_INITDB_DATABASE=nest
mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
environment:
- ME_CONFIG_MONGODB_SERVER=db
- ME_CONFIG_MONGODB_AUTH_USERNAME=root
- ME_CONFIG_MONGODB_AUTH_PASSWORD=root
- ME_CONFIG_MONGODB_ADMINUSERNAME=root
- ME_CONFIG_MONGODB_ADMINPASSWORD=root
depends_on:
- db
my init.js inside .docker/mongo
db.routes.insertMany([
{
_id: "1",
title: "Primeiro",
startPosition: {lat: -15.82594, lng: -47.92923},
endPosition: {lat: -15.82942, lng: -47.92765},
},
{
_id: "2",
title: "Segundo",
startPosition: {lat: -15.82449, lng: -47.92756},
endPosition: {lat: -15.82776, lng: -47.92621},
},
{
_id: "3",
title: "Terceiro",
startPosition: {lat: -15.82331, lng: -47.92588},
endPosition: {lat: -15.82758, lng: -47.92532},
}
]);
and my dockerfile
FROM node:14.18.1-alpine
RUN apk add --no-cache bash
RUN npm install -g #nestjs/cli
USER node
WORKDIR /home/node/app
and this is the 'error' log I get from docker when I run the nest container with mongodb, nest app and mongo express(there is actually a lot more but SO keeps thinking that it is spam for some reason.
about to fork child process, waiting until server is ready for connections.
Successfully added user: {
"user" : "root",
"roles" : [
{
"role" : "root",
"db" : "admin"
}
]
}
Error saving history file: FileOpenFailed Unable to open() file /home/mongodb/.dbshell: No such file or directory
{"t":{"$date":"2022-06-01T19:39:15.542+00:00"},"s":"I", "c":"NETWORK", "id":22944, "ctx":"conn2","msg":"Connection ended","attr":{"remote":"127.0.0.1:39304","connectionId":2,"connectionCount":0}}
/usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/init.js
{"t":{"$date":"2022-06-01T19:39:15.683+00:00"},"s":"I", "c":"NETWORK", "id":22943, "ctx":"listener","msg":"Connection accepted","attr":{"remote":"127.0.0.1:39310","connectionId":3,"connectionCount":1}}
{"t":{"$date":"2022-06-01T19:39:15.684+00:00"},"s":"I", "c":"NETWORK", "id":51800, "ctx":"conn3","msg":"client metadata","attr":{"remote":"127.0.0.1:39310","client":"conn3","doc":{"application":{"name":"MongoDB Shell"},"driver":{"name":"MongoDB Internal Client","version":"4.4.4"},"os":{"type":"Linux","name":"Ubuntu","architecture":"x86_64","version":"18.04"}}}}
{"t":{"$date":"2022-06-01T19:39:15.701+00:00"},"s":"I", "c":"STORAGE", "id":20320, "ctx":"conn3","msg":"createCollection","attr":{"namespace":"nest.routes","uuidDisposition":"generated","uuid":{"uuid":{"$uuid":"f689868e-af6d-4ec6-b555-dcf520f24788"}},"options":{}}}
{"t":{"$date":"2022-06-01T19:39:15.761+00:00"},"s":"I", "c":"INDEX", "id":20345, "ctx":"conn3","msg":"Index build: done building","attr":{"buildUUID":null,"namespace":"nest.routes","index":"_id_","commitTimestamp":{"$timestamp":{"t":0,"i":0}}}}
uncaught exception: ReferenceError: colection is not defined :
#/docker-entrypoint-initdb.d/init.js:23:1
failed to load: /docker-entrypoint-initdb.d/init.js
exiting with code -3
this is what running docker-compose ps shows
NAME COMMAND SERVICE STATUS PORTS
nest-api-app-1 "./.docker/entrypoin…" app running 0.0.0.0:3000->3000/tcp
nest-api-db-1 "docker-entrypoint.s…" db running 27017/tcp
nest-api-mongo-express-1 "tini -- /docker-ent…" mongo-express running 0.0.0.0:8081->8081/tcp
this what my docker desktop shows
The MongoDB container only creates a database if no database already exists. You probably already have one, which is why a new database isn't created and your initialization script isn't run.
Delete the contents of ./.docker/dbdata on the host. Then start the containers with docker-compose and Mongo should create your database for you.

Can't connection DB use ktor application

I try start ktor application with postgresql database. For it i used docker compose. My docker-compose.yml.
version: '3.0'
services:
ktor-sample:
build: ./
command: ./ktor-sample
ports:
- "8080:8080"
depends_on:
- db
db:
restart: unless-stopped
image: postgres:9.6.10-alpine
volumes:
- ./test:/var/lib/postgresql/data
ports:
- "5433:5433"
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD:
POSTGRES_DB: AutoHistory
I every one receive error:
WARN Exposed - Transaction attempt #2 failed: Connection to localhost:5433 refused.
Check that the hostname and port are correct and that the postmaster is accepting
TCP/IP connections.. Statement(s): null
ktor-sample_1 | org.postgresql.util.PSQLException: Connection to localhost:5433
refused. Check that the hostname and port are correct and that the postmaster is
accepting TCP/IP connections.
This database is created and can connection to it.
As for me I do next I create an object for initializing of db
it can look like this
object DbFactory {
fun init() {
val pool = hikari()
val db = Database.connect(pool)
transaction(db) {
SchemaUtils.create(Creators, Visitors, Places, Roles, UserRoles, Users)
}
}
private fun hikari(): HikariDataSource {
val hikariConfig = HikariConfig("db.properties")
return HikariDataSource(hikariConfig)
}
}
and in Application.kt, on the first place initialize your db
fun main() {
embeddedServer(Netty, port = 8080, host = "0.0.0.0") {
DbFactory.init()
...
the file which is describing your data should live in root and be called db.properties look something like
jdbcUrl=jdbc:postgresql://localhost:5433/AutoHistory
dataSource.driverClass = org.postresql.Driver
dataSource.driver=postgresql
dataSource.database= AutoHistory
dataSource.user= postgres
dataSource.password=
I have no such rows in docker-compose.yml
ktor-sample:
build: ./
command: ./ktor-sample
ports:
- "8080:8080"
depends_on:
- db
and do start my app manually but anyway I think it should help you

Connecting to Deno MongoDb in Docker throws uncaught promise (in wsl2)

The project runs in Deno.
I'm trying to connect the MongoClient in Deno to a MongoDb container running in Docker.
The docker container is running in wsl2 with the wsl ip 172.18.96.38 and exposing port 27017.
Connecting to it with the following code leads to an error:
import { MongoClient } from "https://deno.land/x/mongo#v0.23.0/mod.ts";
const uri = 'mongodb://admin:admin#172.18.96.38:27017'
this.mClient = new MongoClient();
await this.mClient.connect(uri);
error: Uncaught (in promise) Error: MongoError: "Connection failed: MongoError: {\"ok\":0,\"errmsg\":\"no such command: 'hello'\",\"code\":59,\"codeName\":\"CommandNotFound\"}"
throw new MongoError(`Connection failed: ${e.message || e}`);
^
at MongoClient.connect (https://deno.land/x/mongo#v0.23.0/src/client.ts:26:13)
at async Function.initMongoClient (file:///home/robert/repos/myDenoWebsite/backend/src/utils/mongoClientWrapper.ts:54:9)
According to the errmsg the error is 'no such command: 'hello'.
What does this mean?
This is my docker-compose.yml for mongo btw:
version: '3.8'
services:
mongodb:
image: mongo:4.4-bionic
container_name: my-mongodb
ports:
- 27017:27017
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=admin
volumes:
- mongodb:/data/db
- mongoconfig:/data/configdb
volumes:
mongodb:
mongoconfig:
I got a same error, so I found an alternative way
This is my solution
import { MongoClient } from "https://deno.land/x/mongo#v0.29.2/mod.ts";
const conn = async () => {
const client = new MongoClient();
const srv = `mongodb+srv://admin:admin#172.18.96.38:27017/<Your Database>?authMechanism=SCRAM-SHA-1&retryWrites=true&w=majority`
await client.connect(srv);
return client;
}
export const getData = async () => {
const conn = await conn()
<Your Logic Here>
}

Failed to connect to a mongodb docker container from mongoose on host

I'm trying to connect to mongodb running in docker from the app running on host using mongoose but it failed.
I can't use the port 27017 for the new mongodb container because it is used by other container. So I followed the guide here for setting it up using the compose.
Below are the snippets:
docker-compose.yml
version: '3'
services:
db:
image: mongo:latest
restart: always
ports:
- '8081:8081'
environment:
MONGO_INITDB_ROOT_USERNAME: root1
MONGO_INITDB_ROOT_PASSWORD: password1
But when I do docker-ps, port 27017 still there but I'm not sure if that causes an issue.
PORTS
0.0.0.0:8081->8081/tcp, 27017/tcp
Then I created a new user in admin database.
use admin
db.createUser(
{
user: "admin1",
pwd: "password2",
roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
}
)
server.js
const connectOption = {
useNewUrlParser: true,
user: 'admin1',
pass: 'password2',
authSource: 'admin',
}
const mongoURL = 'mongodb://localhost:8081/app1';
mongoose.connect(mongoURL, connectOption)
.then(() => console.log('MongoDB Connected'))
.catch(error => console.log(error));
And the error I received is
{
MongoNetworkError: failed to connect to server [localhost:8081] on first connect [MongoNetworkError: write EPIPE]
...
...
...
name: 'MongoNetworkError',
errorLabels: [ 'TransientTransactionError' ],
[Symbol(mongoErrorContextSymbol)]: {}
}
Assuming you're running nodejs application as a docker-compose service, in db service remove ports section (including - '8081:8081'
line). In server.js, change const mongoURL = 'mongodb://localhost:8081/app1'; to const mongoURL = 'mongodb://db:27017/app1';.
If you want to access the db from host machine, change ports 8081:8081 to <give-a-port-number>:27017.

Save monolog in mongodb in symfony 4

I want to add monolog in mongodb with default handler(MongoDBHandler) in Symfony 4.
my monolog.yaml file in dev folder
monolog:
handlers:
mongo:
type: mongo
mongo:
id: monolog.logger.mongo
host: '%env(MONGODB_URL)%'
database: '%env(MONGODB_DB)%'
collection: logs
my services.yaml
services:
monolog.logger.mongo:
class: Monolog\Handler\MongoDBHandler
arguments: ['#doctrine_mongodb']
my doctrine_mongodb.yaml
doctrine_mongodb:
auto_generate_proxy_classes: '%kernel.debug%'
auto_generate_hydrator_classes: '%kernel.debug%'
connections:
default:
server: '%env(MONGODB_URL)%'
options:
db: '%env(MONGODB_DB)%'
log:
server: '%env(MONGODB_URL)%'
options:
db: '%env(MONGODB_DB)%'
connect: true
default_database: '%env(MONGODB_DB)%'
document_managers:
log:
auto_mapping: false
logging: false
But doesn't work.
one of the errors:
Cannot autowire service "monolog.logger.mongo": argument "$database"
of method "Monolog\Handler\MongoDBHandler::__construct()" is
type-hinted "string", you should configure its value explicitly.
While i use database option in monolog config.
Is there any document?
Another way to enable mongodb for monolog is:
monolog:
handlers:
mongo:
type: mongo
mongo:
host: '%env(MONGODB_URL)%'
user: myuser
pass: mypass
database: '%env(MONGODB_DB)%'
collection: logs
, So it mean you need to remove id field and add user and pass instead.
If you use doctrine mongodb already, it's possible to re-use it's connection, avoiding more ENV vars to separate the DSN:
monolog:
handlers:
mongo:
type: mongo
mongo:
id: "doctrine_mongodb.odm.default_connection"
database: "%env(MONGODB_DB)%"
collection: MyLogDocument # Keeping this the same, allows you to simply use a doctrine repository to access the documents in your app if needed
level: debug
I get the following error:
Attempted to load class "MongoClient" from the global namespace.
Did you forget a "use" statement?
protected function getMonolog_Handler_MongoService()
{
$this->privates['monolog.handler.mongo'] = $instance = new \Monolog\Handler\MongoDBHandler(new \MongoClient('mongodb://admin:pass#localhost:27017'), 'monolog', 'logs', 100, true);
$instance->pushProcessor(($this->privates['monolog.processor.psr_log_message'] ?? ($this->privates['monolog.processor.psr_log_message'] = new \Monolog\Processor\PsrLogMessageProcessor())));
return $instance;
}