2

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
  • See this https://stackoverflow.com/questions/63754742/authentication-failure-while-trying-to-save-to-mongodb/63755470#63755470 perhaps user is create in database `test` – Wernfried Domscheit Feb 04 '23 at 14:02
  • It looks like your YAML here is corrupted - `version: '3' services: mongo:` should be on three lines, not one. If this is what your Compose file contains then I imagine it would not even start up. Please repair it here (and there) as necessary. – halfer Feb 06 '23 at 10:13
  • Where are you connecting from - the Mongo container, another container, or the Docker host? Please also add that information to your post. – halfer Feb 06 '23 at 10:15
  • Have you looked at your Docker logs to see if Mongo has started correctly? – halfer Feb 06 '23 at 10:16

0 Answers0