admin 数据库的 system.users 集合存储用户身份验证和授权信息。为了管理该集合中的数据,MongoDB 提供了用户管理命令。
system.users 架构
system.users 集合中的文档具有以下模式:
{ _id: <system defined id>, userId : <system assigned UUID>, user: "<name>", db: "<database>", credentials: { <authentication credentials> }, roles: [ { role: "<role name>", db: "<database>" }, ... ], customData: <custom information>, authenticationRestrictions : [ <documents> ] }
每个 system.users 文档包含以下字段:
admin.system.users.userThe user name. A user exists in the context of a single logical database (see
admin.system.users.db) but can have access on other databases through roles specified in therolesarray.
admin.system.users.dbThe authentication database associated with the user. The user's privileges are not necessarily limited to this database. The user can have privileges in additional databases through the
rolesarray.
admin.system.users.credentials用户的身份验证信息。对于具有外部存储的身份验证凭证的用户,例如使用Kerberos或 X.509 证书进行身份验证的用户,该用户的
system.users文档不包含credentials字段。对于SCRAM用户凭证,信息包括机制、迭代计数和身份验证参数。
admin.system.users.roles角色文档的语法如下:
{ role: "<role name>", db: "<database>" } 角色文档包含以下字段:
admin.system.users.roles[n].role角色的名称。角色可以是 MongoDB 提供的内置角色或用户定义的自定义角色。
使用角色管理或用户管理命令指定角色时,如果运行该命令的数据库上存在该角色,则可单独指定角色名称(例如
"readWrite")。
例子
以 system.users 集合中的以下文档为例:
{ "_id" : "home.Kari", "userId" : UUID("ec1eced7-055a-4ca8-8737-60dd02c52793"), "user" : "Kari", "db" : "home", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "S/xM2yXFosynbCu4GzFDgQ==", "storedKey" : "Ist4cgpEd1vTbnRnQLdobgmOsBA=", "serverKey" : "e/0DyzS6GPboAA2YNBkGYm87+cg=" }, "SCRAM-SHA-256" : { "iterationCount" : 15000, "salt" : "p1G+fZadAeYAbECN8F/6TMzXGYWBaZ3DtWM0ig==", "storedKey" : "LEgLOqZQmkGhd0owm/+6V7VdJUYJcXBhPUvi9z+GBfk=", "serverKey" : "JKfnkVv9iXwxyc8JaapKVwLPy6SfnmB8gMb1Pr15T+s=" } }, "authenticationRestrictions" : [ { "clientSource" : [ "69.89.31.226" ], "serverAddress" : [ "172.16.254.1" ] } ], "customData" : { "zipCode" : "64157" }, "roles" : [ { "role" : "read", "db" : "home" }, { "role" : "readWrite", "db" : "test" } ] }
文档显示,用户 Kari 的身份验证数据库是 home 数据库。Kari 在 home 数据库中具有 read 角色,在 test 数据库中具有 readWrite 角色。