Importing a JSON file in an mongo container using authentication mechanism

前提是你 提交于 2020-05-16 06:18:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!