【问题标题】:Unable to connect with mongodb localhost:27017无法连接 mongodb localhost:27017
【发布时间】:2019-06-07 21:24:44
【问题描述】:

我尝试使用 nodejs 服务器和 express 连接 mongodb。我的 mongoose 版本是 5.2.0,mongodb 版本是 4.0。我尝试了多种连接 mongodb 的方法,但 mongod 服务器只是一直在监听,不与服务器连接,也没有创建数据库。我曾尝试与 mongoose 和 mongoclient 连接,但 cmd 中的 mongod 只是一直在听,没有连接。这是我的服务器代码

var express = require('express');
var mongoose = require('mongoose');

var User = require('./models/user');
var app = express();


var db = mongoose.connect("mongodb://localhost:27017/server", { useNewUrlParser: true }, function(err, response) {
    if(err) {
        console.log('There is error in connecting with mongodb');
    }
    console.log('Connection has been established.');
})

cmd中的mongod

2019-01-13T23:56:05.690+0500 I CONTROL  [initandlisten]
2019-01-13T23:56:05.691+0500 I CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
2019-01-13T23:56:05.691+0500 I CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server.
2019-01-13T23:56:05.692+0500 I CONTROL  [initandlisten] **          Start the server with --bind_ip <address> to specify which IP
2019-01-13T23:56:05.692+0500 I CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
2019-01-13T23:56:05.693+0500 I CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
2019-01-13T23:56:05.693+0500 I CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.
2019-01-13T23:56:05.694+0500 I CONTROL  [initandlisten]
2019-01-13T23:56:06.073+0500 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
2019-01-13T23:56:06.078+0500 I NETWORK  [initandlisten] waiting for connections on port 27017

mongo in cmd 另一个窗口

C:\Program Files\MongoDB\Server\4.0\bin>mongo
MongoDB shell version v4.0.2
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 4.0.2
Server has startup warnings:
2019-01-11T20:05:57.109+0500 I CONTROL  [initandlisten]
2019-01-11T20:05:57.113+0500 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-01-11T20:05:57.114+0500 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2019-01-11T20:05:57.114+0500 I CONTROL  [initandlisten]
---

---
>

【问题讨论】:

  • 你运行mongod后在27017等待连接,然后运行你的代码。你的输出是什么?你能console.log(err)并粘贴吗
  • @feiiiiii 没有错误,甚至没有任何已弃用的警告。只需在控制台中显示已建立连接。 ://
  • 当您尝试使用mongo 命令通过命令行直接连接到它时会发生什么?
  • @Ahsan 那么这意味着您已连接,您应该在等待端口 27017 上的连接后看到控制台这样说,然后通过命令行使用 mongo 连接。看到
  • @unflores 是的,我知道,我确实尝试通过另一个 cmd 窗口运行 mongo,但它仍在监听,并且没有使用名称服务器创建数据库:/

标签: node.js mongodb mongoose


【解决方案1】:

您应该将您的mongodb.bat 更改为下面提到的命令:-

"C:\Program Files\MongoDB\Server\4.2\bin\mongod.exe" --dbpath "f:\mongodb\data"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    相关资源
    最近更新 更多