MongoDB權(quán)限管理之用戶名和密碼的操作
本文我們介紹MongoDB權(quán)限管理,主要介紹的是如何設(shè)置用戶名和密碼。接下來我們就一一介紹。
添加用戶的時(shí)候必須滿足以下兩個(gè)條件:
1.有相關(guān)權(quán)限的情況下(后面會說)。
2.mongod沒有加--auth的情況下(如果加了,你添加權(quán)限的話 會出現(xiàn)下面的情況)。
- > use admin
- switched to db admin
- > db.addUser('sa','sa')
- Fri Jul 22 14:31:13 uncaught exception: error {
- "$err" : "unauthorized db:admin lock type:-1 client:127.0.0.1",
- "code" : 10057
- }
- >
所以我們添加用戶時(shí)必須先在沒有加--auth的時(shí)候添加個(gè)super admin。
服務(wù)起來后,進(jìn)入./mongo。
- [root@:/usr/local/mongodb/bin]#./mongo
- MongoDB shell version: 1.8.2
- connecting to: test
- > use admin
- switched to db admin
- > db.adduser('sa','sa')
- Fri Jul 22 14:34:24 TypeError: db.adduser is not a function (shell):1
- > db.addUser('sa','sa')
- {
- "_id" : ObjectId("4e2914a585178da4e03a16c3"),
- "user" : "sa",
- "readOnly" : false,
- "pwd" : "75692b1d11c072c6c79332e248c4f699"
- }
- >
這樣就說明 已經(jīng)成功建立了,然后我們試一下權(quán)限。
- > show collections
- system.indexes
- system.users
在沒有加--auth的情況下 可以正常訪問admin喜愛默認(rèn)的兩個(gè)表。
- > db.system.users.find()
- { "_id" : ObjectId("4e2914a585178da4e03a16c3"), "user" : "sa", "readOnly" : false, "pwd" : "75692b1d11c072c6c79332e248c4f699" }>
已經(jīng)成功建立。
下面把服務(wù)加上--auth的選項(xiàng),再進(jìn)入./mongo。
- MongoDB shell version: 1.8.2
- connecting to: test
- > use admin
- switched to db admin
- > show collections
- Fri Jul 22 14:38:49 uncaught exception: error: {
- "$err" : "unauthorized db:admin lock type:-1 client:127.0.0.1",
- "code" : 10057
- }
- >
可以看出已經(jīng)沒有訪問權(quán)限了。
我們就用自己的密鑰登錄下:
- > db.auth('sa','sa')
- 1
返回1說明驗(yàn)證成功!
再show collections下就成功了。
.....
我們登錄其它表試試:
- [root@:/usr/local/mongodb/bin]#./mongo
- MongoDB shell version: 1.8.2
- connecting to: test
- > use test
- switched to db test
- > show collections
- Fri Jul 22 14:40:47 uncaught exception: error: {
- "$err" : "unauthorized db:test lock type:-1 client:127.0.0.1",
- "code" : 10057
- }
也需要驗(yàn)證,試試super admin登錄:
- [root@:/usr/local/mongodb/bin]#./mongo
- MongoDB shell version: 1.8.2
- connecting to: test
- > use test
- switched to db test
- > show collections
- Fri Jul 22 14:40:47 uncaught exception: error: {
- "$err" : "unauthorized db:test lock type:-1 client:127.0.0.1",
- "code" : 10057
- }
- > db.auth('sa','sa')
- 0
返回0驗(yàn)證失敗。
好吧,不繞圈子,其實(shí)super admin必須從admin那么登錄 然后 再use其它表才可以。
- > use admin
- > use admin
- switched to db admin
- > db.auth('sa','sa')
- 1
- > use test
- switched to db test
- > show collections
- >
如果想單獨(dú)訪問一個(gè)表,用獨(dú)立的用戶名,就需要在那個(gè)表里面建相應(yīng)的user。
- [root@:/usr/local/mongodb/bin]#./mongo
- MongoDB shell version: 1.8.2
- connecting to: test
- > use admin
- switched to db admin
- > db.auth('sa','sa')
- 1
- > use test
- switched to db test
- > db.addUser('test','test')
- {
- "user" : "test",
- "readOnly" : false,
- "pwd" : "a6de521abefc2fed4f5876855a3484f5"
- }
- >
當(dāng)然必須有相關(guān)權(quán)限才可以建立。
再登錄看看:
- [root@:/usr/local/mongodb/bin]#./mongo
- MongoDB shell version: 1.8.2
- connecting to: test
- > show collections
- Fri Jul 22 14:45:08 uncaught exception: error: {
- "$err" : "unauthorized db:test lock type:-1 client:127.0.0.1",
- "code" : 10057
- }
- > db.auth('test','test')
- 1
- > show collections
- system.indexes
- system.users
- >
關(guān)于MongoDB權(quán)限管理就介紹到這里,希望能對各位有所幫助。
【編輯推薦】