在创建 MongoDB 阿里云数据库时,系统已为你创建了 root 账号,拥有 MongoDB 的 root 权限,你可以使用此账号登录数据库后,创建其他账号。
阿里云数据库MongoDB开通地址 https://www.aliyun.com/product/mongodb
阿里云数据库MongoDB创建账号官方教程 https://help.aliyun.com/document_detail/99142.html
创建账号命令
db.createUser(user, writeConcern)
user文档格式如下
{ user: "name", pwd: "cleartext password", customData: { any information }, roles: [ { role: "role", db: "database" } | "role", ... ] }示例在products库创建accountAdmin01账号。 use products db.createUser( { user: "accountAdmin01", pwd: "changeMe", customData: { employeeId: 12345 }, roles: [ { role: "clusterAdmin", db: "admin" }, { role: "readAnyDatabase", db: "admin" }, "readWrite"] }, { w: "majority" , wtimeout: 5000 } )
创建带角色的用户
以下操作在products库创建账号accountUser,并给账号赋予readWrite、dbAdmin角色。
use products db.createUser( { user: "accountUser", pwd: "password", roles: [ "readWrite", "dbAdmin" ] } )3. 创建不带角色的用户以下操作在admin库创建账号reportsUser,但未赋予角色。 use admin db.createUser( { user: "reportsUser", pwd: "password", roles: [ ] } )
创建带角色的管理员账号
以下操作在admin库中创建appAdmin账号,并给账号赋予config库的readWrite角色。
use admin db.createUser( { user: "appAdmin", pwd: "password", roles: [ { role: "readWrite", db: "config" }, "clusterAdmin" ] } )常用角色数据库用户角色:read,readWrite数据库管理员角色:dbAdmin,dbOwner,userAdmin集群管理员角色:clusterAdmin,clusterManager,clusterMonitor,hostManager备份恢复角色:backup,restore所有库角色:readAnyDatabase,readWriteAnyDatabase,userAdminAnyDatabase,dbAdminAnyDatabase超级管理员角色:root?
关于MongoDB创建用户的详情说明请参见“ MongoDB官方文档”。