我正在使用mongo容器,并想将JSON文件中的记录插入到mongo容器中
这是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是另一个docker容器,它使用mongoimport将数据加载到数据库中
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
但是在运行时会引发错误
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插入数据时,应如何指定用户名和密码(身份验证)。
我也尝试在撰写文件中指定
MONGO_INITDB_DATABASE: "cloud"
env变量,即使这样也不起作用。
最佳答案
预先抱歉,我没有在本地进行任何测试,但我相信您所缺少的是用于创建非默认数据库的配置。
您正在尝试连接到“云”,因此请尝试将以下环境添加到“mongodb”容器中:
MONGO_INITDB_DATABASE=cloud
编辑:
另外,我会对
--authenticationDatabase
的值表示怀疑。过去我不必使用它。IDK默认值是什么,但是您可以尝试删除它,前提是默认值做对了。
我发现了一个不同的问题,即接受的答案对
--authenticationDatabase
使用的值与对--db
使用不同的值。希望这也有帮助。 https://stackoverflow.com/a/58067928/317951
关于mongodb - 使用身份验证机制在mongo容器中导入JSON文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60658838/