Node.Js-错误:连接丢失:服务器关闭了连接

时间:2020-03-12 09:32:16

标签: mysql node.js server xampp connection

两个星期前,作为新工作的一部分,我部署了我的第一个应用程序(Woo Hoo!:)),但是自从部署之后,每天晚上它都崩溃了。 服务器端是使用Node.js构建的,我正在使用XAMPP和MYSQL来运行数据库。 所以这是我不断得到的错误: enter image description here

我在寻找解决方案,并找到以下代码:

var connection;
function handleDisconnect() {
    connection = mysql.createConnection(con); // 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();

我将这段代码粘贴到createConnection函数之后:

var con = mysql.createConnection({
    host: "EXAMPLE FOR STUCK OVER FLOW",
    user: "EXAMPLE FOR STUCK OVER FLOW",
    password: "EXAMPLE FOR STUCK OVER FLOW",
    database: "EXAMPLE FOR STUCK OVER FLOW"
});

非常感谢您的建议!

谢谢!

0 个答案:

没有答案