簡體   English   中英

MongoDB:服務器有啟動警告“未為數據庫啟用訪問控制”

[英]MongoDB: Server has startup warnings ''Access control is not enabled for the database''

我今天首先安裝了 MongoDB 3.4.1。 但是當我啟動它並使用 MongoDB shell 時,它給了我以下這些警告:

C:\Users\hs>"C:\Program Files\MongoDB\Server\3.4\bin\mongo.exe
MongoDB shell version v3.4.1
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.1
Server has startup warnings:
2017-01-12T21:19:46.941+0800 I CONTROL  [initandlisten]
2017-01-12T21:19:46.942+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-01-12T21:19:46.942+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2017-01-12T21:19:46.942+0800 I CONTROL  [initandlisten]

我的電腦是 Microsoft Windows [ version 10.0.14393 ]。

MongoDB v3.4

您需要執行以下操作來創建安全數據庫:

確保啟動該進程的用戶具有權限並且目錄存在(在本例中為/data/db )。

1)在沒有訪問控制的情況下啟動MongoDB。

mongod --port 27017 --dbpath /data/db

2) 連接到實例。

mongo --port 27017

3)創建用戶管理員(在admin認證數據庫中)

use admin
db.createUser(
  {
    user: "myUserAdmin",
    pwd: "abc123",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)

4) 用訪問控制重啟MongoDB實例。

mongod --auth --port 27017 --dbpath /data/db

5) 以用戶管理員身份進行連接和驗證。

mongo --port 27017 -u "myUserAdmin" -p "abc123" --authenticationDatabase "admin"

6) 根據您的部署的需要(例如在測試認證數據庫中)創建額外的用戶。

use test
db.createUser(
  {
    user: "myTester",
    pwd: "xyz123",
    roles: [ { role: "readWrite", db: "test" },
             { role: "read", db: "reporting" } ]
  }
)

7) 以 myTester 身份連接並進行身份驗證。

mongo --port 27017 -u "myTester" -p "xyz123" --authenticationDatabase "test"

我基本上只是在這里解釋了官方文檔的簡短版本:https ://docs.mongodb.com/master/tutorial/enable-authentication/

天哪,多么好的天然氣廠,最佳答案!

您需要做的就是:

  1. 編輯您的配置,例如C:\Program Files\MongoDB\Server\4.4\bin\mongodb.cfg
  2. security: authorization:設置為enabled ,如圖; 請注意,此子條目可能會完全丟失。 然后添加它。
  3. 從 Windows服務控制面板重新啟動MongoDB Server服務。

在此處輸入圖像描述

顯然,如果在此之后設置基於讀/讀/寫角色的策略,那將更有意義。

參考: https://docs.mongodb.com/manual/tutorial/configure-scram-client-authentication/

我剛剛使用 phpunit 對此進行了測試,按預期工作。

您可以創建管理員用戶或其他角色 在 mongo shell 上運行它。

db.createUser({user: "username", pwd: "password", roles: ["dbAdmin"]})

如果您收到 SCRAM-SHA-256 錯誤,您可以設置 SHA-1 機制。

db.createUser({user: "username", pwd: "password", roles: ["dbAdmin"], mechanisms: ["SCRAM-SHA-1"]})

您需要刪除舊的 db 文件夾並重新創建新文件夾。 它將解決您的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM