'Kubernetes mongo DB user creation

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



Solution 1:[1]

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

Solution 2:[2]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Telinov Dmitri
Solution 2 Pierre Lasorak