mongodb管理Security and Authentication mongdb自己也提供了驗證機制,如果不驗證是無法訪問數據庫對象,一個數據可以有多個用戶,用戶的權限也可以各不相同,下面具體介紹一下。 use admin ----登錄到超級管理員 db.addUser(root, abcd); { _id : ObjectId
mongodb管理Security and Authentication
mongdb自己也提供了驗證機制,如果不驗證是無法訪問數據庫對象,一個數據可以有多個用戶,用戶的權限也可以各不相同,下面具體介紹一下。
> use admin ----登錄到超級管理員
>db.addUser("root", "abcd");
{
"_id" : ObjectId("4eaf58af769d879418d93191"),
"user" : "root",
"readOnly" : false,
"pwd" : "1a0f1c3c3aa1d592f490a2addc559383"
}
創建了一個超級用戶,這個用戶可以訪問任何數據庫,權限最大
> use test
switched to db test 切換到test數據庫
> db.addUser("test_user", "efgh");
{
"user" : "test_user",
"readOnly" : false,
"pwd" : "6076b96fc3fe6002c810268702646eec"
}
> db.addUser("read_only", "ijkl", true);
{
"user" : "read_only",
"readOnly" : true,
"pwd" : "f497e180c9dc0655292fee5893c162f1"
}
以上創建兩個用戶,test_user和read_only,不同的是,read_only用戶只有只讀權限
> use test
switched to db test
> db.test.find();
error: { "$err" : "unauthorized for db [test] lock type: -1 " }
> db.auth("read_only", "ijkl");
1
> db.test.find();
{ "_id" : ObjectId("4bb007f53e8424663ea6848a"), "x" : 1 }
> db.test.insert({"x" : 2});
unauthorized
> db.auth("test_user", "efgh");
1
> db.test.insert({"x": 2});
> db.test.find();
{ "_id" : ObjectId("4bb007f53e8424663ea6848a"), "x" : 1 }
{ "_id" : ObjectId("4bb0088cbe17157d7b9cac07"), "x" : 2 }
以上例子可以看到沒有驗證用戶登錄,對數據庫讀寫操作均不能,read_only用戶只能查詢,不能插入,但是test_user用戶驗證登錄后就可以插入了。
如果這時,show dbs,還是報錯,因為test_user 不是admin用戶,所以沒有權限。
> use admin
switched to db admin
> db.auth("root", "abcd");
1
> show dbs
admin
local
test
驗證了超級用戶就可以做相關操作
創建的這些用戶都存在創建時數據庫下面一個叫system.users的集合里面,如果要移除一個user,例如移除test_user
> db.auth("test_user", "efgh");
1
> db.system.users.remove({"user" : "test_user"});
> db.auth("test_user", "efgh");
0
啟用驗證登錄:如果是配置文件啟動server端,里面可以加入auth = true ,如果是跟參數啟動mongod 后面記得跟--auth。
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com