2013-11-26 201 views
43

当我使用节点mysql时,在服务器关闭TCP连接的12:00至2:00之间出现错误。这是一个完整的消息:nodejs mysql错误:连接丢失服务器关闭了连接

Error: Connection lost: The server closed the connection. 
at Protocol.end (/opt/node-v0.10.20-linux-x64/IM/node_modules/mysql/lib/protocol/Protocol.js:73:13) 
at Socket.onend (stream.js:79:10) 
at Socket.EventEmitter.emit (events.js:117:20) 
at _stream_readable.js:920:16 
at process._tickCallback (node.js:415:13) 

还有就是solution。但是,在我尝试这样做后,问题也出现了。现在我不知道该怎么做。有人遇到这个问题吗?

这里是我写的遵循解决方案的方式:

var handleKFDisconnect = function() { 
    kfdb.on('error', function(err) { 
     if (!err.fatal) { 
      return; 
     } 
     if (err.code !== 'PROTOCOL_CONNECTION_LOST') { 
      console.log("PROTOCOL_CONNECTION_LOST"); 
      throw err; 
     } 
     log.error("The database is error:" + err.stack); 

     kfdb = mysql.createConnection(kf_config); 

     console.log("kfid"); 

     console.log(kfdb); 
     handleKFDisconnect(); 
    }); 
    }; 
    handleKFDisconnect(); 

回答

93

尝试使用this code来处理服务器断开连接:

var db_config = { 
    host: 'localhost', 
    user: 'root', 
    password: '', 
    database: 'example' 
}; 

var connection; 

function handleDisconnect() { 
    connection = mysql.createConnection(db_config); // Recreate the connection, since 
                // the old one cannot be reused. 

    connection.connect(function(err) {    // The server is either down 
    if(err) {          // or restarting (takes a while sometimes). 
     console.log('error when connecting to db:', err); 
     setTimeout(handleDisconnect, 2000); // We introduce a delay before attempting to reconnect, 
    }          // to avoid a hot loop, and to allow our node script to 
    });          // process asynchronous requests in the meantime. 
              // If you're also serving http, display a 503 error. 
    connection.on('error', function(err) { 
    console.log('db error', err); 
    if(err.code === 'PROTOCOL_CONNECTION_LOST') { // Connection to the MySQL server is usually 
     handleDisconnect();       // lost due to either server restart, or a 
    } else {          // connnection idle timeout (the wait_timeout 
     throw err;         // server variable configures this) 
    } 
    }); 
} 

handleDisconnect(); 

在你的代码,我缺少的部分后connection = mysql.createConnection(db_config);

+0

好的,我会试试这个。但是我怎么能模拟这个siutation – jackieLin

+0

为什么你需要模拟这个? – CloudyMarble

+0

现在的代码是工作。数据库对象不为null,但选择数据库时,它不能执行并始终停止。但服务器不显示任何错误! – jackieLin

22

一个实用的解决方案是强制MySQL保持连接活着:

setInterval(function() { 
    db.query('SELECT 1'); 
}, 5000); 

我更喜欢这种解决方案连接池和处理断开连接,因为它不需要以知道连接存在的方式来构造您的代码。每5秒进行一次查询可确保连接保持活动状态,并且不会发生PROTOCOL_CONNECTION_LOST

此外,此方法可确保您保持相同的连接存活,而不是重新连接。这个很重要。考虑一下如果你的脚本依赖于LAST_INSERT_ID()和mysql连接在你没有意识到的情况下被重置会发生什么?

但是,这只能确保连接超时(wait_timeoutinteractive_timeout)不会发生。如预期的那样,在所有其他情况下,它将失败。因此,请确保处理其他错误。

+0

只是好奇,你会把那个数据库查询放在哪里?在nodejs服务器的底部?唯一的问题是,我只使用mysql来验证用户身份,然后将他们的数据存储在我的RPG游戏的临时用户对象中。我不知道为什么我今天随机得到这个mysql关闭的错误,嗯。我正在关闭连接,等等。 –

+1

您应该连接到数据库并根据需要断开连接。此解决方案适用于连续运行并始终利用数据库连接的服务。 – Gajus

+0

如果是这样的话,是否有可能发生内存泄漏或忘记应用db.end()? –

-1

创建和销毁在每个查询的连接,也许复杂的,我遇到了一些麻烦与服务器迁移时,我决定安装MariaDB的,而不是MySQL的。出于某种原因,在文件etc/my.cnf中,参数wait_timeout的默认值为10秒(这导致无法实现持久性)。然后,解决方案将其设置为28800,即8小时。那么,我希望能帮助这个“güevonada”的人......请原谅我的英文不好。

相关问题