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.credentialsUser's authentication information. For users with externally stored authentication credentials, such as users that use Kerberos or X.509 certificates for authentication, the
system.usersdocument for that user does not contain thecredentialsfield. For SCRAM user credentials, the information includes the mechanism, iteration count, and authentication parameters.
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 角色。