pgg08927
pgg08927

Reputation: 175

MongoDB atlas error: uncaughtException: (Unauthorized) not authorized on admin to execute command

I am new to mongodb please excuse my language. I have deployed backend of the application to heroku and tring to connect it with Mongodb. The steps I followed as below.

  1. I created a project on mongodb.com, added a user(with role atlasAdmin@admin), added IP.

  2. I connected it to MongoDB Compass and added the data required using compass

  3. I used connect your application and saved below string in heroku config enviornment variable after editing username, password and dbname.

mongodb+srv://<username>:<password>@vidly.5pn3u.azure.mongodb.net/<dbname>?retryWrites=true&w=majority

but it is giving error as 'MongoError: (Unauthorized) not authorized on admin to execute command

I tried below options to fix error:

  1. Using mongo shell I tried to create user. I used string mongo "mongodb+srv://vidly.5pn3u.azure.mongodb.net/<dbname>" --username <username> to connect to mongo and then
use admin
db.createUser(
  {
    user: 'admin',
    pwd: 'password',
    roles: [ { role: 'root', db: 'admin' } ]
  }
);
exit;

But it is giving error : uncaught exception: Error: couldn't add user: (Unauthorized) not authorized on admin to execute command

  1. on cloud.mongodb.com I changed a role on mongodb server to (dbAdminAnyDatabase@admin, backup@admin, readWriteAnyDatabase@admin, clusterMonitor@admin) but its giving same error.

Please advice how this error can be resolved?

Edit:

I am able to access database using mongo connect with the mongo shell using show dbs on shell (but not able to create user) and connect using mongodb compass and edit database in compass. But when I try connect your application option it is giving this error.

The user added has these access (dbAdminAnyDatabase@admin, backup@admin, readWriteAnyDatabase@admin, clusterMonitor@admin), even with this user its not working.

Upvotes: 2

Views: 742

Answers (1)

mingxingwang
mingxingwang

Reputation: 339

You need to install the recent version of mongoose. For example, you can install mongoose by using "yarn add mongoose".

Upvotes: 0

Related Questions