Mongo 客户端使用命令行参数进行连接,但等效 URI 失败

Mongo 客户端使用命令行参数进行连接,但等效 URI 失败

当我使用此脚本创建 MongoDB 数据库时:

#!/bin/bash

javascript='
db.dropUser("admin");
db.createUser({
    user: "admin",
    pwd: "password",
    roles: [
        {role: "userAdminAnyDatabase", db: "admin"}
    ]
});
db.getSiblingDB("example").empty.save({"empty": 0});
'

docker-compose exec mongodb mongo --eval "${javascript}"

mongo我可以使用shell 和命令行参数成功登录:

$ mongo --host docker-machine -u admin -p password
MongoDB shell version v3.4.10
connecting to: mongodb://docker-machine:27017/
MongoDB server version: 3.4.10

但是,对于看似等效的连接 URI,连接却失败:

$ mongo 'mongodb://admin:password@docker-machine/'
MongoDB shell version v3.4.10
connecting to: mongodb://admin:password@docker-machine/
2017-12-06T21:17:47.297-0700 E QUERY    [thread1] Error: Authentication failed. :
connect@src/mongo/shell/mongo.js:237:13
@(connect):1:6
exception: connect failed

MongoDB日志显示:

2017-12-07T04:16:27.703+0000 I ACCESS   [conn100]
    SCRAM-SHA-1 authentication failed for admin on admin from client
    10.211.55.2:52496 ; AuthenticationFailed: SCRAM-SHA-1 authentication
    failed, storedKey mismatch

这是怎么回事?

答案1

您需要连接到 MongoDBadmin数据库:

docker-compose exec mongodb mongo admin --eval "${javascript}"
#                                 ^^^^^ add this

相关内容