How to setup admin username and password for MongoDb

(written by lawrence krubner, however indented passages are often quotes). You can contact lawrence at: lawrence@krubner.com, or follow me on Twitter.

This is surprisingly confusing. It is strange to start MongoDb and be able to access it without needing a username or password.

I got it now. Here is what I document:

1) In mongo command line: (let say, set administrator)
> use admin;
> db.addUser(‘admin’,’123456′);
2) Shutdown Server and exit
> db.shutdownServer();
> exit
3) Restart Mongod with –auth
$ sudo ./mongodb/bin/mongod –auth –dbpath /mnt/db/
4) Run mongo again in 2 ways:
i) run mongo first then login.
$ ./mongodb/bin/mongo localhost:27107
> use admin
> db.auth(‘admin’,’123456′);
ii) run & login to mongo in command line.
$ ./mongodb/bin/mongo localhost:27107/admin -u admin-p 123456

* The username & password will work the same for mongodump and mongoexport.

Post external references

  1. 1
    http://stackoverflow.com/questions/4881208/how-to-put-username-password-in-mongodb
Source