需求:创建一个test数据库,给这个数据库添加一个用户,用户名为testuser,密码为123456,并授予testuser对数据库test其读写操作权限
1.创建数据库test
use test
> db.createUser({user:"testuser",pwd:"123456",roles:[{role:"readWrite",db:"test"}]})
Successfully added user: {
"user" : "testuser",
"roles" : [
{
"role" : "readWrite",
"db" : "test"
}
]
}
3.验证
[root@localhost bin]# ./mongo
MongoDB shell version v4.4.6
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("e1547625-ce02-4473-aa6b-849c41633a9d") }
MongoDB server version: 4.4.6
> use admin
switched to db admin
> use test
switched to db test
> db.auth("testuser","123456")
1
>
4.插入数据,插入一个names集合(表)
> db.auth("testuser","123456")
1
> db.names.insert({"name":"wenxin"})
WriteResult({ "nInserted" : 1 })
> db.names.find()
{ "_id" : ObjectId("60ab5dd114b367ed10535406"), "name" : "wenxin" }
>