The method to enable mongodb authorization authentication:
1. Start mongod with --auth
2. Add auth = true to the configuration file mongod.conf
The first time --auth is enabled it appears:
2015-05-13T11:20:22.296+0800 I ACCESS [conn1] note: no users configured in admin.system.users, allowing localhost access
2015-05-13T11:20:22.297+0800 I ACCESS [conn1] Unauthorized not authorized on admin to execute command { getLog: “startupWarnings” }
2015-05-13T12:07:08.680+0800 I INDEX [conn1] build index on: admin.system.users properties: { v: 1, unique: true, key: { user: 1, db: 1 }, name: “user_1_db_1″, ns: “admin.system.users” }
i.e. no user has been defined before, so mongod will allow local direct access
Create a suitable superuser after mongo login
use admin db.createUser({ user: "mongo", pwd: "mongo", roles: [ { role: "__system", db: "admin" } ] }) http://docs.mongodb.org/manual/reference/method/db.createUser/
To authorize a user:
use admin db.grantRolesToUser ( "mongo", [ { role: "readAnyDatabase", db:"admin" } ] ) http://docs.mongodb.org/manual/tutorial/assign-role-to-user/
Authorization required to enable replica set:
use admin db.createUser( { user: "siteUserAdmin", pwd: "", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] }); db.createUser( { user: "siteRootAdmin", pwd: "", roles: [ { role: "root", db: "admin" } ] }); http://docs.mongodb.org/manual/tutorial/deploy-replica-set-with-auth/