M103 - Error: couldn’t add user: not master

By performing this > mongod --port 27000 --auth --config mongod.conf
And then this >
mongo admin --host localhost:27000 --eval ’
db.createUser({
user: “m103-admin”,
pwd: “m103-pass”,
roles: [
{role: “root”, db: “admin”}
]
})

Do you know why I am getting this error ?

MongoDB shell version v4.0.5
connecting to: mongodb://localhost:27000/admin?gssapiServiceName=mongodb
Implicit session: session { “id” : UUID(“02480d76-daaa-45c9-86db-49aac11fd6cb”) }
MongoDB server version: 4.0.5
2021-10-14T21:17:26.081+0000 E QUERY [js] Error: couldn’t add user: not master :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype.createUser@src/mongo/shell/db.js:1491:15
@(shell eval):1:1

You are most likely connected directly to a SECONDARY node of an initialized replica set.

Please provide the content of your mongod.conf.

Provide a screenshot of the mongo command

rs.status()

Also provide a screenshot of the shell command

ps -aef | grep [m]ongo

Hi, sorry for my late reply. Internet was down due to political situation in Bangladesh.
However i have tried you suggestion. nothing happened on in browser ide. here are the screenshot.


Run rs.status() at mongo prompt (">")
and ps -aef|grep [m]ongo at os prompt (#)
Also show your config file contents

1 Like

Dear Ramachandra_Tummala,
here are these status for your kind review-

conf file YAML :
storage:
dbPath: “/data/db”
replication:
replSetName: MN103

Mongo Shell Prompt :

>  ru.status()
> 2021-10-16T19:27:49.711+0000 E QUERY    [js] ReferenceError: ru is not defined :
> @(shell):1:1

User prompt :

user@M103# ps -aef|grep [m]ongo
396 root 0:00 mongo
402 root 0:03 mongod --port 27000 --auth --config mongod.conf

[Error: couldn’t add user: not master] is showing.

It is not ru.status()
Please run rs.status() and show us the output

to know what rs.status() returns, I tried to connect mongo shell after running mongod server on port 27000. terminal shows following error for > user@M103# mongo command:

MongoDB shell version v4.0.5
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
2021-10-17T09:55:45.097+0000 E QUERY [js] Error: couldn’t connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :
connect@src/mongo/shell/mongo.js:328:13

Returns of ps -aef|grep [m]ongo remains same except the numbers before root:

309 root 0:01 mongod --port 27000 --auth --config mongod.conf

So now another problem …

Your mongod is running on port 27000
So you should connect giving the port number 27000

Looks like you are trying to connect by issuing just mongo
It will try to connect to default mongod running on port 27017
Since there is no mongod running on port 27017 it is failing

1 Like

Dear [Ramachandra_Tummala] thank you for your finding .

Returns of rs.status() on mongoShell in IDE is following:

{
        "ok" : 0,
        "errmsg" : "no replset config has been received",
        "code" : 94,
        "codeName" : "NotYetInitialized"
}

What have to do ?

Run rs.initiate() wait for sometime and then check rs.status() again

After calling rs.initiate() following is the returns of calling rs.status() on MN103:OTHER>:

{
        "set" : "MN103",
        "date" : ISODate("2021-10-17T13:38:31.557Z"),
        "myState" : 1,
        "term" : NumberLong(1),
        "syncingTo" : "",
        "syncSourceHost" : "",
        "syncSourceId" : -1,
        "heartbeatIntervalMillis" : NumberLong(2000),
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1634477902, 1),
                        "t" : NumberLong(1)
                },
                "readConcernMajorityOpTime" : {
                        "ts" : Timestamp(1634477902, 1),
                        "t" : NumberLong(1)
                },
                "appliedOpTime" : {
                        "ts" : Timestamp(1634477902, 1),
                        "t" : NumberLong(1)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1634477902, 1),
                        "t" : NumberLong(1)
                }
        },
        "lastStableCheckpointTimestamp" : Timestamp(1634477892, 1),
        "members" : [
                {
                        "_id" : 0,
                        "name" : "localhost:27000",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 360,
                        "optime" : {
                                "ts" : Timestamp(1634477902, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2021-10-17T13:38:22Z"),
                        "syncingTo" : "",
                        "syncSourceHost" : "",
                        "syncSourceId" : -1,
                        "infoMessage" : "",
                        "electionTime" : Timestamp(1634477601, 2),
                        "electionDate" : ISODate("2021-10-17T13:33:21Z"),
                        "configVersion" : 1,
                        "self" : true,
                        "lastHeartbeatMessage" : ""
                }
        ],
        "ok" : 1,
        "operationTime" : Timestamp(1634477902, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1634477902, 1),
                "signature" : {
                        "hash" : BinData(0,"I+0zKcMujy4lzMJpp9UOleguRbI="),
                        "keyId" : NumberLong("7020027846634504194")
                }
        }
}

And then user has been created successfully. Here are the results :slight_smile:

user@M103# mongo admin --host localhost:27000 --eval 'db.createUser({user:"m103-admin",pwd:"m103-pass",roles:[{role:"root", db:"admin"}]})'
MongoDB shell version v4.0.5
connecting to: mongodb://localhost:27000/admin?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("f98ccaac-eaf8-4a2e-a7fd-6f49d10c687a") }
MongoDB server version: 4.0.5
Successfully added user: {
        "user" : "m103-admin",
        "roles" : [
                {
                        "role" : "root",
                        "db" : "admin"
                }
        ]
}

I appreciate your advice. It has exactly solved my problem. Thank you for being helpful to me.