gpt4 book ai didi

node.js - MongoDB 和 Nodejs 与 Docker "MongoTimeoutError: Server selection timed out after 30000 ms"

转载 作者:太空宇宙 更新时间:2023-11-03 22:19:41 26 4
gpt4 key购买 nike

我正在使用 mongoDB 和 NodeJS 后端。我有问题,问题有 docker 日志如下。

{ MongoTimeoutError: Server selection timed out after 30000 ms
at Timeout.setTimeout [as _onTimeout] (/usr/src/app/node_modules/mongodb/lib/core/sdam/topology.js:897:9)
at ontimeout (timers.js:436:11)
at tryOnTimeout (timers.js:300:5)
at listOnTimeout (timers.js:263:5)
at Timer.processTimers (timers.js:223:10)
name: 'MongoTimeoutError',
reason:
{ Error: connect ECONNREFUSED 127.0.0.1:27017
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1113:14)
name: 'MongoNetworkError',
errorLabels: [ 'TransientTransactionError' ],
[Symbol(mongoErrorContextSymbol)]: {} },
[Symbol(mongoErrorContextSymbol)]: {} }
(node:1) UnhandledPromiseRejectionWarning: MongoTimeoutError: Server selection timed out after 30000 ms
at Timeout.setTimeout [as _onTimeout] (/usr/src/app/node_modules/mongodb/lib/core/sdam/topology.js:897:9)
at ontimeout (timers.js:436:11)
at tryOnTimeout (timers.js:300:5)
at listOnTimeout (timers.js:263:5)
at Timer.processTimers (timers.js:223:10)
(node:1) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:1) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

这是我的 Dockerfile

#FROM node:10.12.0
FROM node:latest-alpline

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package.json ./

RUN npm install
# If you are building your code for production
# RUN npm install --only=production

# Bundle app source
COPY . .

EXPOSE 3000
CMD [ "node", "app.js" ]

这是 docker-compose.yml

version: "2"
services:
app:
container_name: app_test
restart: always
build: .
ports:
- "3000:3000"
links:
- mongo_test
depends_on:
- mongo_test
networks:
- nodeapp_network
mongo_test:
container_name: mongo_test
image: mongo
volumes:
- ./data:/data/db
ports:
- "27017:27017"
networks:
- nodeapp_network
networks:
nodeapp_network:
driver: bridge

以下代码在node js中将mongodb连接到mongoose:

mongoose.connect('mongodb://mongo_test:27017/collectionName', {useNewUrlParser: true});

如果我使用 localhost、127.0.0.1、IP 地址而不是 mongo_test,我会得到相同的结果。我查阅了几个博客,堆栈溢出。我该怎么办?

(ps.我英文不好,所以虽然言语有些别扭,但请理解。谢谢。)

最佳答案

当前配置没有任何问题,经过几次重试后,app.js 正在连接到数据库

这是发生的事情:

  • docker-compose 正在启动 Mongodb 服务,端口尚未开放
  • docker-compose 启动 app_test 并且 app_test 尝试连接失败(2 到 3 次)
  • 几秒钟后,Mongodb 端口现已在 27017 上打开
  • 由于您的配置具有restart:always,node.js 应用将重新启动并成功连接到数据库

为了避免这些重新启动,你可以这样写

setTimeout(function() {
try {
await mongoose.connect('mongodb://mongo_test:27017/collectionName',
{ useNewUrlParser: true }
);
} catch (error) {
console.log('Error connecting - retrying in 30 seconds')
}, 30000); // Wait for 30 seconds and try to connect again

关于node.js - MongoDB 和 Nodejs 与 Docker "MongoTimeoutError: Server selection timed out after 30000 ms",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59558828/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com