使用远程Mongo DB时,Sails无法加载

3

当我连接本地MongoDB时,我的Sail应用程序可以运行,但是当我尝试使用Modulus托管的数据库进行连接时,它无法正确加载。

我已正确设置了连接,并通过IDE进行了测试。

connections.js:
    // MONGO DB reference for the database
    cogspeed: {
    adapter   : 'sails-mongo',
    //host      : 'localhost',
    host      : 'novus.modulusmongo.net',
    port      : 27017,
    user      : '*********',
    password  : '***********',
    database  : '**********'
  },

The error I get is:

C:\Projects\gmm\cogspeed>sails lift

info: Starting app...

error: A hook (`orm`) failed to load!
error: Error: failed to connect to [localhost:27017]
    at null.<anonymous> (C:\Projects\gmm\cogspeed\node_modules\sails-mongo\node_modules\mongodb\lib\mongodb\conne
ction\server.js:553:74)
    at EventEmitter.emit (events.js:106:17)
    at null.<anonymous> (C:\Projects\gmm\cogspeed\node_modules\sails-mongo\node_modules\mongodb\lib\mongodb\conne
ction\connection_pool.js:140:15)
    at EventEmitter.emit (events.js:98:17)
    at Socket.<anonymous> (C:\Projects\gmm\cogspeed\node_modules\sails-mongo\node_modules\mongodb\lib\mongodb\con
nection\connection.js:512:10)
    at Socket.EventEmitter.emit (events.js:95:17)
    at net.js:441:14
    at process._tickDomainCallback (node.js:459:13)

为什么Sails仍在查看本地主机?
提前致谢!

你的/config/models.js文件长什么样子? - sgress454
以下是/config/models.js文件的内容: module.exports.models = { // 您应用程序的默认连接。 // 即您应用程序的某个连接的名称(请参见config/connections.js) // // (默认为localDiskDb) connection: 'cogspeed-mongo' }; - user2900166
罪魁祸首是/config/local.js文件,其mongo适配器指向本地主机。 - user2900166
5个回答

6

我曾经遇到过同样的问题。事实证明,在connections.js中使用相同适配器的多个连接会导致此问题。我的sails版本是0.10.5。


是的。我只是将localDiskDb注释掉,然后保留了我的someMongodbServer,这样就可以工作了。 - Francisco Corrales Morales
1
这个问题仍然存在于Sailsjs的v0.11版本中。 - K20GH

3

请确认您的密码不包含“@”字符,因为当前的node-mongodb-native实现会干扰您的连接字符串。


1
/config/local.js文件仍具有针对本地主机指向mongo的适配器。

1
Sails (v 0.11.0)仍然存在一个问题,即在/config/connections.js配置文件中有多个相同类型的适配器。在部署到Modulus后,我遇到了同样的问题,但支持人员无法帮助我。我花了几个小时来尝试调试它...但解决方法非常简单。不要将db连接参数放入connections.js中,而是直接将它们放入特定环境的配置文件中,就像这样:
// config/env/development.js
module.exports = {

  connections : {
       mongo: {
          adapter: 'sails-mongo',
          host: 'localhost',
          port: 27017,
          database: 'your-db-name'
      },
  },
  // your other dev configs
};

对于生产:

// config/env/development.js
module.exports = {

  connections : {
       mongo: {
          adapter: 'sails-mongo',
          host: 'your-remote-mongo-host',
          port: 27017,
          user: 'your-remote-user',
          password: 'your-remote-db-password',
          database: 'your-remote-db-name'
      },
  },
  // your other dev configs
};

你的/config/models.js看起来是这样的:

module.exports = {

  connection : 'mongo',

  // your other model configs
};

PS:这个问题已经向 sails.js 团队 https://github.com/balderdashy/sails/issues/2480 报告过了。


0

如果错误提示为

error: Error: failed to connect to [localhost:27017]

那么,Mongo没有运行。您可以使用以下命令启动它

mongod

有时候会犯傻瓜错误......


如果错误提示为 error: MongoError: auth fails,那么也许这个解决方案适合你:https://stackoverflow.com/a/29335121/2615737

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接