MongoServerError[Unauthorized]: not authorized on Database to execute command

Hi Team,

I have build mongoDB community edition and configured security setting in the DB conf file. After I started MongoDB. I created Admins. Finally tried to create database but getting the below error.

rs0 [direct: primary] admin> >use DBNAME
rs0 [direct: primary] DBNAME> db.student.insertOne({student_id:105,fname:“Rohit”,lname:“Agarwal”,course:“MySQL”})
MongoServerError[Unauthorized]: not authorized on DBName to execute command { insert: “student”, documents: [ { student_id: 105, fname: “Rohit”, lname: “Agarwal”, course: “MySQL”, _id: ObjectId(‘6601a1337d961c6dc1814c42’) } ], ordered: true, lsid: { id: UUID(“ca4bd51d-61c5-4b99-b014-bf184aab992b”) }, txnNumber: 2, $clusterTime: { clusterTime: Timestamp(1711382675, 1), signature: { hash: BinData(0, F7D5E0F5A7642A4D309724DFDFE8D3B3D9044060), keyId: 7346653907562528775 } }, $db: “DBName” }
rs0 [direct: primary] DBNAME>

Hi Team,

Kindly support on my current issue.

Hi

before setting the security settings in the config file I normally like to create a root user:

db.createUser(
    {
      user: "root",
      pwd: "setPassword",
      roles: [ "root" ]
    }
)

then enable security in the config file and start creating users with their roles.
this allows the root and any user you create and sign in with to be able to insert documents based on their roles given.

Kindest,
Gareth Furnell

This topic was automatically closed after 180 days. New replies are no longer allowed.