问题描述
我正在使用mongo容器,并希望将JSON文件中的记录插入到mongo容器中
I am using a mongo container and want to insert the records in a JSON file to the mongo container
这是docker-compose.yml
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是另一个使用mongoimport将数据加载到数据库中的Docker容器
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
在使用mongoimport插入数据时,如何指定用户名和密码(身份验证).
How should I specify the username and password (authentication) while inserting the data using mongoimport.
我还尝试在撰写文件中指定 MONGO_INITDB_DATABASE: "cloud"
env变量,即使那没有用.
I also tried specifying the MONGO_INITDB_DATABASE: "cloud"
env variable in compose file, even that did not work.
推荐答案
对不起,我没有在本地进行任何测试,但我相信您缺少的是用于创建非默认数据库的配置.
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.
您正尝试连接到云",因此请尝试将以下环境添加到"mongodb"容器中:
You're trying to connect to "cloud" so try adding the following environment to the "mongodb" container:
MONGO_INITDB_DATABASE=cloud
编辑:
此外,我会对--authenticationDatabase
的值表示怀疑.过去我不必使用它.
Also, I would be skeptical of the value of --authenticationDatabase
. I haven't had to use that in the past.
IDK默认值是什么,但是您可以尝试删除它,前提是默认值做对了.
IDK what the default is, but you might try removing, assuming the default does the right thing.
我发现了一个不同的问题,即接受的答案对--authenticationDatabase
使用的值与对--db
使用不同的值.希望这也是有帮助的. https://stackoverflow.com/a/58067928/317951
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
这篇关于使用身份验证机制在mongo容器中导入JSON文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!