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