启动mongodb时出现TransientTransactionError

时间:2019-02-10 11:45:51

标签: node.js linux mongodb google-cloud-platform google-compute-engine

我无法解决mongodb连接问题。在启动节点应用程序时出现以下错误。

这是我的猫鼬连接代码。

var mongoose = require('mongoose');

mongoose.Promise = global.Promise;
mongoose.connect(process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017/ml').then(
    () => { console.log('Connected to mongodb!') },
    err => { console.log('Failed to connect to mongodb'); console.log(err); }
);

module.exports = {mongoose};

这是错误。

Started up at port 80
Failed to connect to mongodb
{ MongoNetworkError: failed to connect to server [10.160.0.2:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 10.160.0.2:27017]
    at Pool.<anonymous> (/home/shivamsharma_btp2/mlsever/magguland/node_modules/mongodb-core/lib/topologies/server.js:564:11)
    at Pool.emit (events.js:189:13)
    at Connection.<anonymous> (/home/shivamsharma_btp2/mlsever/magguland/node_modules/mongodb-core/lib/connection/pool.js:317:12)
    at Object.onceWrapper (events.js:277:13)
    at Connection.emit (events.js:189:13)
    at Socket.<anonymous> (/home/shivamsharma_btp2/mlsever/magguland/node_modules/mongodb-core/lib/connection/connection.js:246:50)
    at Object.onceWrapper (events.js:277:13)
    at Socket.emit (events.js:189:13)
    at emitErrorNT (internal/streams/destroy.js:82:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
    at process._tickCallback (internal/process/next_tick.js:63:19)
  name: 'MongoNetworkError',
  errorLabels: [ 'TransientTransactionError' ],
  [Symbol(mongoErrorContextSymbol)]: {} }

这是我的Google云应用的防火墙规则。

1 个答案:

答案 0 :(得分:0)

解决方案!

在命令之前使用sudo启动服务器。