Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a user in MongoDB using command line

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.


1 Answers

  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'}]})
like image 191
Viral Patel Avatar answered Sep 20 '25 23:09

Viral Patel