Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
@IAmKio

This comment has been minimized.

Copy link

commented Apr 30, 2014

Lifesaver. Thank you!

@inorganik

This comment has been minimized.

Copy link

commented May 20, 2014

Thanks.

To start over, I simply removed the admin db, i.e. sudo rm /data/admin.*

Also, if you don't want to f**k with the 'mongo' command, you could name the alias something like 'mongoAdmin'

@dlio

This comment has been minimized.

Copy link

commented Jun 2, 2014

Thanks a ton for this one.

@gy0624ww

This comment has been minimized.

Copy link

commented Nov 18, 2014

good

@quipu

This comment has been minimized.

Copy link

commented Jan 8, 2015

Thank you - this is GOLD.

@tvrcgo

This comment has been minimized.

Copy link

commented Feb 2, 2015

how to connect dbs other than admin using super user ?

@damiancipolat

This comment has been minimized.

Copy link

commented Jul 31, 2015

thanks you!!!!!! :D

@christophetd

This comment has been minimized.

Copy link

commented Dec 11, 2015

Thanks!

@narambage

This comment has been minimized.

Copy link

commented Apr 7, 2016

Thanks a lot... again saying it

@JasmineBharadiya

This comment has been minimized.

Copy link

commented May 19, 2016

thanks it's very useful

@antoniomgatto

This comment has been minimized.

Copy link

commented Jan 9, 2017

Thanks !!!

@ardinusawan

This comment has been minimized.

Copy link

commented Jan 22, 2017

Really useful!
Thx mate :)

@Olliebrown

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

commented May 10, 2017

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

@a7madev

This comment has been minimized.

Copy link

commented Jul 20, 2017

Thank you!

@nahumt

This comment has been minimized.

Copy link

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
You can’t perform that action at this time.