您好,登錄后才能下訂單哦!
mongodb配置文件如下:
[root@localhost ~]# cat /usr/local/mongodb/mongod.cnf
bind_ip = 0.0.0.0
logpath=/data/mongodb/logs/mongodb.log
logappend = true
fork = true
#auth = true
port = 6068
dbpath=/data/mongodb/data
pidfilepath=/data/mongodb/mongod.pid
root超級管理員權限:
關閉mongod.cnf配置文件參數:auth
直接登錄mongo,設置登錄密碼:
mongo --host 127.0.0.1 --port 6068
use admin
db.createUser({user: 'root', pwd:'TdLLQd689', roles:[{role: 'root', db: 'admin'}]});
db.auth("root","TdLLQSdH689")
關閉mongod
開啟mongod.cnf配置文件參數:auth =true
mongo --host 127.0.0.1 --port 6068
use admin
db.auth("root","TdLLQH689")
創建測試庫dbtest001,以及測試用戶testuser 對dbtest001庫有讀寫的權限
use dbtest001
db.createUser({user:"testuser",pwd:"testuser123",roles:[{role:"readWrite",db:"dbtest001"}]})
創建測試用戶testuser 對dbtest001庫有讀的權限:
db.createUser({user:"testuser01",pwd:"testuser123",roles:[{role:"read",db:"dbtest001"}]})
給測試庫插入一個集合chenji(所謂的表chenji):
db.chenji.insert({"name":"小花","年級":"二年級","性別":"男","愛好":"學習"})
WriteResult({ "nInserted" : 1 })
> db.chenji.insert({"name":"花花","年級":"一年級","性別":"女","愛好":"唱歌"})
WriteResult({ "nInserted" : 1 })
> db.chenji.insert({"name":"小花","年級":"三年級","性別":"女","愛好":"打球"})
WriteResult({ "nInserted" : 1 })
> db.chenji.insert({"name":"小花","年級":"四年級","性別":"女","愛好":"畫畫"})
WriteResult({ "nInserted" : 1 })
查看當前庫下的用戶:
db.getUsers() 或者show users
> show users
{
"_id" : "dbtest001.testuser",
"userId" : UUID("f275fad5-ac4f-4dfa-aea6-af34ed93c3e1"),
"user" : "testuser",
"db" : "dbtest001",
"roles" : [
{
"role" : "readWrite",
"db" : "dbtest001"
}
]
}
查看當前庫下某個用戶的權限:
db.getUser("testuser")
db.getUser("testuser01")
創建角色: db.createRole()
更新角色: db.updateRole()
刪除角色: db.dropRole()
獲得某個角色信息: db.getRole()
> db.getRole("read")
{
"role" : "read",
"db" : "dbtest001",
"isBuiltin" : true,
"roles" : [ ],
"inheritedRoles" : [ ]
}
> db.getRole("readWrite")
{
"role" : "readWrite",
"db" : "dbtest001",
"isBuiltin" : true,
"roles" : [ ],
"inheritedRoles" : [ ]
}
刪除所有用戶: db.dropAllUsers()
刪除用戶:db.dropUsers("xxxxxx")
db.dropUser("testuser01")
true
將一個角色賦予讀寫給用戶: db.grantRolesToUser()
use dbtest001
db.grantRolesToUser('testuser01',[ { role : 'readWrite', db : 'dbtest001' } ] )
撤銷某個用戶的某個角色權限: db.revokeRolesFromUser()
> db.getUser("testuser01")
{
"_id" : "dbtest001.testuser01",
"userId" : UUID("d1fefe19-a94f-4300-8855-fd722f14e13f"),
"user" : "testuser01",
"db" : "dbtest001",
"roles" : [
{
"role" : "readWrite",
"db" : "dbtest001"
}
]
}
撤回讀role讀寫權限:
> db.revokeRolesFromUser('testuser01',[ { role : 'readWrite', db : 'dbtest001' }])
> db.getUser("testuser01")
{
"_id" : "dbtest001.testuser01",
"userId" : UUID("d1fefe19-a94f-4300-8855-fd722f14e13f"),
"user" : "testuser01",
"db" : "dbtest001",
"roles" : [ ]
}
更改密碼: db.changeUserPassword()
db.createUser({user:"testuser01",pwd:"testuser123",roles:[{role:"read",db:"dbtest001"}]})
修改密碼:
> db.changeUserPassword("testuser01","123456")
測試:使用原密碼登錄報錯如下:
[root@localhost ~]# mongo 127.0.0.1:6068/dbtest001 -u testuser01 -p 'testuser123'
MongoDB shell version v3.6.16
connecting to: mongodb://127.0.0.1:6068/dbtest001?gssapiServiceName=mongodb
2020-01-01T21:51:35.637+0800 E QUERY [thread1] Error: Authentication failed. :
connect@src/mongo/shell/mongo.js:263:13
@(connect):1:6
exception: connect failed
使用新的密碼登錄成功
[root@localhost ~]# mongo 127.0.0.1:6068/dbtest001 -u testuser01 -p '123456'
MongoDB shell version v3.6.16
connecting to: mongodb://127.0.0.1:6068/dbtest001?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("966023f0-ff7b-4726-a216-8475bc729971") }
MongoDB server version: 3.6.16
> show collections;
chenji
> db.chenji.find();
{ "_id" : ObjectId("5e0c80a8efc383349153386a"), "name" : "小花", "年級" : "二年級", "性別" : "男", "愛好" : "學習" }
{ "_id" : ObjectId("5e0c80c3efc383349153386b"), "name" : "花花", "年級" : "一年級", "性別" : "女", "愛好" : "唱歌" }
{ "_id" : ObjectId("5e0c83c8efc383349153386c"), "name" : "小花", "年級" : "三年級", "性別" : "女", "愛好" : "打球" }
{ "_id" : ObjectId("5e0c83e9efc383349153386d"), "name" : "小花", "年級" : "四年級", "性別" : "女", "愛好" : "畫畫" }
查看當前庫下的表:
show collections
chenji
查看集合中所有的內容:
> db.chenji.find();
{ "_id" : ObjectId("5e0c80a8efc383349153386a"), "name" : "小花", "年級" : "二年級", "性別" : "男", "愛好" : "學習" }
{ "_id" : ObjectId("5e0c80c3efc383349153386b"), "name" : "花花", "年級" : "一年級", "性別" : "女", "愛好" : "唱歌" }
查看名字叫小花的記錄:
> db.chenji.find({"name":"小花"})
{ "_id" : ObjectId("5e0c80a8efc383349153386a"), "name" : "小花", "年級" : "二年級", "性別" : "男", "愛好" : "學習" }
{ "_id" : ObjectId("5e0c83c8efc383349153386c"), "name" : "小花", "年級" : "三年級", "性別" : "女", "愛好" : "打球" }
{ "_id" : ObjectId("5e0c83e9efc383349153386d"), "name" : "小花", "年級" : "四年級", "性別" : "女", "愛好" : "畫畫" }
參考資料:
MongoDB用戶和角色解釋系列(上)
http://www.mongoing.com/archives/26710
先小結到此處,后續在繼續學習補充
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。