AWS - MySQL - 不断丢失连接

时间:2018-02-08 22:11:00

标签: mysql node.js amazon-rds

我试图在几天内解决这个问题,但没有任何结果。我在RDS上有一个节点服务器mySQL,但我一直在断开连接过夜,服务器关机了。

我尝试添加一个处理断开连接的功能(来自google / stackoverflow),但它似乎没有解决问题

function handleDisconnect() {
  // create new connection
  db = mysql.createConnection(dbIdentity); 
  // re-connect to mySQL and handle err if cant connect
  db.connect(function(err) {              
    if(err) {                                     
      console.log('error when connecting to db:', err);
      setTimeout(handleDisconnect, 2000); // repeat the process
    }                                     
  });                                     

  connection.on('error', function(err) {
    console.log('db error', err);
    if(err.code === 'PROTOCOL_CONNECTION_LOST') { 
      handleDisconnect();                         
    } else {                                      
    throw err;                                  
    }
  });
}

0 个答案:

没有答案
相关问题