MongoDB使用者許可權管理

2021-09-20 22:30:32 字數 1518 閱讀 7648

授權使用者readwrite有test1庫的讀寫許可權:

> use test1

switched to db test1

> db.createuser(,

roles:

})

驗證授權是否正確:

mongo -ureadwrite -preadwrite --authenticationdatabase test1

mongodb shell version: 3.2.16

connecting to: test

注意:使用者授權的時候一定要遵守規範,不然可能會出現連不上的情況,來舉個例子

mongo -uroot -proot --authenticationdatabase admin

mongodb shell version: 3.2

.16connecting to: test

> db.createuser(,

... roles:

... })

我們上面建立了readwrite1使用者,這個使用者與前面的readwrite使用者不同之處在於它在是test庫下面授權的,

mongo -ureadwrite1 -preadwrite1 --authenticationdatabase test1

mongodb shell version: 3.2.16

connecting to: test

2018-02-08t23:17:20.762+0800 e query [thread1] error: authentication failed. :

db.prototype._authorthrow@src/mongo/shell/db.js:1441:20

@(auth):6:1

@(auth):1:2

exception: login failed

mongo -ureadwrite1 -preadwrite1 --authenticationdatabase test

mongodb shell version: 3.2.16

connecting to: test

>

可以看到如果--authenticationdatabase沒有指定成授權的庫就會連不上mongo服務,為了避免出現類似這種情況,有兩種解決方法:

1,建立使用者的時候在test庫下面建立,因為預設連線的就是test庫

2,先切換到要授權的庫下面再來建立使用者

建議採用第二種方法,無論如何最好就是固定採用一種方式,這樣可以節省與開發的溝通成本

mongodb角色表

MongoDB 使用者許可權管理

安裝好mongdb 配置使用者訪問許可權後,在命令模式下發現show dbs 報錯,懵逼了不是配置許可權了麼。下面是我的解決回放 進入mongo 命令模式發現撥錯 r izwz947cvofre97sstzcmuz mongo mongodb shell version v3.4.2 connect...

MONGODB使用者 角色和許可權管理

最近遇到很多問mongodb使用者認證授權方面的問題,現在特記錄下來,與大家共享。一 概念理解 1 使用者概念 mongodb的使用者是由 使用者名稱 所屬庫名組成 例如 登入mongo testdb1 建立使用者testuser 登入mongo testdb2 建立使用者testuser 那上面建...

MongoDB 使用者驗證與許可權管理

說明 首先,建立mongodb使用者驗證的過程是 以不開啟使用者驗證的方式開啟mongo 進入mongo建立管理員 如果需要再建立其他管理員 重啟mongo並使用賬號訪問資料庫。在建立任何使用者之前需要建立乙個管理員來管控這些賬號。例如建立 修改使用者的使用許可權,都需要通過管理員進行 管理員不用於...