MongoDB创建用户官方例子

The following db.createUser() operation creates the accountAdmin01 user on the products database.

products数据库创建用户accountAdmin01

 

use products  # 指定数据库

db.createUser( { user: "accountAdmin01",  # 用户名

                 pwd: "changeMe",  # 密码

                 customData: { employeeId: 12345 },  # 自定义的数据,可以不要,用户的数据都会存到system.users集合中,建议不要直接访问,而是使用管理命令

                 roles: [ { role: "clusterAdmin", db: "admin" },  # 角色roles:[{ {}, {}, }},指定用户对什么数据库具有什么权限

                          { role: "readAnyDatabase", db: "admin" },

                          "readWrite"] },  # 对当前数据库有什么权限

               { w: "majority" , wtimeout: 5000 } )

# 可以写成writeConcern: { w: "majority" , wtimeout: 5000 }意思应该是写操作不超过5秒,还有其它的设置,这个是为了保障或者平衡在写入数据库的时候的可靠性。

 

The operation gives accountAdmin01 the following roles:

这个操作给了用户accountAdmin01 以下角色:

 

the clusterAdmin and readAnyDatabase roles on the admin database  # admin数据库的clusterAdmin readAnyDatabase 角色

the readWrite role on the products database  # products 数据库的readWrite 角色

Create User with Roles  # 配合角色创建用户

 

The following operation creates accountUser in the products database and gives the user the readWrite and dbAdmin roles.

products 数据库创建用户,并给了readWrite dbAdmin 角色。

 

use products

db.createUser( {

     user: "accountUser",

     pwd: "password",

     roles: [ "readWrite", "dbAdmin" ]

   })

Create User Without Roles

没有配合角色创建用户

 

The following operation creates a user named reportsUser in the admin database but does not yet assign roles:

以下操作在admin创建一个账户reportsUser 但没有注册角色

 

use admin

db.createUser( {

     user: "reportsUser",

     pwd: "password",

     roles: [ ]

})

Create Administrative User with Roles

使用角色创建用于管理的用户

 

The following operation creates a user named appAdmin in the admin database and gives the user readWrite access to the config database, which lets the user change certain settings for sharded clusters, such as to the balancer setting.

以下操作在admin数据库创建一个用户appAdmin 并给了readWrite 权限给config数据库。这样可以让用户将分片修改成正确的设置,更平衡的设置。

 

use admin

db.createUser( {

     user: "appAdmin",

     pwd: "password",

     roles:[

         { role: "readWrite", db: "config" },

         "clusterAdmin"

       ]})

posted @ 2017-07-27 17:45  Tororo  阅读(169)  评论(0)    收藏  举报