12,mongodb(3.4版本)的用户管理

1

2,创建用户

use admin//需要切换到admin库

db.createUser( { user: "admin", customData: {description: "superuser"}, pwd: "admin122", roles: [ { role: "root", db: "admin" } ] } )

 

user指定用户,customData为说明字段,可以省略,pwd为密码,roles指定用户的角色,db指定库名

use admin //切换到admin库

db.system.users.find()  //列出所有用户,需要切换到admin库

show users  //查看当前库下所有的用户

db.dropUser('admin') //删除用户

若要用户生效,还需要编辑启动脚本vim /usr/lib/systemd/system/mongod.service,在OPTIONS=后面增--auth

重启服务systemctl restart mongod

mongo -u "admin" -p "admin122" --authenticationDatabase "admin"

3,用户管理

use db1

 db.createUser( { user: "test1", pwd: "123aaa", roles: [ { role: "readWrite", db: "db1" }, {role: "read", db: "db2" } ] } )

 test1用户对db1库读写,对db2库只读。

 之所以先use db1,表示用户在 db1 库中创建,就一定要db1库验证身份,即用户的信息跟随随数据库。比如上述 test1虽然有 db2 库的读取权限,但是一定要先在db1库进行身份验证,直接访问会提示验证失败。

 use db2

 db.auth("test1", "123aaa")

 

posted @ 2020-12-15 22:45  pwcc  阅读(163)  评论(0)    收藏  举报