我遇到从nodejs / express app连接到docker容器中运行的mongodb实例的问题。 nodejs应用程序在我的本地主机Mac OS X上运行。我使用Docker Toolbox在我的Mac上运行docker,而不是boot2docker。
我已通过-p arg启用了端口转发到docker run。
docker run -p 27017:27017 --name test -d mongo:2.6
60b7f3cf658bc75ceca2eae074e5f694ec622f7487b6a2f36fa02edd4af68357
我可以看到它正在运行。
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
60b7f3cf658b mongo:2.6 "/entrypoint.sh mongo" 9 minutes ago Up 9 minutes 0.0.0.0:27017->27017/tcp test
如果我运行docker logs,我发现它正在侦听端口27017。
docker logs test
mongod --help for help and startup options
2015-10-03T15:42:25.833+0000 [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=60b7f3cf658b
2015-10-03T15:42:25.833+0000 [initandlisten] db version v2.6.11
2015-10-03T15:42:25.833+0000 [initandlisten] git version: d00c1735675c457f75a12d530bee85421f0c5548
2015-10-03T15:42:25.833+0000 [initandlisten] build info: Linux build4.ny.cbi.10gen.cc 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49
2015-10-03T15:42:25.833+0000 [initandlisten] allocator: tcmalloc
2015-10-03T15:42:25.833+0000 [initandlisten] options: {}
2015-10-03T15:42:25.835+0000 [initandlisten] journal dir=/data/db/journal
2015-10-03T15:42:25.835+0000 [initandlisten] recover : no journal files present, no recovery needed
2015-10-03T15:42:25.860+0000 [initandlisten] allocating new ns file /data/db/local.ns, filling with zeroes...
2015-10-03T15:42:25.891+0000 [FileAllocator] allocating new datafile /data/db/local.0, filling with zeroes...
2015-10-03T15:42:25.891+0000 [FileAllocator] creating directory /data/db/_tmp
2015-10-03T15:42:25.892+0000 [FileAllocator] done allocating datafile /data/db/local.0, size: 64MB, took 0 secs
2015-10-03T15:42:25.893+0000 [initandlisten] build index on: local.startup_log properties: { v: 1, key: { _id: 1 }, name: "_id_", ns: "local.startup_log" }
2015-10-03T15:42:25.893+0000 [initandlisten] added index to empty collection
2015-10-03T15:42:25.894+0000 [initandlisten] waiting for connections on port 27017
当我尝试从express连接时,出现以下错误。
my-app/node_modules/mongodb/lib/server.js:228
process.nextTick(function() { throw err; })
^
Error: connect ECONNREFUSED
at exports._errnoException (util.js:746:11)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1010:19)
现在,stackoverflow上还有其他帖子引用了这个问题。他们都说“通过-p arg启用端口转发”,我这样做了。但它仍然不会让我联系。
我正在使用正确的端口和主机虚拟机IP - 我可以通过常规的mongo shell工具从我的Mac连接到mongodb容器。
还有一些奇怪的事情......
2015-10-03T15:53:25.938+0000 [clientcursormon] connections:0
2015-10-03T15:54:08.843+0000 [initandlisten] connection accepted from 192.168.99.1:54804 #1 (1 connection now open)
2015-10-03T15:54:08.855+0000 [conn1] end connection 192.168.99.1:54804 (0 connections now open)
另见:
答案 0 :(得分:1)
我会回答我自己的问题,但不接受它。所以事实证明错误是由另一部分埋藏的nodejs代码引起的,该代码试图连接到mongodb://127.0.0.1:27017。这个mongodb实例不存在,所以它抛出错误。
我集中了mongodb配置,一切都很好。