AuthenticationFailed while trying to connect to mongo docker container

I am facing a weird problem regarding the mongo container. I have a server machine whose IP address is In that machine I am trying to deploy my MongoDB, and I want it to be accessible from other machines. I have used the following command to deploy mongo and allowed the port 27017 in the firewall.

docker run -d \
   --name mongo \
   -p 27017:27017 \
   -v /path/to/mongo:/data/db \

The mongo container is running but when I try to access it from another machine either via mongosh or via a nodejs process (mongoose library) it is not accessible.

I am using the following command to access mongodb deployed in this machine from other machine:

mongosh --host --port 27017 --username root --password password --authenticationDatabase admin

This shows authentication error every time and in the server machine logs. It says it is not getting any user named root in the admin database.

In the nodejs , I am using the following urls to connect to the mongodb:


Again, it fails to connect to the mongodb.

However, if I do not use any kind of authentication to deploy mongo db, it can be accessed both via mongosh and nodejs.

For example if I use this:

docker run -d \
   --name mongo \
   -p 27017:27017 \
   -v /path/to/mongo:/data/db \

It is accessible both via mongosh and nodejs library from other machines.

Can anyone tell me what am I doing wrong here?

If at any point the password for root changed on this database the provided credentials on the command line will no longer apply. Could that be the case for this database ?

The password was never changed. But i did try the db without authentication at first. Then stopped and removed the container and created a new one with root password. Is that causing the issue? If yes, how should I fix it?

Yes that will be creating the issue.

Start the database without authentication enabled. Connect and create the user:

 roles: ["root"]

Then start the database with authentication enabled.


It worked. Thanks a lot.

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