数据库
首页 > 数据库> > javascript – 与mongoose和nodejs的mongodb连接超时错误

javascript – 与mongoose和nodejs的mongodb连接超时错误

作者:互联网

我迫切需要一些帮助.我正在尝试使用mongoose和nodeJS将大文件(8 GB)上传到gridfs.但由于文件非常大,上传需要一些时间.过了一会儿我收到以下错误:

home/user/FileUpload/node_modules/mongodb/lib/utils.js:98
    process.nextTick(function() { throw err; });
                                  ^
MongoError: connection 0 to 127.0.0.1:27017 timed out
    at Function.MongoError.create (/home/user/FileUpload/node_modules/mongodb-core/lib/error.js:29:11)
    at Socket.<anonymous> (/home/user/FileUpload/node_modules/mongodb-core/lib/connection/connection.js:186:20)
    at Object.onceWrapper (events.js:314:30)
    at emitNone (events.js:105:13)
    at Socket.emit (events.js:207:7)
    at Socket._onTimeout (net.js:402:8)
    at ontimeout (timers.js:488:11)
    at tryOnTimeout (timers.js:323:5)
    at Timer.listOnTimeout (timers.js:283:5)

我试图通过增加connectTimeoutMS来解决这个问题,但错误仍然存​​在.我使用的是MongoDB 3.4.5 mongoose 4.8.4 nodejs 8.1.4和npm 5.0.3.

以下是app.js:

var mongoose = require('mongoose');
var schema = mongoose.schema;
mongoose.connect('mongodb://127.0.0.1/gridFS'),{
    server: {
        socketOptions: {
            socketTimeoutMS: 3000000,
            connectionTimeoutMS: 3000000,
            keepAlive:3000000
        }
    },

    replset: {
        socketOptions: {
            keepAlive: 3000000,
            connectTimeoutMS: 3000000
        }
    }
};
var conn = mongoose.connection;
var path = require('path');
var Grid = require('gridfs-stream');
var fs = require('fs');
var videoPath = path.join(__dirname, 'readFrom/bio seq test1.txt');
Grid.mongo = mongoose.mongo;
conn.once('open', function(){

    console.log('- connection open -');
    var gfs = Grid(conn.db);

    var writestream = gfs.createWriteStream({

        filename: 'bio seq test 1'
    });

    fs.createReadStream(videoPath).pipe(writestream);
    writestream.on('close', function(file){
      console.log(file.filename + 'Written to DB');
    });
});

以下是package.json文件:

{
  "name": "file-upload-gridfs",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo '' &amp;&amp; exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.16.1",
    "cookie-parser": "^1.4.3",
    "express": "^4.14.1",
    "gridfs-stream": "^1.1.1",
    "mongoose": "^4.8.4",
    "morgan": "^1.8.2",
    "multer": "1.3.0",
    "multer-gridfs-storage": "1.0.0",
    "path.join": "^1.0.0",
    "serve-favicon": "^2.4.3"
  }
}

解决方法:

好.我使用这个非常有用的讨论找出了问题.MongoDB的默认套接字连接时间是30秒.如果任何查询/操作花费的时间超过此时间,则中止连接并发生连接超时错误.
通过此更改,我能够将没有任何中断的32GB文件上传到GridFS.

https://github.com/Automattic/mongoose/issues/4789

我按以下方式传递了timeout参数.

server: {
        socketOptions: {
            socketTimeoutMS: 3000000,
            connectionTimeoutMS: 3000000,
            keepAlive:3000000
        }
    },

    replset: {
        socketOptions: {
            keepAlive: 3000000,
            connectTimeoutMS: 3000000
        }
    }
};

但需要按以下方式设置:

const serverOptions = {
  poolSize: 100,
  socketOptions: {
    socketTimeoutMS: 6000000
  }
};

mongoose.createConnection(dbpath, {
  server: serverOptions,
  replset: serverOptions //if you are using replication
});

在我的情况下,我使用localhost.

const serverOptions = {
    poolsize:100 ,
    socketOptions:{
        socketTimeoutMS: 6000000
        }
    };
    var mongodbUri = 'mongodb://localhost:27017/gridFS';
    mongoose.connect(mongodbUri, {
    server: serverOptions
    });

希望这可以帮助任何有类似问题的人.

标签:javascript,node-js,mongodb,mongoose,gridfs
来源: https://codeday.me/bug/20190527/1163699.html