Mysql
 sql >> Teknologi Basis Data >  >> RDS >> Mysql

Putuskan koneksi database mysql node JS

Anda dapat menggunakan sampel

var dbConfig = {
        host: '----',
        user: '----',
        password: '----',
        database: '----',
        port: ----
    };

var connection;
function handleDisconnect() {
    connection = mysql.createConnection(dbConfig);  // Recreate the connection, since the old one cannot be reused.
    connection.connect( function onConnect(err) {   // The server is either down
        if (err) {                                  // or restarting (takes a while sometimes).
            console.log('error when connecting to db:', err);
            setTimeout(handleDisconnect, 10000);    // 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 onError(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();



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Membuat tabel secara terprogram menggunakan MyBatis dan MySql

  2. Di mana MySQL menyimpan file database di Windows dan apa nama filenya?

  3. Pembuat kueri Laravel - Cara mengelompokkan berdasarkan alias, atau melakukan groupBy mentah

  4. Bagaimana saya bisa melihat berapa banyak koneksi MySQL yang terbuka?

  5. Instalasi MySQL