Mongodb connection timeout error with mongoose and nodejs
I desperately need help. I am trying to upload a large file (8GB) to gridfs using mongoose and nodeJS. But since the file is very large, it takes a while to download. And after a while I get the following error:
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)
I tried to resolve this by increasing the connectionTimeoutMS, but the error still persists. I am using MongoDB 3.4.5 mongoose 4.8.4 nodejs 8.1.4 and npm 5.0.3.
Below is the app.js application:
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');
});
});
Below is the package.json file:
{
"name": "file-upload-gridfs",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo '' && 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"
}
}
source to share
Ok. I figured out the problem using this really helpful discussion. The default socket connection time for MongoDB is 30 seconds. If any request / operation takes longer, the connection is aborted and a connection timeout error occurs. With this change, I was able to upload a 32GB file to GridFS without any interruption.
https://github.com/Automattic/mongoose/issues/4789
I passed the timeout parameter like this.
server: {
socketOptions: {
socketTimeoutMS: 3000000,
connectionTimeoutMS: 3000000,
keepAlive:3000000
}
},
replset: {
socketOptions: {
keepAlive: 3000000,
connectTimeoutMS: 3000000
}
}
};
But it needs to be installed as follows:
const serverOptions = {
poolSize: 100,
socketOptions: {
socketTimeoutMS: 6000000
}
};
mongoose.createConnection(dbpath, {
server: serverOptions,
replset: serverOptions //if you are using replication
});
In my case, I used localhost.
const serverOptions = {
poolsize:100 ,
socketOptions:{
socketTimeoutMS: 6000000
}
};
var mongodbUri = 'mongodb://localhost:27017/gridFS';
mongoose.connect(mongodbUri, {
server: serverOptions
});
Hope this helps anyone with a similar problem.
source to share