在mongodb上添加用户报错,如下:
> db.addUser(‘user01‘,‘123456‘); 2016-07-29T14:50:28.932+0800 E QUERY TypeError: Property ‘addUser‘ of object admin is not a function at (shell):1:4
经查,原因是在mongodb版本3以上已经弃用了adduser,改成createuser了。
[candon@xxx bin]$ ./mongo 127.0.0.1:27017 MongoDB shell version: 3.0.6 connecting to: 127.0.0.1:27017/test > use adminuse admin switched to db admin > db.version() 3.0.6 > db.createUser({user:‘user01‘, pwd:‘123456‘, roles:[‘userAdminAnyDatabase‘]}) Successfully added user: { "user" : "user01", "roles" : [ "userAdminAnyDatabase" ] } > db.auth("user01","123456") 1 > show users { "_id" : "admin.root", "user" : "root", "db" : "admin", "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] } { "_id" : "admin.user01", "user" : "user01", "db" : "admin", "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] } > exit bye
参考文献:MongoDB常用操作命令大全
本文出自 “candon123” 博客,谢绝转载!
原文:http://candon123.blog.51cto.com/704299/1831726