当auth启用时,MongoDB Docker容器拒绝连接

如果我没有auth设置我的容器例如)

docker run --name some-mongo -d -p 27017:27017 mongo

我可以从远程机器连接到我的mongoDB)

mongo [serverIP]

不过,我的问题是使用--auth标志

以下文件:

docker run --name some-mongo -d -p 27017:27017 mongo --auth

 > db.createUser({ user: 'foo', pwd: 'bar', roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] }) 

导致连接被拒绝:

 $ mongo -u foo -p bar [serverIP] MongoDB shell version v3.4.9 connecting to: [serverIP] 2017-12-09T14:01:41.114-0600 W NETWORK [thread1] Failed to connect to [serverIP]:27017, in(checking socket for error after poll), reason: Connection refused 2017-12-09T14:01:41.115-0600 E QUERY [thread1] Error: couldn't connect to server [serverIP]:27017, connection attempt failed : connect@src/mongo/shell/mongo.js:237:13 @(connect):1:6 exception: connect failed 

我仔细检查,端口看起来在服务器上打开:

 $ netstat -tuplen (Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.) Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State User Inode PID/Program name ... tcp6 0 0 :::27017 :::* LISTEN 0 53238875 - ... 

(Docker运行在debian数字海洋液滴上)

您需要指定--authenticationDatabase标志

 mongo --port 27017 -u "myUserAdmin" -p "abc123" --authenticationDatabase "admin" 

看教程