I have been trying to setup password to mongodb
My docker-compose file is
version: "3"
services:
mongo:
image: mongo
ports:
- "27016:27017"
volumes:
- ./data:/data/db
environment:
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=password
- MONG_INITDB_DATABASE=example
Now after running the docker container, when I try after going inside the container
mongo mongodb://root:password@localhost:27017
It connects successfully,
MongoDB shell version v4.2.7 connecting to: mongodb://localhost:27017/?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("71ec853d-3a05-4367-aa9f-d0a69217fb7d") } MongoDB server version: 4.2.7 Server has startup warnings: 2020-06-16T17:43:51.837+0000 I STORAGE [initandlisten] 2020-06-16T17:43:51.837+0000 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine 2020-06-16T17:43:51.837+0000 I STORAGE [initandlisten] ** See
but when I try
mongo mongodb://root:password@localhost:27017/example
authentication fails. MongoDB shell version v4.2.7 connecting to: mongodb://localhost:27017/example?compressors=disabled&gssapiServiceName=mongodb 2020-06-16T17:51:08.226+0000 E QUERY [js] Error: Authentication failed. : connect@src/mongo/shell/mongo.js:341:17
Its the same when I try to access from outside using a node app. (Running node app too using docker-compose)
mongo mongodb://root:password@mongo:27017/example