Inside of mongosh shell I can do this fine…
use admin
db.createUser({user: “admin”, pwd: “admin”, roles: [{role: “root”, db: “admin”}]})
Now I need to do it in a script with a one liner but it doesn’t seem to work. It doesn’t
give any errors. However, the user will NOT be created?
Here is what I’ve tried…
echo ‘use admin ; db.createUser({user: “admin”, pwd: “admin”, roles: [{role: “root”, db: “admin”}]}) ; exit;’ | mongosh
Also:
mongosh --eval ‘use admin ; db.createUser({user: “admin”, pwd: “admin”, roles: [{role: “root”, db: “admin”}]}) ; exit;’
Is there a better way to create user in a script with a one liner?
Thanks,
chris
(Chris Dellaway)
2
Hi @Christian_Seberino
Try this form:
--eval "db.getSiblingDB('admin').createUser(...)"
2 Likes
Thanks. I figured it out. Apparently my problem was trying to switch databases inside my mongosh code.
In other words, this doesn’t work:
mongosh --eval ‘use admin ; db.createUser({user: “admin”, pwd: “admin”, roles: [{role: “root”, db: “admin”}]}) ;’
However, if you remove the ‘use admin;’ and switch to admin another way, then it works…
mongosh admin --eval ‘db.createUser({user: “admin”, pwd: “admin”, roles: [{role: “root”, db: “admin”}]}) ;’
system
(system)
Closed
4
This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.