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

时间:2021-12-15 13:00:47

If I set up my container without auth eg)

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

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

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

I can connect to my mongoDB from a remote machine eg)

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

mongo [serverIP]

However, my issue is when using the --auth flag

但是,我的问题是使用--auth标志时

Following the documentation:

以下文档:

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

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

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

results in connections being refused:

导致连接被拒绝:

$ 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

I double-checked and the port looks to be open on the server:

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

$ 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 is running on a debian Digital Ocean droplet)

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

1 个解决方案

#1


0  

You need to specify the --authenticationDatabase flag

您需要指定--authenticationDatabase标志

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

see the tutorial

看教程

#1


0  

You need to specify the --authenticationDatabase flag

您需要指定--authenticationDatabase标志

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

see the tutorial

看教程