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

查看:326
本文介绍了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.

code的粗线条:

我不包括code - 我不认为这是必要的。它的工作原理(除了连接辍学)

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);
});

我已经在这里尝试了各种CONFIGS,如开启和关闭所有的时间 - 我相信,虽然,普遍的共识是做就像我跟一个打开包装的地段。 ??

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.

我已经从一开始这个问题。我一直主持了MongoDB的使用MongoLab。
这个问题似乎是在本地主机上雪上加霜。但我仍然有在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.

下面是我现在的工作连接code:

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在连接 - 我曾使用过这一点,但也许是选择现在帮助。这篇文章帮助了很多<一个href=\"http://journal.michaelahlers.org/2012/12/building-with-nodejs-persistence.html\">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日 注意,这是第二个应用程序使用不同的设置

现在我想我有这个解决,但这个问题上升它的丑陋的头再次的另一个应用程序最近 - 具有相同的连接code。困惑!

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)蒙戈,让我感动的DB到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 code起来,不需要重新启动应用程序) - 也许这是一个因素;但我刚才猜测。

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天全站免登陆