0

サーバーに node.js をインストールしましたが、動作しています。しかし、しばらくすると次のエラーで停止します。

events.js:77
        throw er; // Unhandled 'error' event
              ^
Error: Connection lost: The server closed the connection.
    at Protocol.end (/var/www/node/node_modules/mysql/lib/protocol/Protocol.js:73:13)
    at Socket.onend (stream.js:79:10)
    at Socket.EventEmitter.emit (events.js:122:20)
    at _stream_readable.js:910:16
    at process._tickCallback (node.js:373:11)
error: Forever detected script exited with code: 8
error: Forever restarting script for 14 time

、およびを使用してポート8000でnode.js を実行します。socket.ionode-mysqlmc

へのファイル パスevents.js/node/lib/events.js.

使用foreverすると、連続して実行できますが、それでもエラーが発生します。スクリプトを再起動するだけです。最善の解決策ではありません (何もないよりはましですが、最悪の解決策かもしれません)。

試してみますuncaughtExceptionが、まだ最善の解決策ではありません。このコード:

process.on('uncaughtException', function (err) {
  console.log('Caught exception: ' + err);
});

できれば助けてください。ありがとう。

4

1 に答える 1

1

mysql 接続でエラー イベントを処理する必要があります。イベント エミッターが「エラー」イベントを発行し、それが処理されない場合、例外がスローされます。コードで何をしているのかわかりませんが、これをどのように処理する必要があるかについては、以下を参照してください。

var mysql      = require('mysql');
var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'me',
  password : 'secret',
});

connection.on('error', function (err) {
    // Handle your error here.
});

connection.connect();

connection.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {
  if (err) throw err;

  console.log('The solution is: ', rows[0].solution);
});

connection.end();

https://github.com/felixge/node-mysql/blob/master/Readme.md#server-disconnectsからの切断を処理する例を次に示します。

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();
于 2013-08-08T01:47:51.777 に答える