Mongodb Study Notes 05-user management
Enable authentication
Mongod does not have the permission to start by default. You need to specify-auth to start, or set security. authorization to "enabled" in the configuration file"
Create user
Db. createUser (user, writeConcern)
Document http://docs.mongodb.org/manual/reference/method/db.createUser/#db.createUser
User format
{ user: "
", pwd: "
", customData: { }, roles: [ { role: "
", db: "
" } | "
", ... ]}
writeConcern:
For example, {w: "majority", j: true, wtimeout: 5000}
W option: values allowed are 1, 0, greater than 1, "majority", and j option: ensure that the mongod instance writes data to the journal (log) on the disk ), this ensures that data is not lost when you disable mongd. Set true to enable. Wtimeout: specifies a time limit, in milliseconds. Wtimeout is applicable only when the value of w is greater than 1.
Built-In Roles (built-in role ):
Database User Roles: read and readWrite; database management roles: dbAdmin, dbOwner, and userAdmin; cluster management roles: clusterAdmin, clusterManager, clusterMonitor, and hostManager; backup and recovery roles: backup and restore; all Database roles: readAnyDatabase, readWriteAnyDatabase, userAdminAnyDatabase, and dbAdminAnyDatabase superuser role: root
// Here, there are several roles that indirectly or directly provide access to system superusers (dbOwner, userAdmin, userAdminAnyDatabase) internal roles :__ system
CreatUser example
Create a super Administrator
use admindb.createUser({ user:"username", pwd:"password", roles:["root"]})
Create an accountAdmin01 user in the products database and have the readWrite permission. You have the permissions of clusterAdmin and readAnyDatabase on the admin database.
use productsdb.createUser( { "user" : "accountAdmin01", "pwd": "cleartext password", "customData" : { employeeId: 12345 }, "roles" : [ { role: "clusterAdmin", db: "admin" }, { role: "readAnyDatabase", db: "admin" }, "readWrite" ] }, { w: "majority" , wtimeout: 5000 } )
Login
use collectionNamedb.auth("username",'password")
View users
show users
Delete a user
db.dropUser("username")
Change User Password
db.changeUserPassword("username","password")
Update user
db.createUser(user, writeConcern)