[root@mongodb2 ~]# mongod -h |grep aut
--keyFile arg private key for cluster authentication
--noauth run without security
authentication. Alternatives are
--auth run with security
--autoresync automatically resync if slave data is
>use admin ?db.createUser( { user: "root", pwd: "root", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] } )Successfully added user: {
"user" : "root",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
]
}
mongos>
[root@mongodb3 ~]# mongo --port 37017 -u root -p root --authenticationDatabase admin
changeCustomData
changePassword
createRole
createUser
dropRole
dropUser
grantRole
revokeRole
viewRole
viewUser
db.createUser(
{
user: "suq",
pwd: "suq",
roles: [
{ role: "dbOwner", db: "admin" },
{ role: "userAdmin", db: "admin" },
{ role: "userAdminAnyDatabase", db: "admin" }
]
}
)
use admin
db.createUser(
{
user: "admin",
pwd: "admin",
roles: [ { role: "root",db:"admin" }]
}
)
[root@mongodb3 ~]# mongo --port 37017 -u admin -p admin --authenticationDatabase admin
MongoDB shell version: 3.2.6
connecting to: 127.0.0.1:37017/test
Server has startup warnings:
2016-06-24T18:41:47.116+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2016-06-24T18:41:47.116+0800 I CONTROL [initandlisten]
> db.auth("admin","admin")
{
role: "<name>",
privileges: [
{ resource: { <resource> }, actions: [ "<action>", ... ] },
...
],
roles: [
{ role: "<role>", db: "<database>" } | "<role>",
...
]
}
use admin
db.createRole(
{
role: "myClusterwideAdmin",
privileges: [
{ resource: { cluster: true }, actions: [ "addShard" ] },
{ resource: { db: "config", collection: "" }, actions: [ "find", "update", "insert", "remove" ] },
{ resource: { db: "users", collection: "usersCollection" }, actions: [ "update", "insert", "remove" ] },
{ resource: { db: "", collection: "" }, actions: [ "find" ] }
],
roles: [
{ role: "read", db: "admin" }
]
},
{ w: "majority" , wtimeout: 5000 }
)
[root@mongodb3 ~]# mongo --port 37017 -u root -p root --authenticationDatabase admin
use admin
db.createRole(
{
role: "manageOpRole",
privileges:
[
{
resource: { cluster: true },
actions: [ "killop", "inprog" ]
},
{
resource: { db: "", collection: "" },
actions: [ "killCursors" ]
}
],
roles: []
}
)
use admin
db.createRole(
{
role: "mongostatRole",
privileges: [
{
resource: { cluster: true },
actions: [ "serverStatus" ]
}
],
roles: []
}
)
> db.getRole("mongostatRole")
{
"role" : "mongostatRole",
"db" : "admin",
"isBuiltin" : false,
"roles" : [ ],
"inheritedRoles" : [ ]
}
> db.getRole("mongostatRole",{showPrivileges: true})
{
"role" : "mongostatRole",
"db" : "admin",
"isBuiltin" : false,
"roles" : [ ],
"inheritedRoles" : [ ],
"privileges" : [
{
"resource" : {
"cluster" : true
},
"actions" : [
"serverStatus"
]
}
],
"inheritedPrivileges" : [
{
"resource" : {
"cluster" : true
},
"actions" : [
"serverStatus"
]
}
]
}
> db.getRoles()
[
{
"role" : "manageOpRole",
"db" : "admin",
"isBuiltin" : false,
"roles" : [ ],
"inheritedRoles" : [ ]
},
{
"role" : "mongostatRole",
"db" : "admin",
"isBuiltin" : false,
"roles" : [ ],
"inheritedRoles" : [ ]
}
]
> db.getRoles({showPrivileges: true})
[
{
"role" : "manageOpRole",
"db" : "admin",
"isBuiltin" : false,
"roles" : [ ],
"inheritedRoles" : [ ],
"privileges" : [
{
"resource" : {
"cluster" : true
},
"actions" : [
"inprog",
"killop"
]
},
{
"resource" : {
"db" : "",
"collection" : ""
},
"actions" : [
"killCursors"
]
}
],
"inheritedPrivileges" : [
{
"resource" : {
"cluster" : true
},
"actions" : [
"inprog",
"killop"
]
},
{
"resource" : {
"db" : "",
"collection" : ""
},
"actions" : [
"killCursors"
]
}
]
},
{
"role" : "mongostatRole",
"db" : "admin",
"isBuiltin" : false,
"roles" : [ ],
"inheritedRoles" : [ ],
"privileges" : [
{
"resource" : {
"cluster" : true
},
"actions" : [
"serverStatus"
]
}
],
"inheritedPrivileges" : [
{
"resource" : {
"cluster" : true
},
"actions" : [
"serverStatus"
]
}
]
}
]
> db.getUser("suq")
{
"_id" : "admin.suq",
"user" : "suq",
"db" : "admin",
"roles" : [
{
"role" : "dbOwner",
"db" : "admin"
},
{
"role" : "userAdmin",
"db" : "admin"
},
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
]
}
还可以使用db.getUsers()获取全部的用户信息:
> db.getUsers()
[
{
"_id" : "admin.root",
"user" : "root",
"db" : "admin",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
]
},
{
"_id" : "admin.admin",
"user" : "admin",
"db" : "admin",
"roles" : [
{
"role" : "root",
"db" : "admin"
}
]
},
{
"_id" : "admin.suq",
"user" : "suq",
"db" : "admin",
"roles" : [
{
"role" : "dbOwner",
"db" : "admin"
},
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
]
}
]
db.revokePrivilegesFromRole(
"manageOpRole",
[
{ resource: {"cluster" : true}, actions: ["inprog","killop"] }
]
)
db.grantPrivilegesToRole(
"manageOpRole",
[
{
resource: {"cluster" : true},
actions: ["inprog","killop"]
}
]
)
db.revokeRolesFromUser( "suq", [ { role: "userAdmin", db: "admin" } ] )
db.grantRolesToUser(
"reportsUser",
[
{ role: "userAdmin", db: "admin" }
]
)
> db.changeUserPassword("suq", "111111")
Name | Description |
---|---|
db.createRole() | Creates a role and specifies its privileges. |
db.updateRole() | Updates a user-defined role. |
db.dropRole() | Deletes a user-defined role. |
db.dropAllRoles() | Deletes all user-defined roles associated with a database. |
db.grantPrivilegesToRole() | Assigns privileges to a user-defined role. |
db.revokePrivilegesFromRole() | Removes the specified privileges from a user-defined role. |
db.grantRolesToRole() | Specifies roles from which a user-defined role inherits privileges. |
db.revokeRolesFromRole() | Removes inherited roles from a role. |
db.getRole() | Returns information for the specified role. |
db.getRoles() | Returns information for all the user-defined roles in a database. |
Name | Description |
---|---|
db.auth() | Authenticates a user to a database. |
db.createUser() | Creates a new user. |
db.updateUser() | Updates user data. |
db.changeUserPassword() | Changes an existing user’s password. |
db.removeUser() | Deprecated. Removes a user from a database. |
db.dropAllUsers() | Deletes all users associated with a database. |
db.dropUser() | Removes a single user. |
db.grantRolesToUser() | Grants a role and its privileges to a user. |
db.revokeRolesFromUser() | Removes a role from a user. |
db.getUser() | Returns information about the specified user. |
db.getUsers() | Returns information about all users associated with a database. |
{
_id: <system-defined id>,
role: "<role name>",
db: "<database>",
privileges:
[
{
resource: { <resource> },
actions: [ "<action>", ... ]
},
...
],
roles:
[
{ role: "<role name>", db: "<database>" },
...
]
}
{
_id: <system defined id>,
user: "<name>",
db: "<database>",
credentials: { <authentication credentials> },
roles: [
{ role: "<role name>", db: "<database>" },
...
],
customData: <custom information>
}