mongodb 无法创建用户

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31387053/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:30:04  来源:igfitidea点击:

Can't create user

mongodbgoogle-compute-engine

提问by Miguel Fermin

I was using MongoDB version 2.6.6 on Google Compute Engine and used the click to deploy method.

我在 Google Compute Engine 上使用了 MongoDB 版本 2.6.6,并使用了点击部署方法。

rs0:SECONDARY> db.createUser({user:"admin", pwd:"secret_password", roles:[{role:"root", db:"admin"}]})
2015-07-13T15:02:28.434+0000 Error: couldn't add user: not master at src/mongo/shell/db.js:1004

rs0:SECONDARY> use admin
switched to db admin
rs0:SECONDARY> db.createUser({user:"admin", pwd:"secret_password", roles:["root"]})
2015-07-13T15:13:28.591+0000 Error: couldn't add user: not master at src/mongo/shell/db.js:1004

采纳答案by Kamran

MongoDB will be deployed in a cluster of Compute Engine instances (also known as a MongoDB replica set). Each instance will use a boot disk and separate disk for database files.

MongoDB 将部署在 Compute Engine 实例集群(也称为 MongoDB 副本集)中。每个实例将使用一个引导磁盘和单独的磁盘用于数据库文件。

Primary and master nodes are the nodes that can accept writes. MongoDB's replication is “single-master:” only one node can accept write operations at a time.

主节点和主节点是可以接受写入的节点。MongoDB 的复制是“single-master”:一次只有一个节点可以接受写操作。

Secondary and slave nodes are read-only nodes that replicate from the primary.

辅助节点和从节点是从主节点复制的只读节点。

Your error message looks like you are trying to add the user on the secondary. Try adding the user in the primary.

您的错误消息看起来像是您正在尝试在辅助服务器上添加用户。尝试在主中添加用户。

回答by comfytoday

I had a similar problem with mongo 3.2:

我在 mongo 3.2 上遇到了类似的问题:

Error: couldn't add user: not master :

错误:无法添加用户:不是主人:

When trying to create a new user, with root role.

尝试创建具有 root 角色的新用户时。

I was using only a local copy of mongo.

我只使用了 mongo 的本地副本。

In my mongod.conf file I had the following uncommented:

在我的 mongod.conf 文件中,我取消了以下注释:

replication:
   replSetName:  <node name>

Commenting that out and restarting fixed the problem. I guess mongo thought it was part of a replication set, and was confused as to who the Master was.

注释掉并重新启动解决了问题。我猜 mongo 认为它是复制集的一部分,并且对 Master 是谁感到困惑。

Edit:

编辑:

I've also found that if you ARE trying to setup a replication set, and you get the above error, then run:

我还发现,如果您尝试设置复制集,并且出现上述错误,请运行:

rs.initiate()

rs.initiate()

This will start a replication set, and set the current node as PRIMARY.

这将启动一个复制集,并将当前节点设置为 PRIMARY。

Exit, and then log back in and you should see:

退出,然后重新登录,您应该看到:

PRIMARY>

主要>

Now create users as needed.

现在根据需要创建用户。

回答by D. SM

I ran into this error when scripting replica set creation.

我在编写副本集创建脚本时遇到了这个错误。

The solution was to add a delay between rs.initiate()and db.createUser().

解决方案是在rs.initiate()和之间添加延迟db.createUser()

Replica set creation is seemingly done in background and it takes time for the primary node to actually become primary. In interactive use this doesn't cause a problem because there is a delay while typing the next command, but when scripting the interactions the delay may need to be forced.

副本集的创建似乎是在后台完成的,主节点真正成为主节点需要时间。在交互使用中,这不会导致问题,因为在键入下一个命令时会出现延迟,但是在编写交互脚本时可能需要强制延迟。

回答by Dylan

I ran into this issue when I thought I was running mongo 3.4 but it was mongo 3.6. Uninstalling 3.6 and installing 3.4 fixed my issue.

当我以为我正在运行 mongo 3.4 但它是 mongo 3.6 时,我遇到了这个问题。卸载 3.6 并安装 3.4 解决了我的问题。