官方详细网址:
https://docs.mongodb.com/manual/reference/method/js-user-management/
1、mongodb 角色权限的划分
1、https://docs.mongodb.com/manual/reference/method/db.createUser/index.html
2、mongodb 用户的创建
官方网址:https://docs.mongodb.com/manual/reference/method/db.createUser/index.html#create-administrative-user-with-roles
方式1:给产品表创建accountUser用户,并给与readWrite,dbAdmin角色
推荐使用方式。use productsdb.createUser( { user: "accountUser", pwd: "password", roles: [ "readWrite", "dbAdmin" ] })不推荐使用方式use productsdb.createUser( { user: "accountUser", pwd: "password", roles: [{ role:"readWrite", db:"products"}, {role:"dbAdmin" , db:"products"}] } )
use productsdb.createUser( { user: "accountAdmin01", pwd: "changeMe", customData: { employeeId: 12345 }, roles: [ { role: "clusterAdmin", db: "admin" }, { role: "readAnyDatabase", db: "admin" }, "readWrite"] }, { w: "majority" , wtimeout: 5000 } )
方式2:创建没有角色的用户
use admindb.createUser( { user: "reportsUser", pwd: "password", roles: [ ] })
3、用户的登陆
连接客户端后,用户登陆。
用户登陆之前一定要切换到对应的数据库中,然后再登陆,否则无法登陆
db.createUser({user:"dbAdmin", pwd:"123456",roles:[{ role:"dbAdmin", db:"solar"}]})db.createUser({user:"rw", pwd:"123456",roles:[{ role:"readWrite", db:"solar"}]})use solardb.auth("rw","123456")