使用Mongodb數據庫,須要爲各個數據庫增長用戶權限,查了一下發現下面代碼:javascript
javascriptuse test2 db.addUser( { user: "test", pwd: "admin", roles: [ "readWrite", "dbAdmin" ] } )
執行後發現:html
bash$ mongo 192.168.1.111/test2 -u test -p admin MyMongo:PRIMARY>
Error: 18 { ok: 0.0, errmsg: "auth failed", code: 18 } at src/mongo/shell/db.js:228java
bash$mongo --help MongoDB shell version: 2.4.9
我使用的Mongodb是2.6版本,可是Shell是2.4.9的,感受是這出現了文問題,因此採用下面方案:mongodb
bashsudo apt-get remove mongodb-clients sudo apt-get autoremove sudo apt-get autoclean
bashsudo apt-get install mongodb-org-shell=2.6.1 $mongo --help MongoDB shell version: 2.6.1
使用新的(Mongodb 2.6的代碼)添加用戶shell
javascriptuse test2 db.createUser( { user: "test", pwd: "admin", roles: [ { role: "readWrite", db: "test2" }, ] } )
bash$mongo 192.168.1.111/test2 -u test -p admin MyMongo:PRIMARY>
登陸成功!!!!數據庫