Skip to content

Instantly share code, notes, and snippets.

@tamoyal
Created April 11, 2014 04:39
Show Gist options
  • Save tamoyal/10441108 to your computer and use it in GitHub Desktop.
Save tamoyal/10441108 to your computer and use it in GitHub Desktop.
Create super user and database user in Mongo 2.6
# Create your superuser
$ mongo
> use admin
> db.createUser({user:"someadmin",pwd:"secret", roles:[{role:"root",db:"admin"}]})
> exit
# Alias for convenience (optional and at your own risk)
$ echo 'alias mongo="mongo --port 27017 -u someadmin -p secret --authenticationDatabase admin"' >> ~/.bash_profile
$ source ~/.bash_profile
# Add user to your DB
$ mongo
> use some_db
> db.createUser(
{
user: "mongouser",
pwd: "someothersecret",
roles: ["readWrite"]
}
)
# If you get locked out, start over
sudo service mongod stop
sudo mv /data/admin.* . # for backup
sudo service mongod start
@Olliebrown
Copy link

Olliebrown commented Feb 15, 2017

If you put the '-p' option at the end of the command line without a password then the client will prompt you for it interactively. Seems a bit more prudent than leaving that password sitting in your bash profile in plaintext!

echo 'alias mongo="mongo --port 27017 -u someadmin --authenticationDatabase admin -p"' >> ~/.bash_profile

@russellhoff
Copy link

Thank you! I'm new to nosql databases and just started with mongodb. Useful :D

@a7madev
Copy link

a7madev commented Jul 20, 2017

Thank you!

@nahumt
Copy link

nahumt commented Aug 25, 2017

Excellent, thank you .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment