MongoDB數(shù)據(jù)庫(kù)授權(quán)認(rèn)證的實(shí)現(xiàn)
1.與用戶相關(guān)的命令
- db.auth() 將用戶驗(yàn)證到數(shù)據(jù)庫(kù)。
- db.changeUserPassword() 更改現(xiàn)有用戶的密碼。
- db.createUser() 創(chuàng)建一個(gè)新用戶。
- db.dropUser() 刪除單個(gè)用戶。
- db.dropAllUsers() 刪除與數(shù)據(jù)庫(kù)關(guān)聯(lián)的所有用戶。
- db.getUser() 返回有關(guān)指定用戶的信息。
- db.getUsers() 返回有關(guān)與數(shù)據(jù)庫(kù)關(guān)聯(lián)的所有用戶的信息。
- db.grantRolesToUser() 授予用戶角色及其特權(quán)。
- db.removeUser() 已過(guò)時(shí)。從數(shù)據(jù)庫(kù)中刪除用戶。
- db.revokeRolesFromUser() 從用戶中刪除角色。
- db.updateUser() 更新用戶數(shù)據(jù)。
2.配置mongodb登陸授權(quán)認(rèn)證
2.1.創(chuàng)建一個(gè)用戶
> 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
使用口令登陸后會(huì)發(fā)現(xiàn)最后一個(gè)警告信息也會(huì)消失

3.授權(quán)用戶并賦予多個(gè)權(quán)限
mongodb可以創(chuàng)建多個(gè)用戶并針對(duì)不同的庫(kù)進(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庫(kù)
> use test
> db.test.insert({"name":"xiaoming","age":10})
> db.test.insert({"name":"xiaohong","age":10})
> db.test.insert({"name":"xiaolan","age":10})
test2庫(kù)
> use test2
> db.test2.insert({"name":"jiangxl","job":"it","age":"99"})
> db.test2.insert({"name":"wanger","job":"it","age":"99"})

3.2.使用mytest登錄test庫(kù)驗(yàn)證權(quán)限
1.登錄mytest用戶并連接到tets庫(kù)
[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庫(kù)驗(yàn)證權(quán)限
由于普通用戶只能登錄test庫(kù)因此想要切換其他庫(kù),只能是登陸test庫(kù)后使用use進(jìn)行切換
1.登錄test庫(kù)
[mongo@mongodb-1 ~]$ mongo -umytest -p123456 192.168.81.210:27017/test
2.切換到tets2庫(kù)
> 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"
})
可以看到只能讀取,不能插入

到此這篇關(guān)于MongoDB數(shù)據(jù)庫(kù)授權(quán)認(rèn)證的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)MongoDB數(shù)據(jù)庫(kù)授權(quán)認(rèn)證內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信