本文介绍了MongoDB 连接错误:MongoTimeoutError:服务器选择在 30000 毫秒后超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个完整的应用程序,阅读以下教程:

I am trying to create a fullstack app reading the following tutorial:

https://medium.com/javascript-in-plain-english/full-stack-mongodb-react-node-js-express-js-in-one-simple-app-6cc8ed6de274

我按照所有步骤操作,然后尝试运行:

I followed all steps and then tried to run:

node server.js

但我收到以下错误:

MongoDB 连接错误:MongoTimeoutError: Server selection timed30000 毫秒后退出在 Timeout._onTimeout (C:RNDfullstack_appackendode_modulesmongodblibcoresdamserver_selection.js:308:9)在 listOnTimeout (internal/timers.js:531:17)在 processTimers (internal/timers.js:475:7) { 名称:'MongoTimeoutError',原因:错误:连接 ETIMEDOUT99.80.11.208:27017在 TCPConnectWrap.afterConnect [as oncomplete] (net.js:1128:14) {name: 'MongoNetworkError',[Symbol(mongoErrorContextSymbol)]: {} }, [Symbol(mongoErrorContextSymbol)]: {} } (node:42892)UnhandledPromiseRejectionWarning: MongoTimeoutError: 服务器选择30000 毫秒后超时在 Timeout._onTimeout (C:RNDfullstack_appackendode_modulesmongodblibcoresdamserver_selection.js:308:9)在 listOnTimeout (internal/timers.js:531:17)在 processTimers (internal/timers.js:475:7)

我在 server.js 上的代码如下:

My code at server.js is as follows:

const mongoose = require('mongoose');
const router = express.Router();

// this is our MongoDB database
const dbRoute =
    'mongodb+srv://user:<password>@cluster0-3zrv8.mongodb.net/test?retryWrites=true&w=majority';

mongoose.Promise = global.Promise;

// connects our back end code with the database
mongoose.connect(dbRoute,
    {   useNewUrlParser: true,
        useUnifiedTopology: true
    });

let db = mongoose.connection;

db.once('open', () => console.log('connected to the database'));

有什么建议吗?

推荐答案

只需进入 mongodb atlas 管理面板.进入安全选项卡>网络访问>然后通过添加将您的 IP 列入白名单

just go to mongodb atlas admin panel.Go in security tab>Network Access> Then whitelist your IP by adding it

查看此图片以找到特定菜单

注意:在谷歌上检查你的IP然后添加

Note:Check your IP on google then add it

这篇关于MongoDB 连接错误:MongoTimeoutError:服务器选择在 30000 毫秒后超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 15:54