通过本地主机连接到 MongoDB:27017 被拒绝

问题描述 投票:0回答:2

我正在使用 MERN 堆栈构建一个应用程序。我正在尝试通过 localhost:27017 方法连接 MongoDB。当我运行 Node app.js 时,我的应用程序将打开,我可以通过在浏览器中访问 localhost:4000 来访问它,但是当它尝试连接到数据库时,它会挂起并抛出此错误:

    Oh no Mongo Connection error!!
MongooseServerSelectionError: connect ECONNREFUSED ::1:27017
    at Connection.openUri (C:\Users\steph\OneDrive\Documents\GitHub\CRM-Tool\node_modules\mongoose\lib\connection.js:819:32)
    at C:\Users\steph\OneDrive\Documents\GitHub\CRM-Tool\node_modules\mongoose\lib\index.js:378:10
    at C:\Users\steph\OneDrive\Documents\GitHub\CRM-Tool\node_modules\mongoose\lib\helpers\promiseOrCallback.js:32:5
    at new Promise (<anonymous>)
    at promiseOrCallback (C:\Users\steph\OneDrive\Documents\GitHub\CRM-Tool\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:10)
    at Mongoose._promiseOrCallback (C:\Users\steph\OneDrive\Documents\GitHub\CRM-Tool\node_modules\mongoose\lib\index.js:1223:10)
    at Mongoose.connect (C:\Users\steph\OneDrive\Documents\GitHub\CRM-Tool\node_modules\mongoose\lib\index.js:377:20)
    at Object.<anonymous> (C:\Users\steph\OneDrive\Documents\GitHub\CRM-Tool\app.js:16:10)
    at Module._compile (node:internal/modules/cjs/loader:1112:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1166:10) {
  reason: TopologyDescription {
    type: 'Unknown',
    servers: Map(1) { 'localhost:27017' => [ServerDescription] },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    logicalSessionTimeoutMinutes: undefined
  },
  code: undefined
}

我已经在我的机器上安装了 MongoDB,并且有 mongod.exe 二进制文件。我还创建了文件夹 C:\data\db 因为这是 mongod.exe 存储其数据的默认位置,我猜它必须存在。我还安装了 MongoDB Compass,并且能够通过端口 27017 进行连接,并创建了我在代码中指定的 /crm 数据库。当我已经具备通过 Compass 连接的能力时,我觉得上面提到的 cmd 连接方法实际上是多余的。我只是沿着这条路走下去,看看它是否可以解决问题,但它没有。 这是我的 app.js 代码:

    require('dotenv').config()
const express = require('express');
const app = express();
const path = require('path');
const ejs = require('ejs');
const ejsMate = require('ejs-mate');
const mongoose = require('mongoose');
const methodOverride = require('method-override'); //to be able to use app.put

// require routes USER, PRODUCT, CUSTOMER, SUPPLIER
const productRoutes = require('./routes/products');
const supplierRoutes = require('./routes/supplier');
const customerRoutes = require('./routes/customer')

// mongoose.connect(process.env.DATABASE_URL, { useNewUrlParser: true })
mongoose.connect('mongodb://localhost:27017/crm', { useNewUrlParser: true, useUnifiedTopology: true })
  .then(() => {
    console.log("Mongo connection open!!!");
  })
  .catch((err) => {
    console.log("Oh no Mongo Connection error!!");
    console.log(err);
  })


// Static Files
app.use(express.static(path.join(__dirname, 'public')));


// ejs allows us to use javascript codes within html files
app.set('view engine', 'ejs');
app.engine('ejs', ejsMate);

// to setup the path
app.set('views', path.join(__dirname, 'views'));

// Parsing Middleware
app.use(express.urlencoded({extended:true}))
app.use(methodOverride('_method'));

// To use the exported routes
app.use('/dashboard/products', productRoutes);
app.use('/dashboard/suppliers', supplierRoutes);
app.use('/dashboard/customers', customerRoutes);

// show routes for REGULAR PAGES
app.get('/', (req, res) => {
  res.render('main');
});

app.get('/login', (req, res) => {
  res.render('login');
})

// this is just for designing purposes,
// must be deleted once deployed because dashboard must depend on who was logged in
app.get('/dashboard', (req, res) => {
  res.render('dashboard');
})

app.post('/dashboard', (req, res) => {
  console.log('you are logged in')
  res.render('dashboard');
})

// CUSTOMER'S SECTION
app.get('/dashboard/customers', (req, res) => {
  res.render('pages/customers/show');
})


app.listen(4000, () => {
  console.log('Serving port 4000')
});

正如我所说,该应用程序通过端口 4000 显示在我的浏览器中,但数据库从未启动并运行。它只是超时并抛出上面的错误消息。我只是希望能够将我的应用程序连接到 localhost:27017 数据库,以便我可以开始向服务器发送数据。谢谢。

javascript mongodb
2个回答
0
投票

您的 mongodB 可能没有运行。

这里问了类似的问题,已经回答了。

MongoError:连接ECONNREFUSED 127.0.0.1:27017


0
投票

当 Mongo 服务器意外停止时,通常会发生此错误。这是针对 Windows 用户的简单解决方案:

1.打开任务管理器。 2.切换到“服务”选项卡。 3.在列表中查找MongoDB。 4.右键单击MongoDB并选择“启动”,将其状态从“已停止”更改为“正在运行”。 5.一旦 MongoDB 再次运行,您可以使用“mongosh”cmd 或单击 MongoDB Compass 中的“连接”按钮来重新启动应用程序。

此过程可确保 MongoDB 正常运行,并允许您重新启动应用程序而不会出现任何问题。

© www.soinside.com 2019 - 2024. All rights reserved.