MongoDB的權限配置:開啟auth之后的eval權限
來源:程序員人生 發布時間:2015-03-24 08:03:57 閱讀次數:4209次
本文為工作日志,解決當打開MongoDB的 --auth 以后,致使沒法使用 db.eval() 的問題。
問題描寫:
使用--auth啟動MongoDB,登錄成功后,履行db.eval,報以下毛病:
> db.eval('return 1111')
2015-03-04T15:18:54.062+0800 {
"ok" : 0,
"errmsg" : "not authorized on test to execute command { $eval: "return 1111" }",
"code" : 13
} at src/mongo/shell/db.js:403
>
解決方案:
在官網 http://docs.mongodb.org/manual/reference/command/eval/#dbcmd.eval 有1段描寫:
If authorization is enabled, you must have access to all actions on all resources in order to run eval.
Providing such access is not recommended, but if your organization requires a user to run eval,
create a role that grants anyAction on anyResource.
Do not assign this role to any other user.
解決步驟:
1)不帶--auth參數啟動數據庫,所以不需要帳號便可連上MongoDB。
2)新建1個角色,比如叫 sysadmin,需要先切換到admin庫進行以下操作:
> use admin
switched to db admin
> db.createRole({role:'sysadmin',roles:[],
... privileges:[
... {resource:{anyResource:true},actions:['anyAction']}
... ]})
3)然后,新建1個用戶,使用這個角色,注意,這個角色的db是admin,操作以下:
> use woplus
switched to db woplus
> db.createUser({
... user:'woplus',
... pwd:'wo@1990',
... roles:[
... {role:'sysadmin',db:'admin'}
... ]})
4)OK,我們看看效果。先看看用戶情況,以下:
> use admin
switched to db admin
> db.system.users.find()
{ "_id" : "admin.root", "user" : "root", "db" : "admin", "credentials" : { "MONGODB-CR" : "dfda4d4e75995650c3e1b3f2b65b1920" }, "roles" : [ { "role" : "root", "db" : "admin" } ] }
{ "_id" : "woplus.woplus", "user" : "woplus", "db" : "woplus", "credentials" : { "MONGODB-CR" : "bcabae4fe4d7951fad37cb2d099437e8" }, "roles" : [ { "role" : "sysadmin", "db" : "admin" } ] }
然后,我們重啟
數據庫,加上 --auth 參數。
再從客戶端使用帳號登錄MongoDB。
./mongo -u woplus -p wo@1990 192.168.1.84/woplus
MongoDB shell version: 2.6.8
connecting to: 192.168.1.84/woplus
>
> use woplus
switched to db woplus
> db.eval('return 222')
222
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈