无法连接到已配置的mongo实例(连接被拒绝)

时间:2019-04-18 05:07:03

标签: mongodb docker

基于本指南:

https://docs.opsmanager.mongodb.com/current/tutorial/install-simple-test-deployment/

我正在安装MongoDB和MongoDB Manager。我为每个应用程序创建了一个docker映像,并在同一虚拟网络上启动它们:

docker network create --driver bridge mongo-network

具有:

MongoDB:

 docker run -ti -d --network mongo-network -p 27017:27017 --name mongodb-container mongodb-image
 docker exec -ti -u mongod mongodb-container "mongod --port 27017 --dbpath /data/appdb --logpath /data/appdb/mongodb.log --wiredTigerCacheSizeGB 1 --fork"

并验证其是否已启动并运行:

$  docker exec -ti -u mongod mongodb-container tail -f /data/appdb/mongodb.log

2019-04-21T15:26:05.208+0000 I CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
2019-04-21T15:26:05.208+0000 I CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server. 
2019-04-21T15:26:05.208+0000 I CONTROL  [initandlisten] **          Start the server with --bind_ip <address> to specify which IP 
2019-04-21T15:26:05.208+0000 I CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
2019-04-21T15:26:05.208+0000 I CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
2019-04-21T15:26:05.208+0000 I CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.
2019-04-18T06:23:35.268+0000 I CONTROL  [initandlisten] 
2019-04-18T06:23:35.269+0000 I STORAGE  [initandlisten] createCollection: admin.system.version with provided UUID: c0736278-72ec-4dfc-893c-8105eefa0ba8
2019-04-18T06:23:35.320+0000 I COMMAND  [initandlisten] setting featureCompatibilityVersion to 4.0
2019-04-18T06:23:35.341+0000 I STORAGE  [initandlisten] createCollection: local.startup_log with generated UUID: 397c17a3-3c5e-4605-b4dc-8a936dd9e40e
2019-04-18T06:23:35.394+0000 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/data/appdb/diagnostic.data'
2019-04-18T06:23:35.396+0000 I NETWORK  [initandlisten] waiting for connections on port 27017
2019-04-18T06:23:35.397+0000 I STORAGE  [LogicalSessionCacheRefresh] createCollection: config.system.sessions with generated UUID: ac7bdb6e-4a60-430f-b1a4-34b09012e6da
2019-04-18T06:23:35.475+0000 I INDEX    [LogicalSessionCacheRefresh] build index on: config.system.sessions properties: { v: 2, key: { lastUse: 1 }, name: "lsidTTLIndex", ns: "config.system.sessions", expireAfterSeconds: 1800 }
2019-04-18T06:23:35.475+0000 I INDEX    [LogicalSessionCacheRefresh]     building index using bulk method; build may temporarily use up to 500 megabytes of RAM
2019-04-18T06:23:35.477+0000 I INDEX    [LogicalSessionCacheRefresh] build index done.  scanned 0 total records. 0 secs

MongoDB Manager:

  docker run -ti -d --network mongo-network -p 8080:8080 --name mongodb-manager-container mongodb-manager-image  
  docker exec -ti -u root mongodb-manager-container "/opt/mongodb/mms/bin/mongodb-mms start"

以下错误消息:

Generating new Ops Manager private key...
Starting pre-flight checks
    Failure to connect to configured mongo instance:Config{
       loadBalance=false,
       encryptedCredentials=false,
       ssl='false',
       dbNames='   [
          mmsdb,
          mmsdbprovisionlog,
          mmsdbautomation,
          mmsdbserverlog,
          mmsdbpings,
          mmsdbprofile,
          mmsdbrrd,
          mmsdbconfig,
          mmsdblogcollection,
          mmsdbjobs,
          mmsdbagentlog,
          mmsdbbilling,
          backuplogs,
          automationcore,
          monitoringstatus,
          mmsdbautomationlog,
          automationstatus,
          ndsstatus,
          cloudconf,
          backupdb,
          mmsdbprovisioning,
          mmsdbqueues
       ]   ',
       uri=mongodb://mongodb-container:27017/?maxPoolSize=150
    }Error:Timed out after 30000 ms while waiting to connect. Client view of cluster state is{
       type=UNKNOWN,
       servers=   [
          {
             address=mongodb-container:27017,
             type=UNKNOWN,
             state=CONNECTING,
             exception=         {
                com.mongodb.MongoSocketOpenException:Exception opening socket
             },
             caused by         {
                java.net.ConnectException:Connection refused (Connection refused)
             }
          }
       ]

对于mongodb-根据以下建议-我现在正在使用:

/etc/mongod.conf

# network interfaces
net:
  port: 27017
  #bindIp: 127.0.0.1  # Enter 0.0.0.0,:: to bind to all IPv4 and IPv6 addresses or, alternatively, use the net.bindIpAll setting.
  bindIp: 0.0.0.0,::

对于MongoDB经理,我在以下位置指定mongodb容器的名称:

/opt/mongodb/mms/conf/conf-mms.properties

#mongo.mongoUri=mongodb://127.0.0.1:27017/?maxPoolSize=150
#mongo.mongoUri=mongodb://0.0.0.0:27017/?maxPoolSize=150
mongo.mongoUri=mongodb://mongodb-container:27017/?maxPoolSize=150

我已验证可以通过以下方式从mongodb-container进行 ping mongodb-manager-container

docker exec -it -u root mongodb-manager-container bash
[root@e23a34bf2161 /]# ping mongodb-container
PING mongodb-container (172.18.0.2) 56(84) bytes of data.
64 bytes from mongodb-container.mongo-network (172.18.0.2): icmp_seq=1 ttl=64 time=0.077 ms
64 bytes from mongodb-container.mongo-network (172.18.0.2): icmp_seq=2 ttl=64 time=0.059 ms
64 bytes from mongodb-container.mongo-network (172.18.0.2): icmp_seq=3 ttl=64 time=0.052 ms
^C
--- mongodb-container ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2042ms
rtt min/avg/max/mdev = 0.052/0.062/0.077/0.013 ms
[root@e23a34bf2161 /]# 

我想念什么?

编辑:

根据我现在尝试的以下建议:

docker network create --driver bridge mongo-network
docker run -ti -d --network mongo-network -p 27017:27017 --name mongodb-container mongodb-image
# Copy modified version of mongod.conf file to container before starting mongodb    
docker cp mongod.conf mongodb-container:/etc/mongod.conf

docker exec -ti -u mongod mongodb-container "mongod --port 27017 --dbpath /data/appdb --logpath /data/appdb/mongodb.log --wiredTigerCacheSizeGB 1 --fork"
docker run -it --rm --net container:mongodb-container nicolaka/netshoot ss -lnt

哪个给:

State    Recv-Q    Send-Q        Local Address:Port        Peer Address:Port    
LISTEN   0         128              127.0.0.11:36001            0.0.0.0:*       
LISTEN   0         128               127.0.0.1:27017            0.0.0.0:*   

不确定这是否是预期的/良好的输出,以及为什么我需要从nicolaka/netshoot图片中旋转容器...

编辑2:

如以下建议,如果我在命令行上通过--bind_ip_all来启动mongod,那么它将起作用:

docker exec -ti -u mongod mongodb-container "mongod --bind_ip_all --port 27017 --dbpath /data/appdb --logpath /data/appdb/mongodb.log --wiredTigerCacheSizeGB 1 --fork"

因此,当作为docker容器运行时,它似乎完全忽略了 /etc/mongod.conf 文件,您需要在docker exec命令中指定所有选项,而不是:-(

3 个答案:

答案 0 :(得分:1)

容器名称上的DNS将解析为容器ip。要以该名称连接mongo,即使从容器内部,也需要在所有接口上监听mongo:

# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0,::

答案 1 :(得分:1)

问题在于它忽略了 /etc/mongod.conf 文件中的配置。谷歌搜索后,例如:

https://jira.mongodb.org/browse/SERVER-36572

我发现您需要将--config参数传递给mongod e。使其读取mongod.conf文件,例如:

mongod --config /etc/mongod.conf

以及docker:

docker exec -ti -u mongod mongodb-container "mongod --config /etc/mongod.conf"

完成上述操作后,我现在可以在/etc/mongod.conf中使用以下配置在所有接口上进行监听:

# network interfaces
net:
  port: 27017
  bindIp: 0.0.0.0

答案 2 :(得分:0)

您正在运行两个单独的容器,并希望它们通过localhost相互通信吗?那永远都行不通。您必须在第二个docker run命令中添加“ --link mongodb-container:mongo”,然后在管理器容器中使用地址mongodb:// mongo:27017。

相关问题