Getting "MongoServerError: Authentication failed" when connecting to MongoDB in Docker container - mongodb

Here is my docker-compose file:
version: '3' services: mongo:
image: mongo
container_name: mongo
restart: always
environment:
- MONGO_INITDB_ROOT_USERNAME: admin
- MONGO_INITDB_ROOT_PASSWORD: root
ports:
- 27017:27017
volumes:
- ./mongo-data:/data/db
command: --wiredTigerCacheSizeGB 1.5
I try to connect to Mongo container but constantly receive the same error:
MongoServerError: Authentication failed
Here are 2 variants of connection strings I've used, neither worked:
mongodb://admin:root#localhost:27017/admin
mongodb://admin:root#localhost:27017/admin?authSource=admin
What can I try next?

Related

MongoDb : Cannot connect database on Docker container

I use the following docker-compose.yml to build a MongoDb database in a Docker container:
version: "3"
services:
mongodb:
image: mongo:latest
container_name: mongodb
volumes:
- mongo_data:/data/db
ports:
- "27017:27017"
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: pass
MONGODB_USERNAME: user
MONGODB_PASSWORD: pass
MONGO_INITDB_DATABASE: social-network-db
volumes:
mongo_data:
Then I restarted the container and try to connect database via MongoDb Compass app by using these values:
Connection parameters
So, what is wrong and why I cannot connect to my MongoDb database? If you have some suggestion regarding to adocker-compose.yml parameter or creating used while building container, please inform me.

MongoDB authentication fails in Docker

My docker-compose.yml
mongo:
image: mongo
volumes:
- ./mongo:/data/db
ports:
- 27017:27017
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: pass
mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: pass
ME_CONFIG_MONGODB_URL: mongodb://root:pass#mongo:27017/
When I run docker-compose up it logs the following errors
for the mongo service:
UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [mongo:27017] on first connect [MongoError: Authentication failed.
and for the mongo-express service:
"error":"UserNotFound: Could not find user "root" for db "admin"
I tried adding ?authSource=admin to the ME_CONFIG_MONGODB_URL but the error remains.

Can't connect to MongoDB inside docker container

I'm trying to connect to a mongodb container inside docker but I'm getting this error:
getaddrinfo ENOTFOUND mongo.ks.local
The database works just fine when I access it from docker but I'm not able to access it from MongoDBCompass
This is my docker-compose.yml file:
version: '3.8'
services:
mongo:
image: mongo
container_name: mongo.ks.local
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: <username>
MONGO_INITDB_ROOT_PASSWORD: <password>
ports:
- 27017:27017
volumes:
- ks_mongodb:/data/db
I'm quite new to docker and will appreciate any help I can get
You need the ip address of the container:
try:
docker inspect <CONTAINER NAME>
And search for the ip address. Then use the ip address to connect to mongodb
I have tried connecting to the container using mongo compass. It failed but when I removed the line of mounting the volume and tried again it connects to the database.
Then tried to mount the volume like below it worked. I have changed the username and password and specified the INITDB env variable.
version: "3.8"
services:
mongo:
image: mongo:latest
container_name: mongo.ks.local
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: 1234
MONGO_INITDB_DATABASE: admin
ports:
- 27017:27017
volumes:
- /data/db

connect EHOSTUNREACH 172.24.0.2:27017 docker-compose

I am currently unable to connect to mongo when I run docker-compose, I keep on getting the error below
connect EHOSTUNREACH 172.25.0.2:27017
Here is my docker-compose.yml file
version: "3.4"
services:
app:
container_name: checki
restart: always
build:
context: .
network: host
ports:
- "3000:9000"
links:
- mongo
mongo:
container_name: mongo
image: mongo
volumes:
- ./data:/data/db
ports:
- "27018:27017"
and my db connection string is like this
"mongodb://mongo:27017/checki"
Please assist

How to use Robo3T to connect mongodb server running as a docker container

I have a mongodb running as a docker container which created by docker-compose.
Here is my docker-compose.yml:
verion:'3.7'
services:
mongo_env:
image: mongo:4.2.1-bionic
ports:
- "27017:27017"
volumes:
- $PWD/DBVOL/mongo/data:/data/db:rw
environment:
MONGO_INITDB_ROOT_USERNAME: <rootuser>
MONGO_INITDB_ROOT_PASSWORD: <mypassword>
MONGO_INITDB_DATABASE: <mydatabase>
I want to connect mongodb from outside using Robo 3T,But It always tell me that 'Failed to load database'.Here is my Robo 3T config:
Please Help me It's emergency!
Are your sure it started successfull? Your Docker Compose has a typo in "version:3.7".
correct version:
version: '3.7'
services:
mongo_env:
image: mongo:4.2.1-bionic
ports:
- "27017:27017"
volumes:
- $PWD/DBVOL/mongo/data:/data/db:rw
environment:
MONGO_INITDB_ROOT_USERNAME: <rootuser>
MONGO_INITDB_ROOT_PASSWORD: <mypassword>
MONGO_INITDB_DATABASE: <mydatabase>
Please check after starting if the container is running with
docker ps