Sharded cluster instance(Node) getting authentication error

We have a Sharded cluster in production setup already. while running COMPACT command to disk reclaim in mogos is not working so we have tried in Sharded cluster instance to login with connection string getting error . Could anyone please help to fix. I am new to Mongodb

while login using connection string

mongo mongodb://user:user@123’@ip-192-168-133-183.ec2.internal:27017,ip-192-168-145-243.ec2.internal:27017,ip-192-168-222-182.ec2.internal:27017/admin?relicaSet=shardreplica01

Getting below error:
Error: Authentication failed. :
connect@src/mongo/shell/mongo.js:374:17
@(connect):2:6
exception: connect failed
exiting with code 1

Is your connect string correct?
There are typos
You have “@” occuring twice and replica is typed as relica

Thanks for your response.

I have updated MongoDB connection string like below:
mongodb://user:‘password’@ip-192-168-133-183.ec2.internal:27017,ip-192-168-145-243.ec2.internal:27017,ip-192-168-222-182.ec2.internal:27017/admin?replicaSet=shardreplica01&authSource=admin

but still I am getting below error. I have googled but not much idea. Please guide me to fix and let me know if you need addition config informations.

Error: can’t connect to new replica set master [ip-192-168-133-183.ec2.internal:27017], err: AuthenticationFailed: Authentication failed. :
connect@src/mongo/shell/mongo.js:374:17
@(connect):2:6
exception: connect failed
exiting with code 1

Are you using correct userid/pwd?
Can you connect to your replica nodes using the same id&pwd
You are authenticating against admin db and connecting to admin db
Is that user have admin private or a app user?
Did this connect string work before?
From where you got this?

Yes, We are using the Mongos user ID and PWD.
How do I connect replica nodes ?
I am not sure. The MongoDB sharded cluster already formed . While I am trying to run the COMPACT query to reclaim the disk space through mongos shell facing the error. So Google about the issue then I got the idea to run the command through Mongod shell. When tried to connect getting the authentication issue.

So consider me as a newbie MongoDB. Please guide me

  1. where I can find the connection string(/etc/mongod.conf) in which nodes
  2. How do authenticate to connect the mongod

Please check this link and refer to mongo documentation for Compact and connect strings for standalone,replica etc

Compact is a maintenance operation.Do not run any compact without understanding the implications

From your connect string remove replicaset and other nodes.
It will put you directly to the node you passed
Be clear about data bearing replica nodes and mongos

Sure. I will remove replicaset,nodes and let me try

I have changed the replicaset and other nodes from connection string. Please find the connection string below.

mongo mongodb://user:password@ip-192-168-X-X.ec2.internal:27017/db_name?authSource=admin

But still I am getting authentication error:
Error: Authentication failed. :
connect@src/mongo/shell/mongo.js:374:17
@(connect):2:6
exception: connect failed
exiting with code 1

I have tried , In command line I just wrote mongo and use myDatabase then db.auth("username","password")

but getting error

Error: Authentication failed.
0

Does this user exist?
In which db this user was created?
You have to authenticate in that db
Try mongo --host IP --port port_num -user -pwd --authenticationDatabase db_name instead of connect string you are trying
Check documentation or our forum threads for exact syntax

The users exist in mongos shell. but when I login sharded primary/secondary node getting bellow error.


“uncaught exception: Error: not authorized on admin to execute command { usersInfo: 1.0, lsid: { id: UUID(“8c86070e-17f1-417c-9219-6e194f0d8c15”) }, $clusterTime: { clusterTime: Timestamp(1680605390, 1), signature: { hash: BinData(0, 1BA96384824611713539DD67BE1B9F88757608DF), keyId: 7158478101695954964 } }, $db: “admin” } :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype.getUsers@src/mongo/shell/db.js:1659:15
@(shell):1:1”

I can able to authenticate in mongos shell but I am getting error while tried to login mongodb sharded nodes like primary/ secondary

It means user does not have privileges on admin db
Does other commands like show dbs,show users work?
What privs this user has on mongos?

1 Like

Hi @Nino_I ,

As mentioned from the documentation, you Need to create user in the primary of shard to perform operation directly on the shard.

These shard local users are completely independent from the users added to the sharded cluster via mongos . Shard local users are local to the shard and are inaccessible by mongos .

Direct connections to a shard should only be for shard-specific maintenance and configuration. In general, clients should connect to the sharded cluster through the mongos

BR

1 Like

Hi @ Fabio_Ramohitaj

How do we run the COMPACT command. I am getting error while running COMPACT in mongos

I dont have an idea sharded mongodb cluster. So please guide me where we can run the compact query

Hi @Nino_I ,
here is all the information you need:

BR

1 Like

I think we have a privileges on admin db . I have listed the details below

"_id" : "admin.fullintelAdmin",
                "userId" : UUID("sdgdsgadsgadgaag"),
                "user" : "User",
                "db" : "admin",
                "roles" : [
                        {
                                "role" : "dbAdmin",
                                "db" : "admin"
                        },
                        {
                                "role" : "myCustomCompactRole",
                                "db" : "admin"
                        },
                        {
                                "role" : "dbAdminAnyDatabase",
                                "db" : "admin"
                        },
                        {
                                "role" : "read",
                                "db" : "admin"
                        },
                        {
                                "role" : "clusterAdmin",
                                "db" : "admin"
                        },
                        {
                                "role" : "userAdminAnyDatabase",
                                "db" : "admin"
                        },
                        {
                                "role" : "root",
                                "db" : "admin"
                        },
                        {
                                "role" : "readWriteAnyDatabase",
                                "db" : "admin"
                        }
                ],

Thanks Fabio_Ramohitaj . Let me try and update

I have followed the documentation and give the privilege to access user and role but still I am getting same error while running from route node ( mongos).

I cannot able to login shared cluster node to run the compact query.

db.runCommand({'compact': 'collection_name'})
{
        "ok" : 0,
        "errmsg" : "compact not allowed through mongos",
        "code" : 115,
        "codeName" : "CommandNotSupported",
        "operationTime" : Timestamp(1680621019, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1680621019, 1),
                "signature" : {
                        "hash" : BinData(0,"X/3G4bRhzIt7SYVaA4JNN++nCEI="),
                        "keyId" : NumberLong("7158478101695954964")
                }
        }
}

We have tried to sharded cluster getting bellow error:

uncaught exception: Error: couldn't add user: not authorized on admin to execute command

Hi @Nino_I ,
as mentioned from documentation:

Sharded Clusters

compact only applies to mongod instances. In a sharded environment, run compact on each shard separately as a maintenance operation.

You cannot issue compact against a mongos instance.

So you need to create the correct user on each shard and than you can apply that operation.

BR

1 Like