35 lines
989 B
JavaScript
35 lines
989 B
JavaScript
const dotenv = require("dotenv");
|
|
const mysql = require("mysql2");
|
|
// const mailer = require("./mailer");
|
|
dotenv.config();
|
|
function connect() {
|
|
// var connection = mysql.createConnection
|
|
var connection = mysql.createPool({
|
|
host: process.env.MYSQL_HOST,
|
|
user: process.env.MYSQL_USER,
|
|
password: process.env.MYSQL_PASSWORD,
|
|
database: process.env.MYSQL_DB,
|
|
});
|
|
//console.log(process.env.MYSQL_PASSWORD)
|
|
// connection.connect
|
|
connection.getConnection(async function (err) {
|
|
if (err) {
|
|
console.log(err);
|
|
} else {
|
|
console.log("Ket noi thanh cong DB ");
|
|
}
|
|
});
|
|
connection.on("error", async function (err) {
|
|
console.log("db error", err);
|
|
if (err.code === "PROTOCOL_CONNECTION_LOST") {
|
|
connection.end();
|
|
connect(); // lost due to either server restart, or a
|
|
} else {
|
|
// connnection idle timeout (the wait_timeout
|
|
throw err; // server variable configures this)
|
|
}
|
|
});
|
|
}
|
|
|
|
module.exports.connect = connect;
|