-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Connection closes after ~10 seconds #225
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Can you please provide the full output (the stuff generated by |
There's all the output :
|
I too am experiencing this issue, using latest commit (360748d2a5) When binding connection to 'close' and 'error' it seems to unbind when the connection is re-created using the example from the readme Server disconnects. The only way I'm able to keep a steady connection is before each query sending a newConn() function which re-creates the client and re-binds the 'error' and 'close' events so once produced it will not cause a fatal_error. I know this solution is not ideal and there is probably a better way of going about it. Here is my newConn() function for those who need a quick fix. function newConn()
{
connection = mysql.createConnection({
host: 'host',
user: 'user',
password: 'password',
database: 'db',
debug: true
});
connection.on('error', function(err) {
console.log(err.code); // 'ER_BAD_DB_ERROR'
});
connection.on('close', function(err) {
if (err) {
console.log("SQL Connection Closed");
// We did not expect this connection to terminate
// connection = mysql.createConnection(connection.config);
} else {
// We expected this to happen, end() was called.
console.log('Manually called .end()');
}
});
} |
I am not able to reproduce this so far. Can you guys check the timeout settings on your database? Also @ScopeXL are you also using MariaDB?
Check out this page: http://github.github.com/github-flavored-markdown/ (Fenced code blocks)
Of course, but that's the way JavaScript works. If you point variable to a new instance of an object, any previous state (such as event listeners), will not be carried over. Once I have implemented a connection pool, I'll re-work this section, but this is really expected behavior. Also: Your workaround is really bad, nobody should use that - let's get this fixed quickly instead : ). |
I am using MySQL version 5.5.23-55. Here are my timeout variables grabbed from phpMyAdmin. Also my database is MyISAM so the innodb variables probably aren't the issue, better to be thorough however.
|
This is very likely the cause of #225. I was not able to re-produce it on my machine, but reading through this makes me think it is the right thing to do: http://dev.mysql.com/doc/refman//5.5/en/mysql-real-connect.html
@felixge this definitely helped. After testing I am getting intermittent results. The "PROTOCOL_CONNECTION_LOST" still occurs, though at different time intervals. First time after 2:00 minutes, second time after 2:30 minutes, third time after 1:30 minutes The .on('error') and .on('close') help prevent the fatal_error, but only after 1 disconnect, then like you said the event listeners unbind and cause the fatal_error. |
It doesn't change anything for me, the server still closes the connection after 20 secs. EDIT: Just tried with .on('error') and .on('close') event handlers, and the connection closes again (with PROTOCOL_CONNECTION_LOST exception). I can't see the thing ScopeXL mentions. |
@felixge @matteo78 I have implemented a setInterval that fires a null query in an effort to keep the connection open. It's been running all night without a problem. So the issue lies somewhere in the connection being idle for too long I believe. @matteo78 try this and see if your connection still closes. If your connection closes after 20 seconds though I would set your interval to 15000 or lower. setInterval(keepAlive, 60000);
function keepAlive() {
connection.query('SELECT 1');
console.log("Fired Keep-Alive");
return;
} |
Any update? |
I'm honestly not sure what is causing this issue. Could you guys all try node-mysql v1 (npm install mysql) and see if that has the same issue? |
@felixge, looks like I'm experiencing the same issue running node v0.6.19, mysql 5.5.24, and node-mysql 2.0.0-alpha2 on Windows 7 x64. The connection will remain open and idle without any issues for about 8 hours before failing; so testing and debugging is a bit difficult. Let me know what I can do to help debug this. |
@davidmurdoch that's expected on an inactive connection given a wait_timeout setting of |
also experiencing the same issue |
@andremoniz please clarify, people have been reporting different observations here. What exactly is happening in your case. What timings are involved? What do your timing related mysql settings look like? |
In my case, this happened exactly after the wait_timeout value of mysql server. |
Can all of you try my new example for re-connecting lost connections: https://github.com/felixge/node-mysql#server-disconnects ? The old version was broken, sorry about that : /. |
I am still having this issue. I continually get the following, about every 5 seconds starting from the time my node app starts up. I never had any issues with 0.9.6.
Maybe I'm not understanding the new API in v2 though? I've got it setup basically how I did before, is doing it like this (obviously very simplified) not the ideal method anymore? var mysql = require('mysql');
var connection = mysql.createConnection({
user: '***',
password: '***',
host: 'localhost',
port: 1234,
database: '***'
});
function handleDisconnect(connection) {
connection.on('error', function(err) {
if (!err.fatal) {
return;
}
if (err.code !== 'PROTOCOL_CONNECTION_LOST') {
throw err;
}
console.log('Re-connecting lost connection: ' + err.stack);
connection = mysql.createConnection(connection.config);
handleDisconnect(connection);
connection.connect();
});
}
handleDisconnect(connection);
app.get('/', function(req, res){
var user = 'test';
connection.query("SELECT id FROM users WHERE username = ?", [user], function(err, info) {
res.end(info[0].id);
});
});
app.get('/test', function(req, res){
var user = 'test2';
connection.query("SELECT id FROM users WHERE username = ?", [user], function(err, info) {
res.end(info[0].id);
});
}); |
wow i just got the same error Connection lost: The server closed the connection after a few times of settimeouts |
I was having the same issue for a while where the connection closes after about 10-20 seconds. Locally everything functioned fine as I was running the latest node v0.8.15 and [email protected] but on my production environment the node version is set to v0.6.17. I resolved the issue by downgrading to [email protected] and then all worked fine. |
Confirm my conclusion. You're saying this 2 environments work fine:
|
Yes much more concise. |
I'm still having this issue on AppFog, with Node.js 0.8.14. I tried 2.0.0-alpha7, 2.0.0-alpha2 and 2.0.0-alpha5.
|
Try using the new Pool. |
Since I couldn't get it to work, I decide to move to Here's an example using
It could be a good thing to clarify in the documentation, it wasn't that clear (from my point of view). Thanks! |
That's already in the Pool documentation. You don't have to do that on a normal connection, that's probably because AppFog has a very low connection timeout. |
Hello all, I was having the same issues when connecting to mysql. My solution is simple, when an error happen I wait 5 sec and try to reconnect like this:
I am sure this solution could be improved and since i am no expert problably i am not taking some things into accout but i works for me. |
the above code would always stop executing at err.fatal && !err.fatal (just noticed that you commented it out sorry ) |
https://github.com/felixge/node-mysql/search?q=Error%3A+Connection+lost%3A+The+server+closed+the+connection&type=Code |
This thread is from 2012; the code does not even apply to the current version. Use the pool is you want reconnections. |
I'm trying to connect to a database - now using new passwords - the connection is etablished normally, but after approximately 10 seconds, an error shows :
Just using this code :
With my own identifiers.
I've tried to connect with another node module, and it seems to works correctly.
The text was updated successfully, but these errors were encountered: