[英]MongoDB on with Docker "failed to connect to server [localhost:27017] on first connect "
I am using mongoDB with and NodeJS backend.我正在使用带有 NodeJS 后端的 mongoDB。 The Problem is I am getting the following error
问题是我收到以下错误
node:16) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]
node:16) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]
This is my docker-compose这是我的 docker-compose
version: '3.4'
services:
db:
image: mongo:latest
ports:
- '27017:27017'
rest-api-node:
build: .
ports:
- '5000:5000'
links:
- db
restart: on-failure
I have tried with depends_on
as well , was not working.我也尝试过
depends_on
,但没有工作。
On backend I am mongoose as a middleware to communicate with DB.在后端,我是猫鼬作为与数据库通信的中间件。 this is the part of my
index.js
这是我的
index.js
的一部分
mongoose.Promise = global.Promise
mongoose.connect('mongodb://localhost/demo')
app.listen(port, () => console.log("live"))
I have tried using promise as well , no change though.我也尝试过使用 promise ,但没有任何变化。 Please Help me out.
请帮帮我。 Thanks in advance
提前致谢
complete error log完整的错误日志
at Pool.
在游泳池。 (/app/node_modules/mongodb-core/lib/topologies/server.js:505:11) rest-api-node_1 |
(/app/node_modules/mongodb-core/lib/topologies/server.js:505:11) rest-api-node_1 | at Pool.emit (events.js:180:13) rest-api-node_1 |
在 Pool.emit (events.js:180:13) rest-api-node_1 | at Connection.
在连接处。 (/app/node_modules/mongodb-core/lib/connection/pool.js:329:12) rest-api-node_1 |
(/app/node_modules/mongodb-core/lib/connection/pool.js:329:12) rest-api-node_1 | at Object.onceWrapper (events.js:272:13) rest-api-node_1 |
在 Object.onceWrapper (events.js:272:13) rest-api-node_1 | at Connection.emit (events.js:180:13) rest-api-node_1 |
在 Connection.emit (events.js:180:13) rest-api-node_1 | at Socket.
在套接字。 (/app/node_modules/mongodb-core/lib/connection/connection.js:245:50) rest-api-node_1 |
(/app/node_modules/mongodb-core/lib/connection/connection.js:245:50) rest-api-node_1 | at Object.onceWrapper (events.js:272:13) rest-api-node_1 |
在 Object.onceWrapper (events.js:272:13) rest-api-node_1 | at Socket.emit (events.js:180:13) rest-api-node_1 |
在 Socket.emit (events.js:180:13) rest-api-node_1 | at emitErrorNT (internal/streams/destroy.js:64:8) rest-api-node_1 |
在 emitErrorNT (internal/streams/destroy.js:64:8) rest-api-node_1 | at process._tickCallback (internal/process/next_tick.js:178:19) rest-api-node_1 |
在 process._tickCallback (internal/process/next_tick.js:178:19) rest-api-node_1 | name: 'MongoNetworkError', rest-api-node_1 |
名称:'MongoNetworkError',rest-api-node_1 |
message: 'failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]' }消息:'第一次连接时无法连接到服务器 [localhost:27017] [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]'}
By default Compose sets up a single network for your app.
默认情况下, Compose 会为您的应用程序设置一个网络。 Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name.
服务的每个容器都加入默认网络,并且可以被该网络上的其他容器访问,并且可以通过与容器名称相同的主机名被它们发现。
According to your docker-compose.yaml
file you can access you mongo container on 127.0.0.1:27017
only from host machine.根据您
docker-compose.yaml
文件,您只能从主机访问127.0.0.1:27017
上的 mongo 容器。 In order to access it from NodeJS backend container you should use db:27017
.为了从 NodeJS 后端容器访问它,您应该使用
db:27017
。
I have the same problem, other solutions not work for me but I did that this way我有同样的问题,其他解决方案对我不起作用,但我是这样做的
for mongo URI you must use your MongoDB service name
instead 127.0.0.1
or localhost
对于 mongo URI,您必须使用 MongoDB
service name
而不是127.0.0.1
或localhost
for example, in below docker-compose file my mongo service name is mongodb-myapp
and I change URI like this mongodb://mongodb-myapp:27017/myapp
and it works for me例如,在下面的 docker-compose 文件中,我的 mongo 服务名称是
mongodb-myapp
并且我像这样更改 URI mongodb://mongodb-myapp:27017/myapp
它适用于我
services:
boilerplate-api-app:
build: .
environment:
- MONGO_URI=mongodb://mongodb-myapp:27017/myapp
volumes:
- .:/app
ports:
- "5000:5000"
depends_on:
- mongodb-myapp
mongodb-myapp:
image: mongo
ports:
- "27017:27017"
I was also stuck on this for hours!我也被困了几个小时! Setting
db:27017
in the connection string and restart: always
on the rest-api-node
makes sure that you connect to the correct IP and makes sure that the node server keeps on trying to connect to your db.在连接字符串中设置
db:27017
并restart: always
在rest-api-node
上确保您连接到正确的 IP 并确保节点服务器继续尝试连接到您的数据库。 This worked for me!这对我有用!
Change this 改变这个
mongoose.connect('mongodb://localhost:27017/dbName', { useNewUrlParser: true });
To this 对此
mongoose.connect('mongodb://db:27017/dbName', { useNewUrlParser: true }); // docker-compose service name
Change localhost
or 127.0.0.1
in your mongoose.connect
to your docker-compose service name. 更改
localhost
或127.0.0.1
你mongoose.connect
到您的码头工人,撰写服务名称。
docker-compose.yaml docker-compose.yaml
version: "3"
services:
node-app:
build: .
ports:
- "3000:3000"
environment:
- PORT=3000
mongo:
image: mongo
environment:
- MONGO_INITDB_ROOT_USERNAME=imran
- MONGO_INITDB_ROOT_PASSWORD=test_password
volumes:
- mongo-db:/data/db
volumes:
mongo-db:
The above code is the docker-compose.yaml file and the service name for the MongoDB is 'mongo'.上面的代码是 docker-compose.yaml 文件,MongoDB 的服务名称是 'mongo'。 You don't have to manually insert the IP address of the container.
您不必手动插入容器的 IP 地址。 Instead of that just insert the service name in the MongoDB URI like this sample code given below:
而不是像下面给出的示例代码那样在 MongoDB URI 中插入服务名称:
const express = require("express");
const mongoose = require("mongoose");
const app = express();
mongoose
.connect("mongodb://imran:test_password@mongo:27017/?authSource=admin")
.then(() => {
console.log("Successfully connected to the DB");
})
.catch((e) => {
console.log(e);
});
const port = process.env.PORT || 3000;
// routes
app.get("/", (req, res) => {
res.send("<h1>Running on Container!</h1>");
});
app.listen(port, () => console.log(`listening on port ${port}`));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.