23

I have a MongoDB Atlas cluster within which I am trying to simply copy a database within the same instance. Unfortunately, every time I try to run db.copyDatabase() or copydb admin command, I get the following error:

not authorized on admin to execute command

Which is very strange, because I just have one user, and it has access to everything, at least as far as I can tell on atlas:

enter image description here

I did some search in stackoverflow, but it looked like most of the answers pertain to mongod instances running locally, and not ones running in atlas... Did I setup the user wrong?

balexandre
  • 73,608
  • 45
  • 233
  • 342
Seaside9
  • 595
  • 1
  • 4
  • 11

13 Answers13

25

I ended up asking this question to MongoDB University discussion board. In case anyone comes across this, this is apparently because I am using a free cluster.

Seaside9
  • 595
  • 1
  • 4
  • 11
  • 6
    For your (and anybody else coming across this) reference: https://docs.atlas.mongodb.com/unsupported-commands/ and https://docs.atlas.mongodb.com/reference/free-shared-limitations/ – Pete Garafano Mar 05 '18 at 16:09
  • 2
    I have the same problem but M10 did changing to M10 resolved this? – Alan Ortega Sep 25 '18 at 21:49
  • https://stackoverflow.com/questions/49179829/cannot-read-write-on-a-mongodb-atlas-database-using-mongoose Above is the best answer for me. – Huiyang Shan Aug 06 '19 at 03:32
22

I had the same issue when I was trying to connect to a cluster with Node.js version 3.0 or later and using the link below:

mongodb+srv://username:password@cluster0-eoowo.mongodb.net/test?retryWrites=true

By selecting version 2.2.12 or later and using the link provided, everything went well

mongodb://username:password@cluster0-shard-00-00-eoowo.mongodb.net:27017,cluster0-shard-00-01-eoowo.mongodb.net:27017,cluster0-shard-00-02-eoowo.mongodb.net:27017/test?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin&retryWrites=true

dktistakis
  • 451
  • 4
  • 7
14

This drove me insane... Turns out the standard link provided by MongoDB was trying to name my default database "admin". Change the link from

mongodb+srv://username:password@portfoliosite-ezeot.mongodb.net/admin?retryWrites=true&w=majority 

to

mongodb+srv://username:password@portfoliosite-ezeot.mongodb.net/test?retryWrites=true&w=majority

You can put any word in for test just not admin. Hope this helps!

JohannesM
  • 213
  • 2
  • 14
Trey Cooper
  • 141
  • 1
  • 5
  • Thanks, help me a lot, too. – Gilberto Galea Oct 04 '19 at 22:53
  • Thank you so much! This was my exact issue! Once I set my Node.js driver to version 2.2.12 or later I got further in the process, but it still just refused with a "Error: querySrv EREFUSED" error. Changing the "admin" to literally anything else fixed the error. – Earle Poole Oct 08 '19 at 03:37
6

just use this version and then copy its link and replace the username and password from your mongodb atlas that have the permision to read and write and it will connect i was also having the same problem spent a lot of time to solve this then i tries 2.2.12 version and it workerd.

enter image description here

virk
  • 71
  • 1
  • 1
3

I had faced a similar obstacle and spent hours, the problem was with the connection string provided by mongodb atlas which is: mongodb+srv://username:password@clusterName-jynkd.mongodb.net/test?retryWrites=true

I used a different connection string and worked perfectly. This is it: mongodb://username:password@clusterName+port/yourdb?retryWrites=true&ssl=true&authSource=admin. Make sure the clustername + port are like this: clusterName-shard-00-00-jynkd.mongodb.net:27017

HMaziad
  • 189
  • 2
  • 5
3

One other cause of this error is being logged in as a different user than the one who started the bad op -- to use db.killOp you need to be logged into the mongo shell as the same user who started the operation.

On Atlas, database users can utilize the db.killOp() command to kill their own operations. You are correct that an account with atlasAdmin privileges cannot kill operations on the cluster that were not initiated by that account.

If you authenticate in the mongo shell with the user that the application used to initiate the offending operations, you should then be able to use db.killOp() to kill said operations.

(source is atlas support ticket)

Community
  • 1
  • 1
klhr
  • 3,300
  • 1
  • 11
  • 25
0

this error is because you are trying create the colecction above admin, in atlas this isn't possible because securty.

in this case you need to use the word key use [collection_name]

this is the link for this answer.

it is in spanish.

https://victorroblesweb.es/2016/12/24/crear-una-base-datos-mongodb/

0

In case you have been using mongoose, I solved a similar error updating mongoose to latest version (based on this post).

mariano_c
  • 371
  • 2
  • 9
0

In my case I had this issue while trying to connect to mongodb atlas.I removed the error by removing these from the mongdb uri string . ssl=true&replicaSet=Cluster0-shard-0&authSource=admin&retryWrites=true

Original link : mongodb+srv://username:password@hostname/dbname?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin&retryWrites=true

After rectification : mongodb+srv://username:password@hostname/dbname

Athira V Ajit
  • 316
  • 5
  • 8
0

This answer is not 100% specific to the OG question, however, this is the main post I kept ending up at when googling my error message "mongodb atlas errmsg: '(unauthorized) not authorized on admin to execute command"

If you are coming across this error in 2021 and are using Mongoose V4 or lower to connect with MongoDB Atlas, try updating Mongoose to the latest version (V5.12.13 at the time of posting this answer).

Open your terminal, navigate to your project directory, and run

npm i mongoose@latest    

I had been using starter code from an online course on Node.js and Express that was made in mid-2018. The starter code had been using Mongoose V4 and this solution worked for me. My MongoDB Atlas cluster was M0 free tier.

ATumms
  • 1
0

I also had a few problems with this, a combination of answers from this post helped me, thanks. :) I was trying to seed from an old project, so I upgraded mongoose to latest version, removed {useMongoClient: true} option that was deprecated, and then used the connectivity string as per given me by mongodb atlas (string starting with mongodb://, option 'use node 2.12 or higher'). I also had some other problem to do with connectivity but this was a problem with the whitelisted IPs I had in my DB config. Whitelisted 0.0.0.0/0 for any access to check this was the problem.

JBird
  • 121
  • 1
  • 8
-1

i have changed node version 2.2.12 or later

raj raj
  • 11
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-ask). – Community Sep 22 '21 at 09:59
-1

I was having the same issue on "mongoose": "^4.7.8" After upgrade to "mongoose": "^6.3.8", issue resolved.