日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

MongoDB數(shù)據(jù)庫授權(quán)認(rèn)證的實現(xiàn)_MongoDB

作者:Jiangxl~ ? 更新時間: 2022-03-19 編程語言

1.與用戶相關(guān)的命令

  • db.auth() 將用戶驗證到數(shù)據(jù)庫。
  • db.changeUserPassword() 更改現(xiàn)有用戶的密碼。
  • db.createUser() 創(chuàng)建一個新用戶。
  • db.dropUser() 刪除單個用戶。
  • db.dropAllUsers() 刪除與數(shù)據(jù)庫關(guān)聯(lián)的所有用戶。
  • db.getUser() 返回有關(guān)指定用戶的信息。
  • db.getUsers() 返回有關(guān)與數(shù)據(jù)庫關(guān)聯(lián)的所有用戶的信息。
  • db.grantRolesToUser() 授予用戶角色及其特權(quán)。
  • db.removeUser() 已過時。從數(shù)據(jù)庫中刪除用戶。
  • db.revokeRolesFromUser() 從用戶中刪除角色。
  • db.updateUser() 更新用戶數(shù)據(jù)。

2.配置mongodb登陸授權(quán)認(rèn)證

2.1.創(chuàng)建一個用戶

> use admin
> db.createUser(
{
   user: "admin",
   pwd: "123456", 
   roles: [ { role: "root", db: "admin" } ]			//指定角色為root,表示管理員
}
> db.getUsers()

在這里插入圖片描述

2.2.修改配置文件啟用用戶認(rèn)證

[mongo@mongodb-1 ~]$ vim /data/mongodb_cluster/mongodb_27017/conf/mongodb.yml 
security:
  authorization: enabled

2.3.重啟mongodb

[mongo@mongodb-1 ~]$ mongod -f /data/mongodb_cluster/mongodb_27017/conf/mongodb.yml --shutdown
killing process with pid: 17899
[mongo@mongodb-1 ~]$ mongod -f /data/mongodb_cluster/mongodb_27017/conf/mongodb.yml 
about to fork child process, waiting until server is ready for connections.
forked process: 18511
child process started successfully, parent exiting

2.4.使用口令登陸mongodb

[mongo@mongodb-1 ~]$ mongo -uadmin -p123456
MongoDB shell version v4.0.14
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("ae65176e-ac6b-4906-b621-496996381417") }
MongoDB server version: 4.0.14
> show dbs

使用口令登陸后會發(fā)現(xiàn)最后一個警告信息也會消失

在這里插入圖片描述

3.授權(quán)用戶并賦予多個權(quán)限

mongodb可以創(chuàng)建多個用戶并針對不同的庫進(jìn)行不同的操作

3.1.創(chuàng)建用戶并制造數(shù)據(jù)

1.創(chuàng)建用戶
[mongo@mongodb-1 ~]$ mongo -uadmin -p123456
> db.createUser(
...   {
...     user: "mytest",
...     pwd: "123456", 
...     roles: [ { role: "readWrite", db: "test" },		//可讀可寫
...              { role: "read", db: "test2" }   ]					//可讀
...   }
... )


2.插入數(shù)據(jù)
test庫
> use test
> db.test.insert({"name":"xiaoming","age":10})
> db.test.insert({"name":"xiaohong","age":10})
> db.test.insert({"name":"xiaolan","age":10})

test2庫
> use test2
> db.test2.insert({"name":"jiangxl","job":"it","age":"99"})
> db.test2.insert({"name":"wanger","job":"it","age":"99"})

在這里插入圖片描述

3.2.使用mytest登錄test庫驗證權(quán)限

1.登錄mytest用戶并連接到tets庫
[mongo@mongodb-1 ~]$ mongo -umytest -p123456 192.168.81.210:27017/test

2.查看所有表
> show tables
hash
test

3.查看是否有讀權(quán)限
> db.test.find()
{ "_id" : ObjectId("602c73b5d9d09b9b700c9eb2"), "name" : "xiaoming", "age" : 10 }
{ "_id" : ObjectId("602c73bdd9d09b9b700c9eb3"), "name" : "xiaohong", "age" : 10 }
{ "_id" : ObjectId("602c73c1d9d09b9b700c9eb4"), "name" : "xiaolan", "age" : 10 }

4.查看是否有寫入權(quán)限
> db.test.insert({"name":"xiaozhang","age":10})

5.查看是否寫入成功
> db.test.find()
{ "_id" : ObjectId("602c73b5d9d09b9b700c9eb2"), "name" : "xiaoming", "age" : 10 }
{ "_id" : ObjectId("602c73bdd9d09b9b700c9eb3"), "name" : "xiaohong", "age" : 10 }
{ "_id" : ObjectId("602c73c1d9d09b9b700c9eb4"), "name" : "xiaolan", "age" : 10 }
{ "_id" : ObjectId("602c74f949b9d3f400ed866b"), "name" : "xiaozhang", "age" : 10 }

可讀可寫

在這里插入圖片描述

3.3.使用mytest登錄test2庫驗證權(quán)限

由于普通用戶只能登錄test庫因此想要切換其他庫,只能是登陸test庫后使用use進(jìn)行切換

1.登錄test庫
[mongo@mongodb-1 ~]$ mongo -umytest -p123456 192.168.81.210:27017/test

2.切換到tets2庫
> use test2

3.查看表
> show tables
test2

4.查看表中數(shù)據(jù)
> db.test2.find()

5.插入一條數(shù)據(jù),查看是否插入成功
> db.test2.insert({"name":"xiaozi","job":"it","age":"99"})
WriteCommandError({
	"ok" : 0,
	"errmsg" : "not authorized on test2 to execute command { insert: \"test2\", ordered: true, lsid: { id: UUID(\"6203f7df-d8f8-4880-aab3-4db712ae785f\") }, $db: \"test2\" }",
	"code" : 13,
	"codeName" : "Unauthorized"
})

可以看到只能讀取,不能插入

在這里插入圖片描述

原文鏈接:https://blog.csdn.net/weixin_44953658/article/details/122185708

欄目分類
最近更新