Kubernetes mongo DB user creation - mongodb

I am trying to create a Mongo db user using the following method via Jenkins Job
kubectl exec -i ${POD} -- sh -c "mongo --eval 'db.createUser({user:"DBusernmae",pwd:"test",roles:[{role:"dbAdmin",db:"training"}]})'"
receiving following error
2021-02-09T10:50:38.641+0000 E QUERY [js] ReferenceError: DBusernmae is not defined :
Please help me on this

You have to use proper quotation: your first " starts with mongo and ends with DBusernmae

You need to escape the quote ":
kubectl exec -i ${POD} -- sh -c \
"mongo \"mongodb://admin:pwd#localhost:27017\" --eval 'db.createUser({user:\"DBusernmae\",pwd:\"test\",roles:[{role:\"dbAdmin\",db:\"training\"}]})'"
I'd be grateful if somebody found a nice way to do this that doesn't require the community or enterprise operators though.

Related

Use [DB] for Mongodb with shell

I am researching now the simple "use" command for the mongo command. Please help me.
I just want save the my query in a file, but before that i need to connect to a certain database. For that i tried to find a "use" command like in sql, but could not find anything.
I just want to execute something like
mongo ....--use [db] --eval 'db.find' > save.query
In your question, you didn't specify what platform you're using, are you using Linux? Windows? Anyway, if you want to use the command line for mongo db then I would recommend to use the mongodb shell. Download the mongodb shell https://www.mongodb.com/try/download/shell and select what platform you're using.
https://www.mongodb.com/docs/mongodb-shell/run-commands/#mongosh-usage
That sais you have use <database> command
I just got it. You can just add the database name:
mongo [dbname] --host etc.
and it worked.
This is the easiest way in linux:
Option 1 ( command line params )
echo "db.exampleollection.find({}).forEach(function(d){printjson(d)})" | mongo --quiet exampledatabase --host "examplehost" --port "examplePort" --authenticationDatabase=admin -u "exampleuser" -p "examplepassword" > output.json
Explained:
Send the command you need to execute via echo to the mongo shell and redirect the output to the result file.
Add the option --quiet to suppress the shell printed info
You can provide the database directly in as comman line argument.
Option 2 : same way but URI format:
echo "show collections" | mongo "mongodb://user:pass#host:port/database?authSource=admin" --quiet

Reference Error when inserting a string with eval into mongodb

I would like to insert data into my mongodb from the command line. The mongodb is running in a docker container.
I want to do the following steps:
Enter docker container
Insert data into mongodb
Exit container
The command that I am using is the following:
docker exec -i <id-of-mongo-container> sh -c "mongo --eval 'db.rules.insert({ description: "test123"})'"
But this throws the following exception:
[js] uncaught exception: ReferenceError: test123 is not defined
I think it is an issue with the quote mark mix, but I am not able to figure out how to get it right. Does someone have an idea on how to fix this?
Research
Something I found from research is that if you swap the string "test123" with a number e.g. 2. Than the command does work.
Therefore, the following command does work.
docker exec -i <id-of-mongo-container> sh -c 'mongo admin --eval "db.rules.insert({ description: 2})"'
It also seems to related to this issue, but it is different because there is an extra set of qoutes from the docker exec.
Cheers!

Problem with nested quotes in bash: mongodump with query in a docker container via ssh

I'm backing up my database that's in a docker container, and since the total filesize is too large to fit onto the remaining disk space I execute it via SSH and dump it onto my local pc with this command (I'm using Ubuntu default bash):
docker-machine ssh my-machine-image "docker exec container-id /bin/sh -c 'mongodump --archive -u=admin --authenticationDatabase=admin -p=mongo-pwd --db=my-db --collection=my-collection --gzip'" > myfile.dump
This works pretty great, however I'm having trouble getting it to work with the --query command. Mongodump requires it to be in strict JSON and I'm having trouble with getting the nested quotes in bash to work. My most successful attempt (aka it actually successfuly executed the command instead of returning a syntax/JSON error) was with a string literal like this, however that seems to parse the JSON wrong, since it always returns 0 documents, no matter the query:
docker-machine ssh my-machine-image "docker exec container-id /bin/sh -c $'mongodump --archive -u=admin --authenticationDatabase=admin -p=mongo-pwd --db=my-db --collection=my-collection --query=\'{ \"_id\": { \"$oid\": \"some_random_object_id\" } }\' --gzip'" > myfile.dump
What is the correct way to pass strict JSON to the --query parameter with this amount of nested quotes?
Since you have multiple layers of quoting it would be easiest to assign each layer to a variable. Then use bash's printf %q to automatically quote any string for use in a shell.
#! /usr/bin/env bash
json='{"_id": { "'"$oid"'": "some_random_object_id" } }'
cmd="mongodump --archive -u=admin --authenticationDatabase=admin -p=mongo-pwd --db=my-db --collection=my-collection --query=$(printf %q "$json") --gzip"
sshCmd="docker exec container-id /bin/sh -c $(printf %q "$cmd")"
docker-machine ssh my-machine-image "$sshCmd"

How to run a command in a container using kubectl exec that uses envrionment variables from the container?

I'm trying to write a script that runs some commands inside the container using kubectl exec. I'd like to use the environment variables that exist inside the container, but struggling to figure out how to prevent my local shell from evaluating the var and still have it evaluated in the container.
This was my first try, but $MONGODB_ROOT_PASSWORD get evaluated by my local shell instead of inside the container:
kubectl -n enterprise exec mycontainer -- mongodump --username root --password $MONGODB_ROOT_PASSWORD --out /dump
I tried this, but the had the same issue with pipe, it was evaluated in my local instead of in the container:
kubectl -n enterprise exec mycontainer -- echo 'mongodump --username root --password $MONGODB_ROOT_PASSWORD --out /dump' | sh
Is there a way to do this with kubectl exec?
You need a sh -c in there, like exec -- sh -c 'whatever $PASSWORD'.

Syntax missing ; before statement in mongoexport

I am trying to export mongodb collection with:
mongoexport -d InventoryPLC -c Spectra -o spectra.json;
but it is giving me error:
Syntax missing ; before statement
I search on mongodb website and other reference websites:
mongoexport -d InventoryPLC -c Spectra;
I want to export collection.
I checked stackoverflow examples and mongodb documentation:
mongoexport -d InventoryPLC -c Spectra -o spectra.json;
I want exported collection, but have had no luck.
Appreciate the help.
You are probably trying to run mongoexport from inside the Mongo shell. That command must be executed outside it, from your OS command shell.