Node.js MongoDB&;带Docker的nodejs“;MongoTimeoutError:服务器选择在30000毫秒后超时”;
我正在使用mongoDB和NodeJS后端。我有问题,问题有docker日志如下Node.js MongoDB&;带Docker的nodejs“;MongoTimeoutError:服务器选择在30000毫秒后超时”;,node.js,docker,mongoose,docker-compose,Node.js,Docker,Mongoose,Docker Compose,我正在使用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)
{ 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.
这是我的文件
#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
以下代码将mongodb连接到节点js中的mongoose:
mongoose.connect('mongodb://mongo_test:27017/collectionName', {useNewUrlParser: true});
如果我使用localhost,127.0.0.1,IP地址而不是mongo_测试,我会得到相同的结果。
我查阅了几个博客,堆栈溢出。
我该怎么办
(顺便说一句,我英语不好,所以即使用词不好也请理解。谢谢。)当前配置没有问题,重试几次后,app.js将连接到数据库 下面是正在发生的事情:
- docker compose正在启动Mongodb服务,端口尚未打开
- docker compose启动app_测试,app_测试尝试连接失败(2到3次)
- 几秒钟后,Mongodb端口现在在27017上打开
- 由于您的配置已
,node.js应用程序将重新启动并成功连接到数据库restart:always
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
在容器中使用
localhost
表示容器本身。如果你想访问另一个在特定端口监听的服务,那么你必须像http://:@Thilak那样构建你的URL。你说的服务名称是指docker服务吗?这里的服务名称
只不过是mongo_test
确保你的数据库已经启动并使用以下方法之一运行:,我想你的应用程序是在DB@Thilak如果是,是否应以“而不是”的形式mongodb://mongo_test:27017/collectionName"?