MongoDB的許可權配置:開啟auth之後的eval許可權

來源:互聯網
上載者:User

標籤:mongodb auth eval

本文為工作日誌,解決當開啟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 有一段描述:

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)建立一個角色,比如叫 sysadmin,需要先切換到admin庫進行如下操作:

> use adminswitched to db admin> db.createRole({role:'sysadmin',roles:[],... privileges:[... {resource:{anyResource:true},actions:['anyAction']}... ]})

3)然後,建立一個使用者,使用這個角色,注意,這個角色的db是admin,操作如下:

> use woplusswitched to db woplus> db.createUser({... user:'woplus',... pwd:'[email protected]',... roles:[... {role:'sysadmin',db:'admin'}... ]})

4)OK,我們看看效果。先看看使用者情況,如下:

> use adminswitched 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 [email protected]  192.168.1.84/woplusMongoDB shell version: 2.6.8connecting to: 192.168.1.84/woplus> > use woplusswitched to db woplus> db.eval('return 222')222




MongoDB的許可權配置:開啟auth之後的eval許可權

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.