您好,登錄后才能下訂單哦!
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超級(jí)管理員權(quán)限:
關(guān)閉mongod.cnf配置文件參數(shù):auth
直接登錄mongo,設(shè)置登錄密碼:
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")
關(guān)閉mongod
開(kāi)啟mongod.cnf配置文件參數(shù):auth =true
mongo --host 127.0.0.1 --port 6068
use admin
db.auth("root","TdLLQH689")
創(chuàng)建測(cè)試庫(kù)dbtest001,以及測(cè)試用戶(hù)testuser 對(duì)dbtest001庫(kù)有讀寫(xiě)的權(quán)限
use dbtest001
db.createUser({user:"testuser",pwd:"testuser123",roles:[{role:"readWrite",db:"dbtest001"}]})
創(chuàng)建測(cè)試用戶(hù)testuser 對(duì)dbtest001庫(kù)有讀的權(quán)限:
db.createUser({user:"testuser01",pwd:"testuser123",roles:[{role:"read",db:"dbtest001"}]})
給測(cè)試庫(kù)插入一個(gè)集合chenji(所謂的表chenji):
db.chenji.insert({"name":"小花","年級(jí)":"二年級(jí)","性別":"男","愛(ài)好":"學(xué)習(xí)"})
WriteResult({ "nInserted" : 1 })
> db.chenji.insert({"name":"花花","年級(jí)":"一年級(jí)","性別":"女","愛(ài)好":"唱歌"})
WriteResult({ "nInserted" : 1 })
> db.chenji.insert({"name":"小花","年級(jí)":"三年級(jí)","性別":"女","愛(ài)好":"打球"})
WriteResult({ "nInserted" : 1 })
> db.chenji.insert({"name":"小花","年級(jí)":"四年級(jí)","性別":"女","愛(ài)好":"畫(huà)畫(huà)"})
WriteResult({ "nInserted" : 1 })
查看當(dāng)前庫(kù)下的用戶(hù):
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"
}
]
}
查看當(dāng)前庫(kù)下某個(gè)用戶(hù)的權(quán)限:
db.getUser("testuser")
db.getUser("testuser01")
創(chuàng)建角色: db.createRole()
更新角色: db.updateRole()
刪除角色: db.dropRole()
獲得某個(gè)角色信息: db.getRole()
> db.getRole("read")
{
"role" : "read",
"db" : "dbtest001",
"isBuiltin" : true,
"roles" : [ ],
"inheritedRoles" : [ ]
}
> db.getRole("readWrite")
{
"role" : "readWrite",
"db" : "dbtest001",
"isBuiltin" : true,
"roles" : [ ],
"inheritedRoles" : [ ]
}
刪除所有用戶(hù): db.dropAllUsers()
刪除用戶(hù):db.dropUsers("xxxxxx")
db.dropUser("testuser01")
true
將一個(gè)角色賦予讀寫(xiě)給用戶(hù): db.grantRolesToUser()
use dbtest001
db.grantRolesToUser('testuser01',[ { role : 'readWrite', db : 'dbtest001' } ] )
撤銷(xiāo)某個(gè)用戶(hù)的某個(gè)角色權(quán)限: 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讀寫(xiě)權(quán)限:
> 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")
測(cè)試:使用原密碼登錄報(bào)錯(cuò)如下:
[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" : "小花", "年級(jí)" : "二年級(jí)", "性別" : "男", "愛(ài)好" : "學(xué)習(xí)" }
{ "_id" : ObjectId("5e0c80c3efc383349153386b"), "name" : "花花", "年級(jí)" : "一年級(jí)", "性別" : "女", "愛(ài)好" : "唱歌" }
{ "_id" : ObjectId("5e0c83c8efc383349153386c"), "name" : "小花", "年級(jí)" : "三年級(jí)", "性別" : "女", "愛(ài)好" : "打球" }
{ "_id" : ObjectId("5e0c83e9efc383349153386d"), "name" : "小花", "年級(jí)" : "四年級(jí)", "性別" : "女", "愛(ài)好" : "畫(huà)畫(huà)" }
查看當(dāng)前庫(kù)下的表:
show collections
chenji
查看集合中所有的內(nèi)容:
> db.chenji.find();
{ "_id" : ObjectId("5e0c80a8efc383349153386a"), "name" : "小花", "年級(jí)" : "二年級(jí)", "性別" : "男", "愛(ài)好" : "學(xué)習(xí)" }
{ "_id" : ObjectId("5e0c80c3efc383349153386b"), "name" : "花花", "年級(jí)" : "一年級(jí)", "性別" : "女", "愛(ài)好" : "唱歌" }
查看名字叫小花的記錄:
> db.chenji.find({"name":"小花"})
{ "_id" : ObjectId("5e0c80a8efc383349153386a"), "name" : "小花", "年級(jí)" : "二年級(jí)", "性別" : "男", "愛(ài)好" : "學(xué)習(xí)" }
{ "_id" : ObjectId("5e0c83c8efc383349153386c"), "name" : "小花", "年級(jí)" : "三年級(jí)", "性別" : "女", "愛(ài)好" : "打球" }
{ "_id" : ObjectId("5e0c83e9efc383349153386d"), "name" : "小花", "年級(jí)" : "四年級(jí)", "性別" : "女", "愛(ài)好" : "畫(huà)畫(huà)" }
參考資料:
MongoDB用戶(hù)和角色解釋系列(上)
http://www.mongoing.com/archives/26710
先小結(jié)到此處,后續(xù)在繼續(xù)學(xué)習(xí)補(bǔ)充
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。