54 lines
1.1 KiB
JavaScript
54 lines
1.1 KiB
JavaScript
var mysql = require('mysql');
|
|
var fs = require("fs");
|
|
|
|
var file = null;
|
|
var connectionConfig;
|
|
var connection = null;
|
|
|
|
function handleDisconnect() {
|
|
connection = mysql.createConnection(connectionConfig);
|
|
|
|
connection.connect(function(err) {
|
|
if(err) {
|
|
console.log('error when connecting to db:', err);
|
|
setTimeout(handleDisconnect, 2000);
|
|
}
|
|
else {
|
|
console.log("Connected to MySQL database ( "+connectionConfig.user+"@"+connectionConfig.host+"/"+connectionConfig.database+" )");
|
|
}
|
|
});
|
|
connection.on('error', function(err) {
|
|
console.log('db error', err);
|
|
if(err.code === 'PROTOCOL_CONNECTION_LOST') {
|
|
handleDisconnect();
|
|
} else if(err.code === 'ECONNRESET'){
|
|
handleDisconnect();
|
|
} else{
|
|
throw err;
|
|
}
|
|
});
|
|
}
|
|
|
|
try {
|
|
file = fs.readFileSync("./config.json");
|
|
}
|
|
catch(exception) {
|
|
console.log(exception.toString());
|
|
}
|
|
|
|
if(file != null) {
|
|
var json = JSON.parse(file);
|
|
|
|
connectionConfig = {
|
|
host: json.host,
|
|
database: json.database,
|
|
user: json.user,
|
|
password: json.password
|
|
};
|
|
|
|
handleDisconnect();
|
|
}
|
|
|
|
|
|
module.exports = connection;
|