连接丢失:服务器关闭了连接

时间:2019-04-21 08:48:53

标签: mysql node.js heroku cpanel

我在Heroku上部署了我的代码,但是在部署应用程序后“崩溃了”。连接丢失:服务器关闭了连接。 解决方案在这里” nodejs mysql Error: Connection lost The server closed the connection

但是仍然面临同样的问题。 我的代码如下:

const mysql = require('mysql');

var db_config = {
  host: 'myhost',
  user: 'myusername',
  password: 'mypassword',
  database: 'mydatabase'
};

var db;

function handleDisconnect() {

  db = mysql.createPool(db_config); 

  db.getConnection(function(err) {              
    if(err) {                                  
      console.log('error when connecting to db:', err);
      setTimeout(handleDisconnect, 2000); 
    }                                     
  });                                     
  db.on('error', function(err) {
    console.log('db error', err);
    if(err.code === 'PROTOCOL_CONNECTION_LOST') {
      handleDisconnect();                         
    } else {                                      
      throw err;                                 
    }
  });
}

handleDisconnect();

global.db = db;

0 个答案:

没有答案
相关问题