MongoDB 用户管理方法

MongoDB 用户管理方法

MongoDB用户管理方法用于管理数据库的用户。

db.auth()

auth方法允许shell中的用户向数据库提供身份验证。它可以接受用户名和密码,即db.auth( <username>, passwordPrompt() )db.auth(<username>, <password>)

我们可以定义一个包含用户名称、密码、身份验证机制和摘要密码标志的用户集合。

db.auth( {
   user: <name>,
   pwd: "<cleartext password>",
   mechanism: <auth. mechanism>,
   digestPassword: <boolean>
} )

示例:

连接Mongo shell后,如果您想进行身份验证,必须在用户的认证数据库中使用db.auth()命令:

use test
db.auth( "javaTpoint", passwordPrompt() )

MongoDB 用户管理方法

db.changeUserPassword(username, password)

更新用户的密码。在定义用户的数据库中运行此方法,即您创建用户的数据库。

示例

以下操作将产品数据库中名为accountUser的用户的密码更改为SOh3TbYhx8ypJPxmt1oOfL:

use products
db.changeUserPassword("accountUser", passwordPrompt())

你还可以直接将新密码传递给db.changeUserPassword():

use products
db.changeUserPassword("accountUser", "SOh3TbYhx8ypJPxmt1oOfL")

输出:

MongoDB 用户管理方法

db.createUser(<user>, <writeConcern>)

该方法在当前运行的数据库上创建一个指定的新用户。如果用户已经存在于指定的数据库中,则该方法会返回一个重复错误。

使用createUser方法定义数据库用户的语法如下:

{
  user: "<name>",
  pwd: "<cleartext password>",
  customData: { <any info.> },
  roles: [
    { role: "<role>", db: "<database>" } | "<role>",
    ...
  ],
  authenticationRestrictions: [
     {
       clientSource: ["<IP>" | "<CIDR range>", ...],
       serverAddress: ["<IP>" | "<CIDR range>", ...]
     } ]
  mechanisms: [ "<SCRAM-SHA-1|SCRAM-SHA-256>", ... ],
  passwordDigestor: "<server|client>"
}

示例:

以下示例将在学生数据库上创建accountJTP用户。

use EmployeeAdmin
db.createUser( { user: "accountJTP",
           pwd: "<cleartext password>",
           customData: { Employee: 12345 },
            roles: [ { role: "clusterAdmin", db: "admin" },
                 { role: "readAnyDatabase", db: "admin" },
                   "readWrite"] },
           w: "majority" , wtimeout: 5000 } )

输出:

MongoDB 用户管理方法

db.dropUser(<user>, <writeConcern>)

db.dropUser()方法包装了dropUser命令,在删除具有userAdmin AnyDatabase角色的用户之前,从当前数据库中删除该用户。你必须明确指出你至少有一个具有用户管理权限的额外用户。

示例:

以下操作使用db.dropUser()在studnet数据库上删除jtpAdmin用户。

use testwriter
db.dropUser("testwriter", {w: "majority", wtimeout: 4000})

MongoDB 用户管理方法

db.removeUser(<username>)

这个方法已经没有更多的用途了。您可以使用此方法从当前数据库中删除指定的用户名。

db.updateUser(<username>, <update>, <writeConcern>)

updateUser方法用于更新指定数据库的用户配置文件。使用此方法将完全替换旧字段的值。该方法会将更新添加到用户的角色数组中。

语法:

db.updateUser(
   "<username>",
   {
     customData : { <any info.> },
     roles : [
       { role: "<role>", db: "<database>" } | "<role>",
       ...
     ],
     pwd: "<cleartext password>",
     authenticationRestrictions: [
        {
          clientSource: ["<IP>" | "<CIDR range>", ...],
          serverAddress: ["<IP>", | "<CIDR range>", ...]
        },
        ...
     ],
     mechanisms: [ "<SCRAM-SHA-1|SCRAM-SHA-256>", ... ],
     passwordDigestor: "<server|client>"
   },
   writeConcern: { <write concern> }
)

示例:

下面的示例将使用db.updateUser()方法完全替换用户的customData和roles数据:

use Employee
db.updateUser( "NewMartin",
{
   customData : { employeeId : "001" },
   roles : [
      { role : "read", db : "assets"  }
   ]
} )

输出:

MongoDB 用户管理方法

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程