0

I created an admin user on a Mongo 4.4 instance (Ubuntu)

db.createUser(
  {
    user: "admin",
    pwd: "password",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]
  }
)

Then, I tried to rs.initiate() and it told me I am not "master". I read this SO thread where people suggest that I do db.grantRolesToUser('admin', [{ role: 'root', db: 'admin' }]), but it doesn't work!

It gives me the following error:

uncaught exception: Error: not master :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype.grantRolesToUser@src/mongo/shell/db.js:1613:15
@(shell):1:1

Do I have to uninstall MongoDB, reinstall, then create the root user, and then create the admin user?

Besto
  • 388
  • 1
  • 13

1 Answers1

1

Your node can only be "not master" if it's already in a replica set. You can only initiate a replica set once and so you shouldn't be trying to initiate it again, it's already initiated.

D. SM
  • 13,584
  • 3
  • 12
  • 21
  • Do you know how to make an admin become "root" or is it useless now? – Besto Feb 10 '21 at 18:57
  • When you create a user then it is replicated to the secondary. I don't understand your question. – Wernfried Domscheit Feb 10 '21 at 19:07
  • I tried to check whether it is replica, but it tells me I'm unauthorized... I know it's probably replica, but isn't it useless if I can't use it? How do I become authorized to use it? Do I have to uninstall? `> rs.status() { "ok" : 0, "errmsg" : "not authorized on admin to execute command { replSetGetStatus: 1.0, lsid: { id: UUID(\"Some ID") }, $db: \"admin\" }", "code" : 13, "codeName" : "Unauthorized" }` I asked after doing `db.auth('admin','pwd')` – Besto Feb 10 '21 at 19:08
  • To find out the type of a node, issue ismaster to it which doesn't require authentication. See https://github.com/mongodb/specifications/blob/master/source/server-discovery-and-monitoring/server-discovery-and-monitoring.rst#parsing-an-ismaster-response under "parsing ismaster response". – D. SM Feb 10 '21 at 22:23