之前沒用過MongoDB,設定好后,我的mongoDB被黑了1秒,請幫我回答我的問題:“如何保護我的mongoDB?”

uj5u.com熱心網友回復:
要保護 MongoDB,您需要:
- 啟用安全性(在 mongod.conf 檔案中),
- 創建用于身份驗證的資料庫用戶,
- 您可以將埠 27001(默認)更改為任何埠,例如 27000(在 mongod.conf 檔案中)
- 您可以添加特定的 ip 地址以允許連接和訪問您的資料庫(在 mongod.conf 檔案中)。
你需要找出mongod.conf并打開它。(用谷歌搜索 mongod.conf 在你的電腦 windows/mac/ubuntu 中的存盤位置)
security:
authorization: enabled
關閉埠 27001 上的 MongoDB 實體
mongo admin --port 27001 --eval 'db.shutdownServer()'
使用新配置重啟 MongoDB 實體
mongod -f mongod.conf
使用以下命令在admin資料庫上創建第一個用戶
mongo
>use admin
db.createUser({
user: "USER_NAME_HERE",
pwd: "PASSWORD_HERE",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
});
例子 :
db.createUser({
user: "AdminUser",
pwd: "57d49$4eqwe#adb4d",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
});
之后運行以下以檢查用戶是否已通過身份驗證
語法: db.auth("USER_NAME_HERE", "PASSWORD_HERE")
db.auth( "AdminUser", "57d49$4eqwe#adb4d" )
檢查用戶:
db.getUsers()
它將回傳:
[
{
"_id" : "admin.AdminUser",
"userId" : UUID("31ccb892-d3ef-46b6-8ac1-2e9b5be11892"),
"user" : "globalAdminUser",
"db" : "admin",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}
]
現在您的資料庫是安全的,只有經過身份驗證的用戶才能訪問它。
您可以通過以下方式連接 mongo:
mongo admin --port 27001 --username 'AdminUser' --password '57d49$4eqwe#adb4d'
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/374682.html
上一篇:MongoDB中的SQL等效查詢
下一篇:如何使用mongodb聚合添加值
