Unable to create user

I used the version of MongoDB 2.6.6 in the Google Compute Engine and used the click method to deploy.

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 
+7
source share
4 answers

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

Primary and leading nodes are nodes that can receive records. MongoDBs replication is the "only master": only one node can accept write operations at a time.

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

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

+2
source

I had a similar problem with mongo 3.2:

Error: Failed to add user: not master:

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

I used only a local copy of mongo.

In my mongod.conf file, I had the following without comment:

 replication: replSetName: <node name> 

Commenting on this, restart and fix the problem. I think the mongo thought it was part of a replica set, and was embarrassed by those who were Master.

Edit:

I also found that if you are trying to set up a replication set and get the above error, run:

rs.initiate ()

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

Log out and then log in and you will see:

PRIMARY>

Now create users as needed.

+12
source

I encountered this error while creating a set of replication scripts.

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

Creating a replica set seems to be running in the background, and the primary node really needs time to become the primary. With interactive use, this does not cause problems, since the next command introduces a delay, but in interaction scenarios, a delay may be required forcibly.

+3
source

I ran into this problem when it seemed to me that I was running Mongo 3.4, but it was Mongo 3.6. Removing 3.6 and installing 3.4 fixed my problem.

0
source

All Articles