【问题标题】:Mongoose gives error while connecting using mongolab uri ?Mongoose 在使用 mongolab uri 连接时出错?
【发布时间】:2016-04-12 13:50:25
【问题描述】:

它给出了以下错误-

mongoose connection error:  { [MongoError: connect EINVAL] name: 'MongoError', message: 'connect EINVAL' }

/home/user/Documents/oes/node_modules/connect-mongo/node_modules/mongodb/lib/server.js:228
    process.nextTick(function() { throw err; })
                                        ^
Error: connect EINVAL
at errnoException (net.js:905:11)
at connect (net.js:767:19)
at net.js:846:9
at asyncCallback (dns.js:68:16)
at Object.onanswer [as oncomplete] (dns.js:121:9)

14 Jun 18:04:11 - [nodemon] 应用程序崩溃 - 在启动前等待文件更改...

【问题讨论】:

    标签: node.js mongodb mlab


    【解决方案1】:

    这是一个非常古老的问题,但目前还没有人回答它。

    我们经常看到用户在使用 Mongoose 驱动程序连接到 MongoLab 时遇到问题。根本原因几乎总是驱动程序的错误配置,尤其是在超时时。以下是使用 MongoLab 推荐的驱动程序选项的连接示例:

    // mongoose 4.3.x
    var mongoose = require('mongoose');
    
    
    
    /* 
     * Mongoose by default sets the auto_reconnect option to true.
     * We recommend setting socket options at both the server and replica set level.
     * We recommend a 30 second connection timeout because it allows for 
     * plenty of time in most operating environments.
     */
    var options = { server: { socketOptions: { keepAlive: 300000, connectTimeoutMS: 30000 } }, 
                    replset: { socketOptions: { keepAlive: 300000, connectTimeoutMS : 30000 } } };       
    
    var mongodbUri = 'mongodb://user:pass@host:port/db';
    
    mongoose.connect(mongodbUri, options);
    var conn = mongoose.connection;             
    
    conn.on('error', console.error.bind(console, 'connection error:'));  
    
    conn.once('open', function() {
      // Wait for the database connection to establish, then start the app.                         
    });
    

    如果这没有帮助,请确保您的密码或用户名没有任何特殊字符,请先尝试通过 CMD 连接。

    希望我能帮上忙 :)

    【讨论】:

    • 确保密码中没有特殊字符的建议是个好建议。请确保您的密码中没有“@”符号。我的里面有和@,我想它与URI模板冲突。
    猜你喜欢
    • 1970-01-01
    • 2018-01-07
    • 2015-08-31
    • 1970-01-01
    • 2016-01-25
    • 2016-03-16
    • 1970-01-01
    • 2014-08-18
    • 1970-01-01
    相关资源
    最近更新 更多