mongodb “db.createUser 不是函数”和“密码不能为空”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23034879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"db.createUser is not a function" and "password can't be empty"
提问by Xeos
I am trying to setup the authentication on MongoDB. I've uncommented auth=true
in configuration file and restarted the server. The first connection via mongowas rejected. The second one (immediately after the first one) went through and I got this in the logs:
我正在尝试在 MongoDB 上设置身份验证。我auth=true
在配置文件中取消了注释并重新启动了服务器。通过mongo的第一个连接被拒绝。第二个(在第一个之后)通过了,我在日志中得到了这个:
Sat Apr 12 14:56:46 [initandlisten] connection accepted from 127.0.0.1:44001 #1
Sat Apr 12 14:56:46 [conn1] note: no users configured in admin.system.users, allowing localhost access
While in console, I am able to execute commands, however if I try to follow any of the numberous tutorials (including the official ones), I get either db.createUser is not a function
when executing db.createUser()
or password can't be empty
when executing db.addUser()
:
在控制台中,我能够执行命令,但是如果我尝试遵循任何数量众多的教程(包括官方教程),我会db.createUser is not a function
在执行db.createUser()
或password can't be empty
执行时得到db.addUser()
:
The object which I am passing is:
我传递的对象是:
{
user: "username",
pwd: "passphrase",
roles: [
{
role: "userAdminAnyDatabase",
db: "admin"
}
]
}
回答by Dave Albert
What version of mongodb are you running? db.createUser was introduced in version 2.6
你运行的是什么版本的mongodb?db.createUser 是在 2.6 版本中引入的
Deprecated since version 2.6: Use db.createUser() and db.updateUser() instead of db.addUser() to add users to MongoDB.
In 2.6, MongoDB introduced a new model for user credentials and privileges, as described in Security Introduction. To use db.addUser() on MongoDB 2.4, see db.addUser() in the version 2.4 of the MongoDB Manual.
2.6 版后已弃用:使用 db.createUser() 和 db.updateUser() 而不是 db.addUser() 将用户添加到 MongoDB。
在 2.6 中,MongoDB 引入了用户凭据和权限的新模型,如安全介绍中所述。要在 MongoDB 2.4 上使用 db.addUser(),请参阅 MongoDB 手册 2.4 版中的 db.addUser()。
回答by ivanleoncz
Regarding my experience, this issue also happens when you have an old version of Mongo Shell.
根据我的经验,当您使用旧版本的 Mongo Shell 时也会出现此问题。
So, be sure to have the Mongo Shell according to the version of your MongoDB Server.
因此,请务必根据您的 MongoDB 服务器版本安装 Mongo Shell。
$ mongo
MongoDB shell version v3.4.10
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.10
$ mongo
MongoDB shell 版本v3.4.10
连接到:mongodb://127.0.0.1:27017
MongoDB 服务器版本:3.4.10
Below:
以下:
$ mongo
MongoDB shell version v3.4.10
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.10
Server has startup warnings:
2018-01-16T11:43:00.195-0600 I STORAGE [initandlisten]
2018-01-16T11:43:00.195-0600 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2018-01-16T11:43:00.195-0600 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
2018-01-16T11:43:00.687-0600 I CONTROL [initandlisten]
2018-01-16T11:43:00.687-0600 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-01-16T11:43:00.687-0600 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2018-01-16T11:43:00.687-0600 I CONTROL [initandlisten]
2018-01-16T11:43:00.687-0600 I CONTROL [initandlisten]
2018-01-16T11:43:00.687-0600 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-01-16T11:43:00.687-0600 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2018-01-16T11:43:00.687-0600 I CONTROL [initandlisten]
>
> use admin
switched to db admin
>
> db.createUser( {user:"mongoloide", pwd:"mongoloide*2017", roles:["root"]} )
Successfully added user: { "user" : "mongoloide", "roles" : [ "root" ] }