账户权限配置
1,创建超级管理用户:
> show dbs admin 0.000GB config 0.000GB local 0.000GB > use admin switched to db admin > db.createUser({ ... user:"admin", ... pwd:"123", ... roles:[{role:"root",db:"admin"}] ... }) Successfully added user: { "user" : "admin", "roles" : [ { "role" : "root", "db" : "admin" } ] } > show users { "_id" : "admin.admin", "userId" : UUID("eb5ce3d6-7112-441e-a73b-153814f2604b"), "user" : "admin", "db" : "admin", "roles" : [ { "role" : "root", "db" : "admin" } ], "mechanisms" : [ "SCRAM-SHA-1", "SCRAM-SHA-256" ] } >
2,修改 MongoDb数据库配置文件:
C:\Program Files\MongoDB\Server\4.2\bin\mongod.cfg
3,重启Mongodb 服务
4,用超级管理员账户连接数据库:
>mongo MongoDB shell version v4.2.8 connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("6f7fa306-0dbd-4cd0-9f6d-cb8dd3ec0226") } MongoDB server version: 4.2.8 > show dbs > use admin switched to db admin > db.auth("admin","123") 1 >
5,给 hello 数据库 创建一个用户,只能访问 hello 不能访问其他数据库:
> show dbs > db.auth("admin","123") 1 > show dbs admin 0.000GB config 0.000GB hello 0.000GB local 0.000GB > use hello switched to db hello > db.createUser({ ... user:"hello_admin", ... pwd:"h_123", ... roles:[{role:"dbOwner",db:"hello"}] ... }) Successfully added user: { "user" : "hello_admin", "roles" : [ { "role" : "dbOwner", "db" : "hello" } ] } > show users { "_id" : "hello.hello_admin", "userId" : UUID("037e9508-19c2-4940-bdd8-9517855338db"), "user" : "hello_admin", "db" : "hello", "roles" : [ { "role" : "dbOwner", "db" : "hello" } ], "mechanisms" : [ "SCRAM-SHA-1", "SCRAM-SHA-256" ] } >
MongoDB 账户权限配置中的常用命令:
① 查看当前库下的用户 show users
② 删除用户 db.dropUser("hello_admin")
③ 修改用户密码:db.updateUser("admin" , { pwd : "password" } )
④ 密码认证:db.auth("admin" , "password") 注意是逗号
MongDb 数据库角色:
①数据库用户角色:read,readWrite
②数据库管理角色:dbAdmin dbOwner userAdmin
③集群管理角色:clusterAdmin clusterManager clusterMonitor hostManager
④备份恢复角色:backup restore
⑤所有数据库角色:readAnyDatabase readWriteAnyDatabase userAdminAnyDatabase dbAdminAnyDatabase
⑥超级用户角色:root
https://www.cnblogs.com/zzw1787044/p/5773178.html