Home  >  Q&A  >  body text

node.js - nodejs连接mongodb失败的问题

前些天用python做了一个能发文章的博客,最近学习了nodejs,准备用nodejs也来做一遍,可是当我连接MongoDB的时候遇上了*烦。
不管我用mongo 或者mongoose都会提示

{ [MongoError: failed to connect to server [localhost:27017] on first connect]
  name: 'MongoError',
  message: 'failed to connect to server [localhost:27017] on first connect' }

先说明一下,O(∩_∩)O~

1.我的MongoDB开着,版本2.0.0 。而且可用MongoDB自带的mongo连接。
2.我的系统是Windows Xp
3.我用python可以连接MongoDB
4.mongoose版本: ^4.5.7"

贴一下代码

var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost:27017/test',function(err){
    if(err){
        console.log('连接数据库错误!');
        console.log('错误信息:\n\t');
        console.log(err);
    }else{
        console.log('Success!!!');
    }
})

尝试了网上说的删除mongod.lock,还有尝试mongodb://127.0.0.1:27017/test连接都不行,是不是xp系统的问题?还是连接权限的问题?请大神指教!!!

天蓬老师天蓬老师2766 days ago447

reply all(1)I'll reply

  • 阿神

    阿神2017-04-17 15:02:53

    https://docs.mongodb.com/manu...

    Starting in version 2.2, MongoDB does not support Windows XP. Please use a more recent version of Windows to use more recent releases of MongoDB.

    Although I am curious where you found the 2.0 version, it was probably 4 or 5 years ago. But if you really want to use it this way, you need to find a driver corresponding to 2.0.

    reply
    0
  • Cancelreply