Sharding even with Admin access - (Unauthorized) not authorized on admin to execute command { enableSharding

Hello, I’m new to MongoDB and have encountered an error trying to shard a collection. Here’s the setup I have:

Project: ‘weather’
Database: ‘weather’
Collections within ‘weather’: ‘users’ and ‘weatherData’
Objective: Create 2 partitions of ‘weatherData’ using a partition key

My access setup in Atlas for user appemail002:
Database access for database ‘weather’: * atlasAdmin[@]admin

  • dbAdmin[@]weather
  • dbAdminAnyDatabase[@]admin
  • enableSharding[@]admin
  • readWrite[@]weather.weatherData
  • readWriteAnyDatabase[@]admin
    Resources: All Resources

Project access for project ‘weather’:
Project Owner, Project Cluster Manager, Project Data Access Admin, Project Data Access Read Write

Organization access: Organization Owner

Problem: When I connect to the database and try to shard the collection ‘weatherData’, I get this error (I’m using Terminal on Mac):

Atlas atlas-l0q9v9-shard-0 [primary] admin> sh.enableSharding(“weather”)

MongoServerError: (Unauthorized) not authorized on admin to execute command { enableSharding: “weather”, lsid: { id: {4 [36 21 142 212 209 203 73 59 180 167 251 12 105 72 226 177]} }, $clusterTime: { clusterTime: {1686475395 2}, signature: { hash: {0 [227 222 216 58 147 247 173 17 114 162 175 202 192 80 195 225 79 131 207 72]}, keyId: 7187767833034489856.000000 } }, $db: “admin” }

From what I can see, I have the required admin access to enable sharding. I’m not sure what I’ve missed.

Looking forward to any advice.

Thanks!

Hi @D_M2,
Can you paste your connection string?
In which db you’ve create the user?

BR

Thanks for the reply @Fabio_Ramohitaj!

Here’s my connection string for Mongo shell in MacOS:
mongosh “mongodb+srv://weather.xdeoqms.mongodb.net” --apiVersion 1 --username appemail002

And this is the access info for db ‘weather’:

Update: I was able to create an index for db ‘weather’> collection ‘weatherData’ > index ‘Weather Station’ (this is my Shard key)

Atlas atlas-l0q9v9-shard-0 [primary] weather> db.weatherData.createIndex({ "Weather Station": 1 })
Weather Station_1

but still getting the same error when trying ‘sh.enableSharding(“weather”)’. – ‘weather’ is the database

Atlas atlas-l0q9v9-shard-0 [primary] weather> sh.enableSharding("weather")
MongoServerError: (Unauthorized) not authorized on admin to execute command { enableSharding: "weather", lsid: { id: {4 [210 136 236 165 103 73 78 142 157 207 39 31 47 93 86 139]} }, $clusterTime: { clusterTime: {1686529903 1}, signature: { hash: {0 [43 89 216 71 105 224 184 255 137 187 7 86 38 240 215 220 177 21 20 55]}, keyId: 7187767833034489856.000000 } }, $db: "admin" }

I also tried “sh.enableSharding(“weatherData”)” - weatherData is the collection I want to shard within the ‘weather’ database but got the same error

Atlas atlas-l0q9v9-shard-0 [primary] weather> sh.enableSharding("weatherData")
MongoServerError: (Unauthorized) not authorized on admin to execute command { enableSharding: "weatherData", lsid: { id: {4 [210 136 236 165 103 73 78 142 157 207 39 31 47 93 86 139]} }, $clusterTime: { clusterTime: {1686530065 15}, signature: { hash: {0 [55 207 72 0 87 110 79 207 192 229 63 95 190 141 226 62 159 6 231 158]}, keyId: 7187767833034489856.000000 } }, $db: "admin" }

Thanks!

Looks like you are running the command from a shard? Run it against mongos instead.

1 Like

Thanks @Kobe_W - how do I do that? I used the connection string provided in Atlas but it seems to connect me to the shard straight away… This is the first time I’m using Mongodb for for study. Thanks again!

Ho @D_M2,
Add at the end of you connection string /weather.
In you case:

mongodb+srv://weather.xdeoqms.mongodb.net/weather

I think in this way, will work correctly.

BR

Thanks BR, I tried it just now, and it seems to still point me to the shard… very weird

If it helps at all, here’s what my Metrics page looks like for db ‘weather’ - there seems 2 be 1 Primary Shard and 2 Secondary Shards - I did not create any of these… What I am after is:

Partition collection ‘weatherData’ (which lives in db ‘weather’) into 2 shards/partitions

Hi @D_M2,
Let’s do this, to create a temporary solution, we can create a root user in the admin db.
So in the connection string you will have to put:
mongodb+srv://weather.xdeoqms.mongodb.net/admin
And log in with the new user name.

Regards

1 Like

Thanks heaps for your time BR. I got this reply from MongoDB, apparently it’s an issue with being on the M0 free cluster:

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.