"Unauthorized" error in a docker container

Doing a docker exec into the container but can’t list the DBs. Tried everything but can’t get it work. Any help?

2023-02-02T18:19:58.094+0000 E QUERY    [thread1] Error: listDatabases failed:{
        "operationTime" : Timestamp(1675361994, 1),
        "ok" : 0,
        "errmsg" : "there are no users authenticated",
        "code" : 13,
        "codeName" : "Unauthorized",
        "$clusterTime" : {
                "clusterTime" : Timestamp(1675361994, 1),
                "signature" : {
                        "hash" : BinData(0,"3m4e6DdOpo7rTKlGzJDj8+MnlMw="),
                        "keyId" : NumberLong("7188273050037518337")
} :

I am guessing your config file enables authentication, but you forgot to add, at least, an admin user. when auth is enabled you can only access from localhost (inside the container) until you create the first user.

1 Like

Hi @Dev_Engine,
As mentioned by the error, you are not authorized to do this operation!
So you need to authenticate before with a user which have the correct privileges. For example:

mongosh --authenticationDatabase admin -u admin -p ***

And then you can list database!

I hope it is useful.


1 Like

yes, I’m trying to access inside the container (after doing docker exec -it…) and I continue to see this error.

@Fabio_Ramohitaj , I’ve already tried this but same error.

alright then, second round: after the first user is created, you cannot just do whatever you want without proper credentials and assigned roles :slight_smile:

now, to understand how you ended up with this error, please share how you enabled auth, how you added users, which user is logged in to which database, and how you logged in.

  • either give detailed lines of commands you have used (minus sensitive information such as password),
  • or create a new container to reproduce the error with 1 or 2 users and minimal setup, then share those steps.

Mongodb is running in a k8 container. So I figured that there is no way to do it within the container itself. I had to recreate the container or go to etcd to get the settings.

Thanks for the responses, truly appreciate!

the equivalent command for k8 is “kubectl exec”. but the important part is that many things are still in your configuration files (pod,deployment,service etc). so also check them. keep in mind, if you are doing too many things inside the container/pod manually, then you are not following the repetable installation logic.

by the way, did that recration solve your problem?