how to solve the error reading database : not authorized in mongo DB

Sekhar picture Sekhar · Feb 19, 2018 · Viewed 8.6k times · Source

am using Mongo DB version 3.4 and I have a user in DB

C:\Program Files\MongoDB\Server\3.4\bin>mongo
MongoDB shell version v3.4.5
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.5
> use admin
switched to db admin
> db.auth("myUserAdmin","abc123");
1

I have used following query with authentication to restore the dump file

mongorestore --host 50.50.1.57:27017 --username myUserAdmin --password abc123 --authenticationDatabase admin --db targetdb E:\test\DB\test_02_19_2018_06_00_01\test

but I got the issue can u suggest, please

2018-02-19T14:45:29.743+0530    the --db and --collection args should only be used when restoring from a BSON file. Other uses are deprecated and will not exist in the future; use --nsInclude instead
2018-02-19T14:45:29.745+0530    building a list of collections to restore from E:\test\DB\test_02_19_2018_06_00_01\test dir
2018-02-19T14:45:29.790+0530    Failed: targetdb.access_token: error reading database: not authorized on targetdb to execute command { listCollections: 1, cursor: { batchSize: 0 } }

Answer

user9251303 picture user9251303 · Feb 19, 2018

MyUserAdmin doesn't have sufficient privilege to restore into targetdb.

You can use Mongodb's built in roles:

restore, which will provide you the privileges needed to restore data from backups that do not include system.profile collection data. It will also provide you with the following action on any resource:

listCollections

Which will solve the error you're seeing:

not authorized on targetdb to execute command { listCollections: 1, cursor: { batchSize: 0 } }

backup Will provides minimal privileges needed for backing up data.

To update your current user with the correct roles/privs run:

db.updateUser( "MyUserAdmin",
  { roles : [  
    { role : "restore", db : "admin"  },
    { role : "backup", db : "admin"  }
  ] } )