MongoDB在docker-compose环境中通过mongoose与nodeJs连接

时间:2018-01-21 07:09:51

标签: node.js mongodb mongoose docker-compose

我在连接从我的nodejs服务器容器的单独容器中运行的MongoDB时遇到了奇怪的问题,它会显示出来 尝试连接MongoDB时出现以下错误

{ MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]
todo-node-server_1     |     at Pool.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/topologies/server.js:503:11)
todo-node-server_1     |     at emitOne (events.js:116:13)
todo-node-server_1     |     at Pool.emit (events.js:211:7)
todo-node-server_1     |     at Connection.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/connection/pool.js:326:12)
todo-node-server_1     |     at Object.onceWrapper (events.js:317:30)
todo-node-server_1     |     at emitTwo (events.js:126:13)
todo-node-server_1     |     at Connection.emit (events.js:214:7)
todo-node-server_1     |     at Socket.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/connection/connection.js:245:50)
todo-node-server_1     |     at Object.onceWrapper (events.js:315:30)
todo-node-server_1     |     at emitOne (events.js:116:13)
todo-node-server_1     |     at Socket.emit (events.js:211:7)
todo-node-server_1     |     at emitErrorNT (internal/streams/destroy.js:64:8)
todo-node-server_1     |     at _combinedTickCallback (internal/process/next_tick.js:138:11)
todo-node-server_1     |     at process._tickCallback (internal/process/next_tick.js:180:9)
todo-node-server_1     |   name: 'MongoNetworkError',
todo-node-server_1     |   message: 'failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]' }

明确指出连接被拒绝。当我在本地运行所有服务时,代码工作正常。

以下列方式通过mongoose与MongoDB连接

const options = {
  autoIndex: true, // Don't build indexes
  reconnectTries: Number.MAX_VALUE, // Never stop trying to reconnect
  reconnectInterval: 500, // Reconnect every 500ms
  poolSize: 10, // Maintain up to 10 socket connections
  // If not connected, return errors immediately rather than waiting for reconnect
  bufferMaxEntries: 0
};
console.log(options);
mongoose.connect('mongodb://localhost:27017/tododb',options).then(
  () => { console.log("connected !!!"); },
  err => { console.log(err);  }
);

docker-compose.yaml

version : '3.3'

services:
  mongo-database:
    image: mongo
    ports:
      - "27017:27017"

  todo-node-server:
    build: server
    ports:
      - "3000:3000"
    links:
      - mongo-database   

  angular4-todoclient:
    build: todo-app
    ports:
      - "4200:4200"

完成Docker构建日志

[~/dev/aguha/docker-compose] $ docker-compose up
dockercompose_mongo-database_1 is up-to-date
dockercompose_angular4-todoclient_1 is up-to-date
Starting dockercompose_todo-node-server_1 ... 
Starting dockercompose_todo-node-server_1 ... done
Attaching to dockercompose_mongo-database_1, dockercompose_angular4-todoclient_1, dockercompose_todo-node-server_1
angular4-todoclient_1  | 
angular4-todoclient_1  | > todo-app@0.0.0 start /usr/src/app
angular4-todoclient_1  | > ng serve
angular4-todoclient_1  | 
angular4-todoclient_1  | ** NG Live Development Server is listening on localhost:4200, open your browser on http://localhost:4200/ **
mongo-database_1       | 2018-01-21T07:00:22.275+0000 I CONTROL  [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=02d79173d565
mongo-database_1       | 2018-01-21T07:00:22.275+0000 I CONTROL  [initandlisten] db version v3.6.2
mongo-database_1       | 2018-01-21T07:00:22.275+0000 I CONTROL  [initandlisten] git version: 489d177dbd0f0420a8ca04d39fd78d0a2c539420
mongo-database_1       | 2018-01-21T07:00:22.275+0000 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.1t  3 May 2016
mongo-database_1       | 2018-01-21T07:00:22.275+0000 I CONTROL  [initandlisten] allocator: tcmalloc
mongo-database_1       | 2018-01-21T07:00:22.275+0000 I CONTROL  [initandlisten] modules: none
mongo-database_1       | 2018-01-21T07:00:22.275+0000 I CONTROL  [initandlisten] build environment:
mongo-database_1       | 2018-01-21T07:00:22.275+0000 I CONTROL  [initandlisten]     distmod: debian81
mongo-database_1       | 2018-01-21T07:00:22.275+0000 I CONTROL  [initandlisten]     distarch: x86_64
mongo-database_1       | 2018-01-21T07:00:22.275+0000 I CONTROL  [initandlisten]     target_arch: x86_64
mongo-database_1       | 2018-01-21T07:00:22.275+0000 I CONTROL  [initandlisten] options: { net: { bindIpAll: true } }
mongo-database_1       | 2018-01-21T07:00:22.276+0000 I STORAGE  [initandlisten] 
mongo-database_1       | 2018-01-21T07:00:22.276+0000 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
mongo-database_1       | 2018-01-21T07:00:22.276+0000 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
mongo-database_1       | 2018-01-21T07:00:22.276+0000 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=487M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),statistics_log=(wait=0),verbose=(recovery_progress),
mongo-database_1       | 2018-01-21T07:00:22.308+0000 I CONTROL  [initandlisten] 
mongo-database_1       | 2018-01-21T07:00:22.308+0000 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
mongo-database_1       | 2018-01-21T07:00:22.308+0000 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
mongo-database_1       | 2018-01-21T07:00:22.308+0000 I CONTROL  [initandlisten] 
mongo-database_1       | 2018-01-21T07:00:22.308+0000 I STORAGE  [initandlisten] createCollection: admin.system.version with provided UUID: a336e5c8-1032-4095-8fc2-37de71d12006
mongo-database_1       | 2018-01-21T07:00:22.318+0000 I COMMAND  [initandlisten] setting featureCompatibilityVersion to 3.6
mongo-database_1       | 2018-01-21T07:00:22.321+0000 I STORAGE  [initandlisten] createCollection: local.startup_log with generated UUID: 5b3f3374-6f4e-4f63-aee1-4727eeb1c2e1
mongo-database_1       | 2018-01-21T07:00:22.328+0000 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
mongo-database_1       | 2018-01-21T07:00:22.328+0000 I NETWORK  [initandlisten] waiting for connections on port 27017
mongo-database_1       | 2018-01-21T07:00:22.898+0000 I NETWORK  [listener] connection accepted from 172.18.0.1:57658 #1 (1 connection now open)
mongo-database_1       | 2018-01-21T07:00:28.682+0000 I NETWORK  [conn1] end connection 172.18.0.1:57658 (0 connections now open)
todo-node-server_1     | 
todo-node-server_1     | > server@0.0.0 start /usr/src/app
todo-node-server_1     | > node ./bin/www
todo-node-server_1     | 
todo-node-server_1     | yes,connecting to db....
todo-node-server_1     | { autoIndex: true,
todo-node-server_1     |   reconnectTries: 1.7976931348623157e+308,
todo-node-server_1     |   reconnectInterval: 500,
todo-node-server_1     |   poolSize: 10,
todo-node-server_1     |   bufferMaxEntries: 0 }
todo-node-server_1     | done with tries
todo-node-server_1     | { MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]
todo-node-server_1     |     at Pool.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/topologies/server.js:503:11)
todo-node-server_1     |     at emitOne (events.js:116:13)
todo-node-server_1     |     at Pool.emit (events.js:211:7)
todo-node-server_1     |     at Connection.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/connection/pool.js:326:12)
todo-node-server_1     |     at Object.onceWrapper (events.js:317:30)
todo-node-server_1     |     at emitTwo (events.js:126:13)
todo-node-server_1     |     at Connection.emit (events.js:214:7)
todo-node-server_1     |     at Socket.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/connection/connection.js:245:50)
todo-node-server_1     |     at Object.onceWrapper (events.js:315:30)
todo-node-server_1     |     at emitOne (events.js:116:13)
todo-node-server_1     |     at Socket.emit (events.js:211:7)
todo-node-server_1     |     at emitErrorNT (internal/streams/destroy.js:64:8)
todo-node-server_1     |     at _combinedTickCallback (internal/process/next_tick.js:138:11)
todo-node-server_1     |     at process._tickCallback (internal/process/next_tick.js:180:9)
todo-node-server_1     |   name: 'MongoNetworkError',
todo-node-server_1     |   message: 'failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]' }
angular4-todoclient_1  | Date: 2018-01-21T07:00:35.368Z
angular4-todoclient_1  | Hash: 9bc58c25a75cb62a0c88
angular4-todoclient_1  | Time: 9909ms
angular4-todoclient_1  | chunk {inline} inline.bundle.js (inline) 5.79 kB [entry] [rendered]
angular4-todoclient_1  | chunk {main} main.bundle.js (main) 33.9 kB [initial] [rendered]
angular4-todoclient_1  | chunk {polyfills} polyfills.bundle.js (polyfills) 548 kB [initial] [rendered]
angular4-todoclient_1  | chunk {scripts} scripts.bundle.js (scripts) 387 kB [initial] [rendered]
angular4-todoclient_1  | chunk {styles} styles.bundle.js (styles) 546 kB [initial] [rendered]
angular4-todoclient_1  | chunk {vendor} vendor.bundle.js (vendor) 11.1 MB [initial] [rendered]
angular4-todoclient_1  | 
angular4-todoclient_1  | webpack: Compiled successfully.
mongo-database_1       | 2018-01-21T07:05:22.332+0000 I STORAGE  [thread2] createCollection: config.system.sessions with generated UUID: 276d07dc-b507-4684-a3a6-75e560b44679
mongo-database_1       | 2018-01-21T07:05:22.347+0000 I INDEX    [thread2] build index on: config.system.sessions properties: { v: 2, key: { lastUse: 1 }, name: "lsidTTLIndex", ns: "config.system.sessions", expireAfterSeconds: 1800 }
mongo-database_1       | 2018-01-21T07:05:22.347+0000 I INDEX    [thread2]   building index using bulk method; build may temporarily use up to 500 megabytes of RAM
mongo-database_1       | 2018-01-21T07:05:22.347+0000 I INDEX    [thread2] build index done.  scanned 0 total records. 0 secs

请指导我错过的地方。提前谢谢。

1 个答案:

答案 0 :(得分:4)

将连接字符串“localhost”更改为“mongo-database”

mongoose.connect('mongodb://mongo-database:27017/tododb',options)

当你使用docker-compose时,它会创建两个容器,因此nodehost for node与localhost不同,mongo不同。 运行“docker container ls”这将为您提供连接字符串中使用的mongo容器名称。