MongoDB數(shù)據(jù)庫授權認證的實現(xiàn)
1.與用戶相關的命令
- db.auth() 將用戶驗證到數(shù)據(jù)庫。
- db.changeUserPassword() 更改現(xiàn)有用戶的密碼。
- db.createUser() 創(chuàng)建一個新用戶。
- db.dropUser() 刪除單個用戶。
- db.dropAllUsers() 刪除與數(shù)據(jù)庫關聯(lián)的所有用戶。
- db.getUser() 返回有關指定用戶的信息。
- db.getUsers() 返回有關與數(shù)據(jù)庫關聯(lián)的所有用戶的信息。
- db.grantRolesToUser() 授予用戶角色及其特權。
- db.removeUser() 已過時。從數(shù)據(jù)庫中刪除用戶。
- db.revokeRolesFromUser() 從用戶中刪除角色。
- db.updateUser() 更新用戶數(shù)據(jù)。
2.配置mongodb登陸授權認證
2.1.創(chuàng)建一個用戶
> use admin
> db.createUser(
{
user: "admin",
pwd: "123456",
roles: [ { role: "root", db: "admin" } ] //指定角色為root,表示管理員
}
> db.getUsers()

2.2.修改配置文件啟用用戶認證
[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.授權用戶并賦予多個權限
mongodb可以創(chuàng)建多個用戶并針對不同的庫進行不同的操作
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庫驗證權限
1.登錄mytest用戶并連接到tets庫
[mongo@mongodb-1 ~]$ mongo -umytest -p123456 192.168.81.210:27017/test
2.查看所有表
> show tables
hash
test
3.查看是否有讀權限
> 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.查看是否有寫入權限
> 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庫驗證權限
由于普通用戶只能登錄test庫因此想要切換其他庫,只能是登陸test庫后使用use進行切換
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"
})
可以看到只能讀取,不能插入

到此這篇關于MongoDB數(shù)據(jù)庫授權認證的實現(xiàn)的文章就介紹到這了,更多相關MongoDB數(shù)據(jù)庫授權認證內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
MongoDB數(shù)據(jù)庫安裝配置、基本操作實例詳解
這篇文章主要介紹了MongoDB數(shù)據(jù)庫安裝配置、基本操作,結合實例形式詳細分析了MongoDB數(shù)據(jù)庫安裝配置具體步驟、相關命令與基本操作實現(xiàn)技巧,需要的朋友可以參考下2020-01-01
Window環(huán)境下配置Mongodb數(shù)據(jù)庫
這篇文章介紹了Window環(huán)境下配置Mongodb數(shù)據(jù)庫的方法,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-07-07
Navicat遠程連接MongoDB最全實現(xiàn)方法以及報錯解決
這篇文章主要給大家介紹了關于Navicat遠程連接MongoDB最全實現(xiàn)方法以及報錯解決的相關資料,文中通過圖文將實現(xiàn)的步驟一步步介紹的非常詳細,對大家學習或者使用MongoDB具有一定的參考學習價值,需要的朋友可以參考下2023-03-03

