我尝试使用nodejs服务器与mongodb连接并进行表达。我的猫鼬版本是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.');
})
mongod in cmd
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在另一个窗口中
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]
---
---
>
答案 0 :(得分:0)
您应该将 mongodb.bat
更改为下面提到的命令:-
"C:\Program Files\MongoDB\Server\4.2\bin\mongod.exe" --dbpath "f:\mongodb\data"