首先是對MongoDB用戶和權限的設置,若是不設置用戶的話,直接使用mongo命令就能夠進入客戶端shell界面進行操做了,可是若是沒有設置用戶的話,總感受少了點什麼,因而通過半天的查找和實踐,差很少把用戶和權限弄明白了。總結以下:mongodb
若是按照如下這個指令安裝的話:shell
mongod --install --dbpath "C:\Program Files\mongodb\data\db" --logpath "C:\Program Files\mongodb\data\log\MongoDB.log"數據庫
以下:服務器
c:\Program Files\mongodb\bin>mongod --install --dbpath "C:\Program Files\mongodb\data\db" --logpath "C:\Program Files\mongodb\data\log\MongoDB.log"less
Fri Apr 05 13:47:43.164ui
Fri Apr 05 13:47:43.168 warning: 32-bit servers don't have journaling enabled by default. Please use --journal if you want durability.spa
Fri Apr 05 13:47:43.169code
Fri Apr 05 13:47:43.169 Trying to install Windows service 'MongoDB'server
Fri Apr 05 13:47:43.170 There is already a service named 'MongoDB', abortingip
c:\Program Files\mongodb\bin>net start MongoDB
Mongo DB 服服務務已已經經啓啓動動成成功功。。
c:\Program Files\mongodb\bin>mongo
MongoDB shell version: 2.4.1
connecting to: test
Server has startup warnings:
Fri Apr 05 13:48:02.516 [initandlisten]
Fri Apr 05 13:48:02.516 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.
Fri Apr 05 13:48:02.516 [initandlisten] ** 32 bit builds are limited to less than 2GB of data (or less with --journal).
Fri Apr 05 13:48:02.516 [initandlisten] ** Note that journaling defaults to off for 32 bit and is currently off.
Fri Apr 05 13:48:02.516 [initandlisten] ** See http://dochub.mongodb.org/core/32bit
Fri Apr 05 13:48:02.516 [initandlisten]
> show dbs
admin (empty)
local 0.03125GB
>
能夠看到有兩個默認的數據庫admin和local
實例1:建立一個用戶名爲root,密碼爲admin的用戶,以下:
> use admin
switched to db admin
> db.addUser("root","admin")
{
"user" : "root",
"readOnly" : false,
"pwd" : "bde0d84f6749c235a6b4e36d945eb666",
"_id" : ObjectId("515e662430d89f61f6991c91")
}
> show collections
Fri Apr 05 13:50:36.685 JavaScript execution failed: error: {
"$err" : "not authorized for query on admin.system.namespaces",
"code" : 16550
} at src/mongo/shell/query.js:L128
>
說明:使用以上指令show collections的時候,發現報錯了。是由於沒有權限。作以下操做:
> db.auth("root","admin");
1
說明:返回1表示驗證成功了,返回0表示驗證失敗。
此時,輸入如下指令:show collections則能夠看到admin下的集合了。
> show collections
system.indexes
system.users
> db.system.users.find()
{ "_id" : ObjectId("515e662430d89f61f6991c91"), "user" : "root", "readOnly" : false, "pwd" : "bde0d84f6749c235a6b4e36d945eb666" }
>
實例2:在用戶名爲root,密碼爲admin的用戶下建立一個student數據庫,並在student數據庫中建立一個stu的集合並插入一個文檔,以下:
> use student
switched to db student
> db.stu.insert({"name":"maoyuanjun","age":25,"sex":"male"})
> db.stu.find()
{ "_id" : ObjectId("515e676630d89f61f6991c92"), "name" : "maoyuanjun", "age" : 25, "sex" : "male" }
退出服務器,從新登錄以下:
c:\Program Files\mongodb\bin>mongo
MongoDB shell version: 2.4.1
connecting to: test
> show dbs
Fri Apr 05 13:58:05.420 JavaScript execution failed: listDatabases failed:{ "ok" : 0, "errmsg" : "unauthorized" }
at src/mongo/shell/mongo.js:L46
出錯緣由:是沒有權限。則先判斷是否有權限,以下:
> use admin
switched to db admin
> db.auth("root","admin")
1
> show dbs
admin 0.0625GB
local 0.03125GB
student 0.0625GB
>
這時,咱們能夠看到用戶root,密碼admin的用戶下多了一個student數據庫。
實例3:在建立一個用戶
c:\Program Files\mongodb\bin>mongo
MongoDB shell version: 2.4.1
connecting to: test
> use admin
switched to db admin
直接添加用戶會報錯:
> db.addUser("test","123456")
Fri Apr 05 14:01:31.404 JavaScript execution failed: error: { "$err" : "not authorized for query on admin.system.users", "code" : 16550 } at src/mongo/shell/query.js:L128
作權限的判斷:
> db.auth("root","admin");
1
> use admin
switched to db admin
> db.addUser("test",123456)
{
"user" : "test",
"readOnly" : false,
"pwd" : "c8ef9e7ab00406e84cfa807ec082f59e",
"_id" : ObjectId("515e68e2be252e81c5dee198")
}
> show collections
system.indexes
system.users
> db.system.users.find()
{ "_id" : ObjectId("515e662430d89f61f6991c91"), "user" : "root", "readOnly" : fa
lse, "pwd" : "bde0d84f6749c235a6b4e36d945eb666" }
{ "_id" : ObjectId("515e68e2be252e81c5dee198"), "user" : "test", "readOnly" : fa
lse, "pwd" : "c8ef9e7ab00406e84cfa807ec082f59e" }
>