Mongodb Study Notes 05 and mongodb Study Notes
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: "<name>", pwd: "<cleartext password>", customData: { <any information> }, roles: [ { role: "<role>", db: "<database>" } | "<role>", ... ]}
WriteConcern:
For example, {w: "majority", j: true, wtimeout: 5000}
- W option: values allowed are 1, 0, greater than 1, "majority", and ",;
- J option: Make sure that the mongod instance writes data to the journal (log) on the disk. This ensures that data is not lost when it is disabled outside 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
- Super User role: root
// There are also several roles that indirectly or directly provide system superuser access (dbOwner, userAdmin, userAdminAnyDatabase)
- Internal role :__ 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)
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.