如何在现有数据库中创建具有正确角色的 MongoDB 用户?
要创建 MongoDB 中的新用户,请使用 createUser()。以下是查询 −
> db.createUser( ... { ... user: "John", ... pwd: "123456", ... roles: [ { role: "readWrite", db: "test" } ], ... mechanisms: [ "SCRAM-SHA-256" ] ... } ... )
这会生成以下输出 −
Successfully added user: { "user" : "John", "roles" : [ { "role" : "readWrite", "db" : "test" } ], "mechanisms" : [ "SCRAM-SHA-256" ] }
以下是显示所有用户的查询 −
> db.getUsers();
这会生成以下输出 −
[ { "_id" : "test.John", "user" : "John", "db" : "test", "roles" : [ { "role" : "readWrite", "db" : "test" } ], "mechanisms" : [ "SCRAM-SHA-256" ] } ]
广告