4

I have a pipeline that executes commands and I want to use it to add a user to MongoDB. I have seen similar answers on stackoverflow, but those are for an older MongoDB version (they use .addUser() instead of .createUser()).

So far I have tried this:

'mongo {AUTH_URL} admin --eval "db.createUser({ createUser: {DB_USERNAME}, pwd: {DB_USER_PASSWORD}, roles: [{ role: "readWrite", db: {DB_NAME}}]})'

So what i need is a single line of code that will create the user for me and i will set all of the necessary elements through environment variables. If someone could provide an example of a terminal command to create a new user that would be great.

2
  • Possible duplicate of Create a MongoDB user from commandline Commented Aug 26, 2016 at 14:09
  • 2
    You've got a line of code which looks like it can add a user - what goes wrong when you run it? Commented Aug 26, 2016 at 16:13

3 Answers 3

13
  1. install mongodb3.0
  2. mongo
  3. use admin
  4. db.createUser({user:'admin',pwd:'admin',roles: [{role:'root',db:'admin'}]})
  5. use newdatabase
  6. db.createUser({user:'database_user',pwd:'newdatabasepass',roles:[{role:'readWrite',db:'newdatabase'}]})
Sign up to request clarification or add additional context in comments.

Comments

1

As per the document given you need to pass values like this.

db.createUser({user: "<username>",pwd: "<password>", roles: [{ role: "readWrite", db: "<dbName>" }, "clusterAdmin"]})

As this is as per the latest documentation.

3 Comments

so what i do is mongo db.createUser({user: "<username>",pwd: "<password>", roles: [{ role: "readWrite", db: "<dbName>" }, "clusterAdmin"]})
Login to mongo console and then type use dbname; then run the above command
Thanks, i will give it a try
0

Open _MONGOSHenter image description here

db.createUser({user:'user',pwd:'P@ss909',roles:[{role:'readWrite',db:'UseDB'}]})

Click Enter, You will get Return response as OK enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.