· 7 years ago · May 06, 2018, 02:12 AM
1var mysql = require('mysql');
2var db_config = {
3 //development
4 // host:'localhost',
5 // user: 'root',
6 // password: '',
7 // database: 'merchantdev',
8 //insecureAuth : true
9
10 //staging
11 host:'abcd',
12 user: '123',
13 password: '***',
14 database: 'db'
15}
16
17var secretKey = "iloveyou";
18
19var connection;
20
21function handleDisconnect() {
22 connection = mysql.createConnection(db_config); // Recreate the connection, since
23 // the old one cannot be reused.
24
25 connection.connect(function(err) { // The server is either down
26 if(err) { // or restarting (takes a while sometimes).
27 console.log('error when connecting to db:', err);
28 setTimeout(handleDisconnect, 2000); // We introduce a delay before attempting to reconnect,
29 } // to avoid a hot loop, and to allow our node script to
30 }); // process asynchronous requests in the meantime.
31 // If you're also serving http, display a 503 error.
32 connection.on('error', function(err) {
33 console.log('db error', err);
34 if(err.code === 'PROTOCOL_CONNECTION_LOST') { // Connection to the MySQL server is usually
35 handleDisconnect(); // lost due to either server restart, or a
36 } else { // connnection idle timeout (the wait_timeout
37 throw err; // server variable configures this)
38 }
39 });
40}
41
42handleDisconnect();
43
44module.exports = {hashkey:secretKey, connection:connection};