问题
I am using a mongo container and want to insert the records in a JSON file to the mongo container
Here is the docker-compose.yml
version: '3'
services:
mongodb:
image: mongo
ports:
- 27017:27017
environment:
MONGO_INITDB_ROOT_USERNAME: "dev"
MONGO_INITDB_ROOT_PASSWORD: "pass"
mongo_seed:
build: ./mongo-seed
depends_on:
- mongodb
mongo-seed is another docker container which uses mongoimport to load the data in the database
FROM mongo
COPY services.json /services.json
CMD mongoimport --host mongodb --username dev --password pass --authenticationDatabase cloud --db cloud --collection services --type json --upsertFields number,type --file /services.json
but while running it throws error
SASL SCRAM-SHA-1 authentication failed for dev on cloud from client 192.168.229.9:34598 ; UserNotFound: Could not find user "dev" for db "cloud"
mongodb_1 | 2020-03-12T13:46:35.293+0000 I NETWORK [conn2] end connection 192.168.229.9:34598 (1 connection now open)
mongo_seed_1 | 2020-03-12T13:46:35.293+0000 error connecting to host: could not connect to server: connection() : auth error: sasl conversation error: unable to authenticate using mechanism "SCRAM-SHA-1": (AuthenticationFailed) Authentication failed
How should I specify the username and password (authentication) while inserting the data using mongoimport.
I also tried specifying the MONGO_INITDB_DATABASE: "cloud"
env variable in compose file, even that did not work.
回答1:
Sorry in advance that I didn't test this locally or anything, but I believe what you're missing is the configuration to create your non-default database.
You're trying to connect to "cloud" so try adding the following environment to the "mongodb" container:
MONGO_INITDB_DATABASE=cloud
Edit:
Also, I would be skeptical of the value of --authenticationDatabase
. I haven't had to use that in the past.
IDK what the default is, but you might try removing, assuming the default does the right thing.
I found a different issue where the accepted answer uses a different value for --authenticationDatabase
than for --db
. Hopefully that is helpful too. https://stackoverflow.com/a/58067928/317951
来源:https://stackoverflow.com/questions/60658838/importing-a-json-file-in-an-mongo-container-using-authentication-mechanism