title: Nodejs, mysql 'connection lost the server closed the connection'
body: <p>I connected mysql to nodejs. After a certain period of time, you will get the error :</p>
  
  <blockquote>
    <p><strong>'Connection lost the server closed the connection'</strong>.</p>
  </blockquote>
  
  <p>I need your help.</p>
  
  <p>An error has occurred and we added the function <code>handleDisconnect</code>. However, once a disconnect is resolved, the second error occurs again from <code>Connection lost the server closed the connection</code>. </p>
  
  <p>I wonder why it should be only once and not the second one.</p>
  
  <p>ps: The description may not be smooth using a translator.</p>
  
  <p>This is part of the <code>app.js</code> file</p>
  
  <pre><code>// connection to databases
  var mysql_dbc = require('./config/db_con')();
  var connection = mysql_dbc.init();
  mysql_dbc.test_open(connection);
  
  // Added Code
  handleDisconnect(connection);
  function handleDisconnect(client) {
  
    client.on('error', function (error) {
  
      if (!error.fatal) return;
  
      if (error.code !== 'PROTOCOL_CONNECTION_LOST') throw err;
  
      console.error('&gt; Re-connecting lost MySQL connection: ' + error.stack);
  
      mysql_dbc.test_open(connection);
  
    });
  
  };
  </code></pre>