sql >> Database >  >> RDS >> Mysql

nodejs mysql Fout:Verbinding verbroken De server heeft de verbinding verbroken

Probeer deze code te gebruiken om de verbinding met de server af te handelen:

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();

In je code mis ik de delen na connection = mysql.createConnection(db_config);



  1. Hoe Ruby on Rails configureren met Oracle?

  2. Wat is de JDBC-tegenhanger van het \connect-commando van Postgres?

  3. Algemene fout:OS-versie komt niet overeen

  4. sql-server selecteer de eerste rij van een groep