In this example, we will start the MongoDB server in authentication mode, and then add an admin user to the database. We will use this admin user account to create users for specific databases.
1. Launch the
monogd
process in authentication mode by passing in the--auth
flag:$ ./mongodb/bin/mongod --auth Sat Aug 27 23:11:56 [initandlisten] MongoDB starting : pid=603 port=27017 dbpath=/data/db/ 32-bit
2. Launch the mongo shell and switch to
admin
database:$ ./mongodb/bin/mongo MongoDB shell version: 1.8.1 connecting to: test > use admin switched to db admin >
3. Add a user named
dbadmin
to theadmin
database and set an arbitrary password:> db.addUser(‘dbadmin', ‘mysecretpass') { "user" : "dbadmin", "readOnly" : false, "pwd" : "ca75881da377b1f792f82ce374cb2c0f" }
4. Authenticate yourself as the just created
admin
user using the following command:> db.auth(‘dbadmin', ‘mysecretpass') 1
6. Switch to a different database named...