eHayik
eHayik

Reputation: 3262

Authentication failed connecting to MongoDB running in a local docker cointainer from Spring Boot application

I cannot connect to a mongodb instance running in a local docker container from my spring boot application. When the application tries to connect to the database the error below is thrown:

2022-04-03 22:45:57.243 ERROR 14559 --- [nio-5000-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.data.mongodb.UncategorizedMongoDbException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-256, userName='root', source='admin', password=<hidden>, mechanismProperties=<hidden>}; nested exception is com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-256, userName='root', source='admin', password=<hidden>, mechanismProperties=<hidden>}] with root cause

com.mongodb.MongoCommandException: Command failed with error 18 (AuthenticationFailed): 'Authentication failed.' on server localhost:27017. The full response is {"ok": 0.0, "errmsg": "Authentication failed.", "code": 18, "codeName": "AuthenticationFailed"}
    at com.mongodb.internal.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:198) ~[mongodb-driver-core-4.4.0.jar:na]
    at com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:418) ~[mongodb-driver-core-4.4.0.jar:na]

This is the docker-compose mongodb service definition:

  mongodb:
    image: mongo
    container_name: mongodb
    restart: always
    ports:
      - "27017:27017"
    volumes:
      - "mongo_data:/tmp/techbank/mongo"
    environment:
      - MONGO_INITDB_ROOT_USERNAME=root
      - MONGO_INITDB_ROOT_PASSWORD=Pa$$w0rd

  mongo-express:
    image: mongo-express
    container_name: mongo-express
    restart: always # fixes MongoNetworkError when mongodb is not ready when mongo-express starts
    ports:
      - "8081:8081"
    environment:
      - ME_CONFIG_MONGODB_ADMINUSERNAME=root
      - ME_CONFIG_MONGODB_ADMINPASSWORD=Pa$$w0rd
      - ME_CONFIG_MONGODB_SERVER=mongodb

I can access the database from mongo-express client, when all services are up and running.

These are the spring data mongodb properties:

spring:
  data:
    mongodb:
      host: localhost
      port: 27017
      database: bankAccount
      username: root
      password: Pa$$w0rd
      authentication-database: admin

I also tried:

spring:
  data:
    mongodb:
       uri: "mongodb://root:Pa$$w0rd@localhost:27017/bankAccount=true&authSource=admin&authMechanism=SCRAM-SHA-1"

I have found similar questions on stackoverflow, but the suggested solutions does not work in my case.

You may find instructions to run the application locally here techbank-build-github

If you need more details leave your comments below.

Upvotes: 0

Views: 7744

Answers (2)

eHayik
eHayik

Reputation: 3262

Thanks to @Mark Bramnik answer I realized that I cannot authenticate to the mongodb container using root:Pa$$w0rd credentials.

I created a file named init-mongo.js:

db.createUser(
    {
        user: "storeAdmin",
        pwd: "storeAdmin2022",
        roles: [
            {
                role: "readWrite",
                db: "bankAccount"
            }
        ]
    }
)

Added ./init-mongo.js:/docker-entrypoint-initdb.d/init-mongo-js:ro under services:mongodb:volumes in the docker-compose.yml in order to copy init-mongo.js to /docker-entrypoint-initdb.d/ as a read only file.

/docker-entrypoint-initdb.d is a folder that is already created inside the mongodb container used for initiating the database.

Then updated the spring data mongodb properties accordingly:

spring:
  data:
    mongodb:
      host: localhost
      port: 27017
      database: bankAccount
      username: storeAdmin
      password: storeAdmin2022

Now it works like a charm

Upvotes: 3

Mark Bramnik
Mark Bramnik

Reputation: 42541

Could it be a network issue rather than authentication?

Spring boot application specifies a "localhost" here, so I assume it doesn't run with docker-compose...

I suggest trying the following:

  1. Try to connect with some kind of client (robomongo, mongod whatever) and make sure it's accessible at all
  2. Try to bind mongo itself to all the interfaces by adding an option of --bind_ip_all as the official mongo documentation suggests

Upvotes: 0

Related Questions