简体   繁体   English

nodejs 无法使用猫鼬连接到 mongodb

[英]nodejs cant connect to mongodb using mongoose

so all my applications were working just fine using mongoose, all of a sudden they all stopped.所以我所有的应用程序都使用 mongoose 工作得很好,突然间它们都停止了。 i cant connect to my database.我无法连接到我的数据库。 however my deployed apps using mongoose databases work fine, but i cant access to any database using my local computer.但是我使用 mongoose 数据库部署的应用程序工作正常,但我无法使用本地计算机访问任何数据库。 here is a test server ive written.这是我编写的测试服务器。 can someone help me whats happening.有人可以帮我看看发生了什么。

const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser')

const app = express();

app.use(express.json())

const db = require('./keys').mongoURI;

mongoose.connect(db, { useNewUrlParser: true, useUnifiedTopology: true })
    .then(() => console.log('connected'))
    .catch(err => console.log(err))

const port = process.env.PORT || 5000;

app.listen(port, () => console.log('server running'))



module.exports = {
    mongoURI: 'mongodb+srv://ertemishakk:<password>@cluster0-xi4zx.mongodb.net/test?retryWrites=true&w=majority'
}

Terminal: server running MongoTimeoutError: Server selection timed out after 30000 ms at Timeout._onTimeout (/Users/ishakertem/Desktop/test/node_modules/mongodb/lib/core/sdam/server_selection.js:308:9) at listOnTimeout (internal/timers.js:536:17) at processTimers (internal/timers.js:480:7) { name: 'MongoTimeoutError', }终端:服务器运行 MongoTimeoutError:服务器选择在 Timeout._onTimeout (/Users/ishakertem/Desktop/test/node_modules/mongodb/lib/core/sdam/server_selection.js:308:9) 在 30000 毫秒后超时在 listOnTimeout(内部/ timers.js:536:17) 在 processTimers (internal/timers.js:480:7) { name: 'MongoTimeoutError', }

You should check your你应该检查你的

bindIp : [public_ip] bindIp : [public_ip]

option in选项

mongod.conf mongod.conf

file.文件。 It should be pointed out to public IP of that system on which MongoDB is running.应该指出运行 MongoDB 的系统的公共 IP。 You can use 0.0.0.0 for debuging only, because it is exposed One. 0.0.0.0 只能用于调试,因为它是暴露的 1。

[DO VOTE TO THIS ANSWER, IF ITS HELPFUL TO YOU] [请投票给这个答案,如果它对你有帮助]

You Can Follow This Code I hope solved your problem你可以遵循这个代码我希望能解决你的问题

const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser')

const app = express();

app.use(express.json())


//Remove <password> and provide DB password
/**
 * Example: mongodb + srv: //ertemishakk:123@cluster0-xi4zx.mongodb.net/test?retryWrites=true&w=majorit
 */
mongoose.connect('mongodb+srv://ertemishakk:<password>@cluster0-xi4zx.mongodb.net/test?retryWrites=true&w=majorit', {
        useNewUrlParser: true,
        useUnifiedTopology: true
    })
    .then(() => console.log('connected'))
    .catch(err => console.log(err))

const port = process.env.PORT || 5000;

app.listen(port, () => console.log('server running'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM