How to execute mongodb commands inside the bash script

8/18/2020

I'm trying to run mongo commands through my bash script file.

I have sample.sh -- run with ./sample.sh

I want to run below command inside my sample.sh file. It is bash script. I want to mongo commands run inside the file, not inside the shell.

kubectl exec $mongoPodName -c mongo-mongodb -- mongo db.createUser({user:"admin",pwd:"123456",roles:[{role:"root",db:"admin"}]})

I get the error below :

./generate-service-dbs.sh: line 37: syntax error near unexpected token `('
./generate-service-dbs.sh: line 37: `kubectl exec $mongoPodName -c mongo-mongodb -- mongo db.createUser({user:"admin",pwd:"123456",roles:[{role:"root",db:"admin"}]})'
-- Hakimeh Mordadi
bash
containers
kubernetes
linux
mongodb

1 Answer

8/18/2020

Don't you have to run command with --eval?

kubectl exec $mongoPodName -c mongo-mongodb -- mongo --eval 'db.createUser({user:"admin",pwd:"123456",roles:[{role:"root",db:"admin"}]})'
-- Faheem
Source: StackOverflow