mongoDB 有重新连接问题还是我做错了? [英] Does mongoDB have reconnect issues or am i doing it wrong?

查看:37
本文介绍了mongoDB 有重新连接问题还是我做错了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 nodejs 和 mongoDB - 我遇到了一些连接问题.

I'm using nodejs and a mongoDB - and I'm having some connection issues.

好吧,实际上是唤醒"问题!它连接得非常好 - 速度超快,我对结果总体上很满意.

Well, actually "wake" issues! It connects perfectly well - is super fast and I'm generally happy with the results.

我的问题:如果我有一段时间不使用连接(我说是一段时间,因为时间范围变化超过 5 分钟),它似乎会停止.我不会触发断开连接事件 - 它只是挂起.

My problem: If i don't use the connection for a while (i say while, because the timeframe varies 5+ mins) it seems to stall. I don't get disconnection events fired - it just hangs.

最终我得到了一个类似错误的响应:无法连接到 [ * .mongolab.com: * ] - ( * = 掩码值)

Eventually i get a response like Error: failed to connect to [ * .mongolab.com: * ] - ( * = masked values)

应用程序快速重启,连接再次良好.有时,如果我不重新启动应用程序,我可以刷新并愉快地重新连接.

A quick restart of the app, and the connection's great again. Sometimes, if i don't restart the app, i can refresh and it reconnects happily.

这就是为什么我认为这是唤醒"问题.

This is why i think it is "wake" issues.

粗略的代码大纲:

我没有包含代码 - 我认为不需要.它有效(除了连接丢失)

I've not included the code - I don't think it's needed. It works (apart from the connection dropout)

注意事项:只有一个连接"——我从不关闭它.我从不重新打开.

Things to note: There is just the one "connect" - i never close it. I never reopen.

我正在使用猫鼬、socketio.

I'm using mongoose, socketio.

/* constants */

var mongoConnect = 'myworkingconnectionstring-includingDBname';


/* includes */

/* settings */

/* Schema */

var db = mongoose.connect(mongoConnect);

    /* Socketio */

io.configure(function (){
    io.set('authorization', function (handshakeData, callback) {

    });
});

io.sockets.on('connection', function (socket) {

});//sockets

io.sockets.on('disconnect', function(socket) {
    console.log('socket disconnection')
});

/* The Routing */

app.post('/login', function(req, res){  

});

app.get('/invited', function(req, res){

});

app.get('/', function(req, res){

});

app.get('/logout', function(req, res){

});

app.get('/error', function(req, res){

});

server.listen(port);
console.log('Listening on port '+port);

db.connection.on('error', function(err) {
    console.log("DB connection Error: "+err);
});
db.connection.on('open', function() {
    console.log("DB connected");
});
db.connection.on('close', function(str) {
    console.log("DB disconnected: "+str);
});

我在这里尝试了各种配置,比如一直打开和关闭 - 不过我相信,普遍的共识是像我一样做一个打开包装的东西.??

I have tried various configs here, like opening and closing all the time - I believe though, the general consensus is to do as i am with one open wrapping the lot. ??

我尝试了一个连接测试器,它会不断检查连接状态......即使这似乎表明一切正常 - 问题仍然存在.

I have tried a connection tester, that keeps checking the status of the connection... even though this appears to say everthing's ok - the issue still happens.

我从第一天起就遇到了这个问题.我一直用 MongoLab 托管 MongoDB.本地主机上的问题似乎更糟.但是我在 Azure 和现在 nodejit.su 上仍然有问题.

I have had this issue from day one. I have always hosted the MongoDB with MongoLab. The problem appears to be worse on localhost. But i still have the issue on Azure and now nodejit.su.

因为它无处不在 - 必须是我、MongoDB 或 mongolab.

As it happens everywhere - it must be me, MongoDB, or mongolab.

顺便说一句,我对 php 驱动程序也有类似的经历.(不过要确认这是在 nodejs 上)

Incidentally i have had a similar experience with the php driver too. (to confirm this is on nodejs though)

如果能得到一些帮助,那就太好了——即使有人只是说这是正常的"

It would be great for some help - even if someone just says "this is normal"

提前致谢

罗布

推荐答案

感谢所有帮助人员 - 我已经设法在本地主机和部署到实时服务器上解决了这个问题.

Thanks for all the help guys - I have managed to solve this issue on both localhost and deployed to a live server.

这是我现在工作的连接代码:

Here is my now working connect code:

var MONGO = {
    username: "username",
    password: "pa55W0rd!",
    server: '******.mongolab.com',
    port: '*****',
    db: 'dbname',
    connectionString: function(){
        return 'mongodb://'+this.username+':'+this.password+'@'+this.server+':'+this.port+'/'+this.db;
    },
    options: {
        server:{
            auto_reconnect: true,
            socketOptions:{
                connectTimeoutMS:3600000,
                keepAlive:3600000,
                socketTimeoutMS:3600000
            }
        }
    }
};

var db = mongoose.createConnection(MONGO.connectionString(), MONGO.options);

db.on('error', function(err) {
    console.log("DB connection Error: "+err);
});
db.on('open', function() {
    console.log("DB connected");
});
db.on('close', function(str) {
    console.log("DB disconnected: "+str);
});

我认为最大的变化是使用createConnection"而不是connect"——我以前用过这个,但也许现在这些选项有帮助.这篇文章很有帮助 http://journal.michaelahlers.org/2012/12/building-with-nodejs-persistence.html

I think the biggest change was to use "createConnection" over "connect" - I had used this before, but maybe the options help now. This article helped a lot http://journal.michaelahlers.org/2012/12/building-with-nodejs-persistence.html

老实说,我不太确定为什么要添加这些选项 - 正如@jareed 所提到的,我还发现有些人在MaxConnectionIdleTime"方面取得了成功 - 但据我所知,javascript 驱动程序没有这个选项:这是我试图复制这种行为的尝试.

If I'm honest I'm not overly sure on why I have added those options - as mentioned by @jareed, i also found some people having success with "MaxConnectionIdleTime" - but as far as i can see the javascript driver doesn't have this option: this was my attempt at trying to replicate the behavior.

到目前为止一切顺利 - 希望这对某人有所帮助.

So far so good - hope this helps someone.

更新:2013 年 4 月 18 日 请注意,这是第二个具有不同设置的应用

现在我以为我已经解决了这个问题,但最近在另一个应用程序上问题再次出现 - 使用相同的连接代码.一头雾水!!!

Now I thought i had this solved but the problem rose it's ugly head again on another app recently - with the same connection code. Confused!!!

但是设置略有不同......

However the set up was slightly different…

这个新应用在使用 IISNode 的 Windows 机器上运行.我最初并不认为这很重要.

This new app was running on a windows box using IISNode. I didn't see this as significant initially.

我了解到 Azure (@jareed) 上的 mongo 可能存在一些问题,因此我将数据库移至 AWS - 问题仍然存在.

I read there were possibly some issues with mongo on Azure (@jareed), so I moved the DB to AWS - still the problem persisted.

所以我再次开始玩那个选项对象,阅读了很多关于它的内容.得出这个结论:

So i started playing about with that options object again, reading up quite a lot on it. Came to this conclusion:

options: {
    server:{
        auto_reconnect: true,
        poolSize: 10,
        socketOptions:{
            keepAlive: 1
        }
    },
    db: {
        numberOfRetries: 10,
        retryMiliSeconds: 1000
    }
}

这比我声明的原始选项对象更具教育意义.但是 - 这仍然不好.

That was a bit more educated that my original options object i state. However - it's still no good.

现在,出于某种原因,我不得不离开那个窗口框(与未在其上编译的模块有关) - 移动比再花一周时间试图让它工作更容易.

Now, for some reason i had to get off that windows box (something to do with a module not compiling on it) - it was easier to move than spend another week trying to get it to work.

所以我将我的应用程序移至 nodejitsu.瞧瞧我的连接还活着!哇!

So i moved my app to nodejitsu. Low and behold my connection stayed alive! Woo!

所以……这是什么意思……我不知道!我所知道的是这些选项似乎适用于 Nodejitsu ......对我来说.

So…. what does this mean… I have no idea! What i do know is is those options seem to work on Nodejitsu…. for me.

我相信 IISNode 使用某种永远"脚本来使应用程序保持活动状态.现在公平地说,应用程序不会因为这个而崩溃,但我认为必须有某种不断刷新的应用程序周期"——这就是它如何进行持续部署(ftp 代码,不需要重新启动应用程序)-也许这是一个因素;但我现在只是猜测.

I believe IISNode uses some kind of "forever" script for keeping the app alive. Now to be fair the app doesn't crash for this to kick in, but i think there must be some kind of "app cycle" that is refreshed constantly - this is how it can do continuous deployment (ftp code up, no need to restart app) - maybe this is a factor; but i'm just guessing now.

当然这一切都意味着现在,这还没有解决.它仍然没有解决.它刚刚在我的设置中为我解决了.

Of course all this means now, is this isn't solved. It's still not solved. It's just solved for me in my setup.

这篇关于mongoDB 有重新连接问题还是我做错了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆