0

registry, gateway and uaa running on a computer, with one microservice running on another computer, the microservice use mongodb to save data, the mongodb is in the same computer with the microservice.

I follow this post to create user when using "docker-compose -f app.yml up", but an error "the PWD variable is not set", however the PWD is defined in the current shell.

After I work around by subsitute $PWD with ~/docker/, another error occurs:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongobee' defined in class path resource [com/james/shortvideo/config/DatabaseConfiguration.class]: Invocation of init method failed; nested exception is com.mongodb.MongoQueryException: Query failed with error code 13 and error message 'there are no users authenticated' on server hdshortvideo-mongodb:27017

The application-prod.yml is as below (mongo related):

spring:
    ...
    data:
        mongodb:
            uri: mongodb://james:xxxx@localhost:27017
            database: shortvideo

When I debug locally, I manually setup a mongodb with non-root user created, and the microservice can registered in the registry successfully, so I suspect the cause is mongodb initialization related. Any advice?

mongodb.yml is as below:

version: '2'
services:
    hdshortvideo-mongodb:
        image: mongo:3.6.3
        environment:
            - MONGO_INITDB_ROOT_USERNAME=root
            - MONGO_INITDB_ROOT_PASSWORD=password
        ports:
            - "27017:27017"
        volumes:
            - ~/volumes/HDShortVideo/mongodb/:/data/db/
            - ~/docker/mongo-entrypoint/:/docker-entrypoint-initdb.d/
        command: mongod

and the user.sh in the ~/docker/mongo-entrypoint/ is as below:

#!/usr/bin/env bash
echo "Creating mong users..."
mongo --authenticationDatabase admin --host localhost -u root -p xxxxx --eval "db.createUser({user: 'james', pwd: 'xxxxx', roles: [{role: 'readWrite', db: 'shortvideo'}]}); db.createUser({user: 'admin', pwd: 'xxxx', roles: [{role: 'userAdminAnyDatabase', db: 'admin'}]});"
echo "Mongo users created."

================================================== the environment for hdshortvideo-app (from app.yml) is as below:

mongorelated:

 - SPRING_DATA_MONGODB_URI=mongodb://hdshortvideo-mongodb:27017
 - SPRING_DATA_MONGODB_DATABASE=shortvideo
James Hao
  • 765
  • 2
  • 11
  • 40

1 Answers1

0

Your user script define user in admin database, ,so you need to authenticate against this admin database to connect.

To do this, just add the authentication-database property in your yml file :

spring:
    ...
    data:
        mongodb:
            uri: mongodb://james:xxxx@localhost:27017
            database: shortvideo
            authentication-database: admin
matthPen
  • 4,253
  • 1
  • 16
  • 16
  • Thanks, matthPen, I added the "authentication-database: admin" but the same error still exists, I suspect the user.sh not executed at all, because I checked the console output when mongodb container was started and could not find anything about creatUser. – James Hao Nov 09 '18 at 10:30
  • maybe the environment for hdshortvideo-app wrong, I have updated the post – James Hao Nov 09 '18 at 11:26