본문 바로가기
기타2/Nodejs

Error: Connection lost The server closed the connection

by 죠부니 2020. 11. 30.
반응형

uhou.tistory.com/125

 

Connection lost: The server closed the connection 에러 해결하기

 Node.js와 mysql, AWS EC2, S3, Nginx 등을 연동하여 백엔드 작업을 하고있는데 가끔씩 'Error: Connection lost: The server closed the connection.' 이라는 에러로그가 뜨면서 웹 사이트에 접속이 되지 않을..

uhou.tistory.com

stackoverflow.com/questions/20210522/nodejs-mysql-error-connection-lost-the-server-closed-the-connection

 

nodejs mysql Error: Connection lost The server closed the connection

when I use node mysql, an error is appear between 12:00 to 2:00 that the TCP connection is shutdown by the server. This is the full message: Error: Connection lost: The server closed the connectio...

stackoverflow.com

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();
반응형

'기타2 > Nodejs' 카테고리의 다른 글

버전체크 npm-check-updates  (0) 2022.02.17
NodeJS: PM2 Startup on Windows[2022-10-19]  (0) 2021.05.06
크롤링  (0) 2020.10.29
nvm  (0) 2019.11.28
https://nodejs.org/  (0) 2019.11.28