前言
Mongodb 數(shù)據(jù)庫(kù)默認(rèn)情況下是沒有訪問控制的,整個(gè)數(shù)據(jù)庫(kù)對(duì)外是開發(fā)的,只要能連上數(shù)據(jù)庫(kù),則可以進(jìn)行任何操作,這會(huì)對(duì)數(shù)據(jù)帶來很大的風(fēng)險(xiǎn)。當(dāng)然,我們可以啟用mongodb的訪問控制,只讓通過認(rèn)證的用戶才能對(duì)數(shù)據(jù)庫(kù)進(jìn)行角色范圍內(nèi)的操作。
啟用訪問控制可以通過在啟動(dòng) mongodb 時(shí)指定 --auth
參數(shù)來設(shè)置,另外還涉及到創(chuàng)建用戶 db.createUser
操作以及一些角色的定義,我們先來看這部分內(nèi)容。
db.createUser() 用法
1
2
3
4
5
6
7
|
db.createUser({ user : "$USERNAME" , pwd: "$PASSWROD" , roles: [ { role: "$ROLE_NAME" , db: "$DBNAME" } ] }) |
參數(shù)說明:
- user 是用戶名
- pwd 是密碼
- role 來指定用戶的角色
- db 來指定所屬的數(shù)據(jù)庫(kù)
- roles 是用戶所有角色的集合
Mongodb 預(yù)定義角色
Mongodb 中預(yù)定義了一些角色,把這些角色賦予給適當(dāng)?shù)挠脩羯希脩艟椭荒苓M(jìn)行角色范圍內(nèi)的操作。
1、數(shù)據(jù)庫(kù)用戶角色 (所有數(shù)據(jù)庫(kù)都有)
- read 用戶可以讀取當(dāng)前數(shù)據(jù)庫(kù)的數(shù)據(jù)
- readWrite 用戶可以讀寫當(dāng)前數(shù)據(jù)庫(kù)的數(shù)據(jù)
2、數(shù)據(jù)庫(kù)管理角色(所有數(shù)據(jù)庫(kù)都有)
- dbAdmin 管理員用戶但不能對(duì)用戶和角色管理授權(quán)
- dbOwner 數(shù)據(jù)庫(kù)所有者可進(jìn)行任何管理任務(wù)
- userAdmin 可以管理當(dāng)前數(shù)據(jù)的用戶和角色
3、集群管理角色(admin數(shù)據(jù)庫(kù)可用)
- clusterAdmin 集群所有管理權(quán)限,是 clusterManager , clusterMonitor, hostManager 合集
- clusterManager 集群管理和監(jiān)控
- clusterMonitor 集群監(jiān)控,只讀的
- hostManager 監(jiān)控和管理服務(wù)器
4、備份和恢復(fù)角色(admin數(shù)據(jù)庫(kù)可用)
- backup
- restore
5、所有數(shù)據(jù)庫(kù)角色(admin數(shù)據(jù)庫(kù)可用)
- readAnyDatabase 讀取所有數(shù)據(jù)庫(kù)
- readWriteAnyDatabase 讀寫所有數(shù)據(jù)庫(kù)
- userAdminAnyDatabase 所有數(shù)據(jù)庫(kù)的 userAdmin 權(quán)限
- dbAdminAnyDatabase 所有數(shù)據(jù)庫(kù)的 dbAdmin 權(quán)限
6、超級(jí)角色(admin數(shù)據(jù)庫(kù)可用)
- root 超級(jí)用戶
7、內(nèi)部角色
- __system 所有操作權(quán)限
更多預(yù)定于角色的信息請(qǐng)參看:https://docs.mongodb.com/manual/core/security-built-in-roles/
啟用訪問控制的步驟
1, 啟動(dòng) mongodb 實(shí)例,關(guān)閉 訪問控制
不帶 --auth
1
|
. /mongod |
2, 連接上 mongodb 實(shí)例
1
|
./mongo |
3,創(chuàng)建用戶管理員
在 admin 數(shù)據(jù)庫(kù)中添加一個(gè) 具有 userAdminAnyDatabase 角色的用戶作為用戶管理用戶。下面的例子中創(chuàng)建了 admin 為用戶管理員。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
> use admin switched to db admin > db.createUser({ ... user : "admin" , ... pwd: "admin" , ... roles: [ ... { role: "userAdminAnyDatabase" , db: "admin" } ... ] ... }) Successfully added user : { "user" : "admin" , "roles" : [ { "role" : "userAdminAnyDatabase" , "db" : "admin" } ] } > |
退出連接
4,重啟數(shù)據(jù)庫(kù)啟用訪問控制
命令行啟動(dòng),只需要添加 --auth 參數(shù)
1
|
. /mongo --auth |
5,使用管理用戶連接,有兩種方法
-
使用命令行
./mongo -u "$USERNAME" -p "$PASSWROD" --authenticationDatabase "admin"
-
使用
db.auth()
我們使用第二種
1
2
3
4
5
6
|
> > use admin switched to db admin > db.auth( "admin" , "admin" ) 1 > |
1 表示認(rèn)證成功
6, 為某個(gè)數(shù)據(jù)庫(kù)創(chuàng)建獨(dú)立用戶
以下為 test 數(shù)據(jù)庫(kù) 創(chuàng)建具有讀寫權(quán)限的用戶 test
admin 用戶由于只有 userAdminAnyDatabase 權(quán)限,所以沒有 test 數(shù)據(jù)的讀寫權(quán)限,所以,為了讀寫 test 數(shù)據(jù)庫(kù),我們需要?jiǎng)?chuàng)建一個(gè)用戶。先看一下直接用 admin 會(huì)報(bào)什么錯(cuò)誤
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
> use test > show collections 2017-01-13T13:49:17.691+0800 E QUERY [thread1] Error: listCollections failed: { "ok" : 0, "errmsg" : "not authorized on test to execute command { listCollections: 1.0, filter: {} }" , "code" : 13 } : _getErrorWithCode@src/mongo/shell/utils.js:25:13 DB.prototype._getCollectionInfosCommand@src/mongo/shell/db.js:773:1 DB.prototype.getCollectionInfos@src/mongo/shell/db.js:785:19 DB.prototype.getCollectionNames@src/mongo/shell/db.js:796:16 shellHelper.show@src/mongo/shell/utils.js:754:9 shellHelper@src/mongo/shell/utils.js:651:15 @(shellhelp2):1:1 |
我們直接使用 show collections , 則報(bào)錯(cuò):not authorized on test to execute command ,意思是沒有權(quán)限。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
> use test switched to db test > db.createUser({ ... user : "test" , ... pwd: "test" , ... roles: [ ... { role: "readWrite" , db: "test" } ... ] ... }) Successfully added user : { "user" : "test" , "roles" : [ { "role" : "readWrite" , "db" : "test" } ] } > |
然后我們使用 db.auth(“test”, “test”)
, 再執(zhí)行命令 則沒有報(bào)錯(cuò)
1
2
3
4
|
> db.auth( "test" , "test" ) 1 > > show collections |
試著寫入一條數(shù)據(jù),也是正常的。
1
2
3
4
5
|
> db.t. insert ({ name : "buzheng" }); WriteResult({ "nInserted" : 1 }) > db.t.find(); { "_id" : ObjectId( "58786c84bf5dd606ddfe1144" ), "name" : "buzheng" } > |
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。
原文鏈接:https://buzheng.org/2017/20170114-mongodb-enable-access-control.html