简体   繁体   中英

Error Connecting to MongoDB: “not authorized on admin to execute command”

I am currently trying to connecting my app to a MongoDB database (previously on mlab which worked). I am using the string provided for connection:

mongodb+srv://admin:password@cluster0.stbko.mongodb.net/name?retryWrites=true&w=majority

When I run the app, I get the following error message:

(node:35944) UnhandledPromiseRejectionWarning: MongoError: (Unauthorized) not authorized on admin to execute command { insert: "system.indexes", documents: [[{ns admin.sessions} {key [{expires 1}]} {name expires_1} {expireAfterSeconds 0} {unique false}]], ordered: true, writeConcern: { w: "majority" } }

I've tried this with both the mlab migration and uploading a backup copy of the database with no luck. I tried changing the privileges on the user as well... I cannot track down where this error is arising from in the code either.

I have reviewed multiple posts which I am not sure they apply to me: #1 , #2

I have connected to the database using the mongo shell however get the following error when I tried to rung db.getUsers()

2020-12-09T17:17:43.592-0500 E QUERY    [thread1] Error: (Unauthorized) not authorized on admin to execute command { usersInfo: 1.0, $clusterTime: { clusterTime: {1607552228 10}, signature: { hash: {0 [ random numbers here ]}, keyId: more numbers } }, $db: "myDatabase" } :

Any thoughts?

How about connecting without writeMajority? use the connection string

mongodb+srv://admin:password@cluster0.stbko.mongodb.net/name

The issue was with the versions of mongo-connect and connect-mongodb-session were out of date. After I upgraded I had no problems.

Also db.getUsers() has to be run for a specific user. When I tried to find my admin user it worked without a problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM