I have been running my reactJS website for about one year so far and there's server crashing happened(this start to happen in the last 3 months) when each time I did a deployment(after doing the deployment it works fine but) & on the next day morning when I tried to log in to my website I couldn't access it & I have to restart my PM2 server to make it work again. but from last week my whole AWS instance crashed & I can't find why that happened, from last week this happened to me 2 times. then I have to go to AWS web and restart the server(before I have started using the console but since now I couldn't access it via console have to go to the AWS web and start it again).
also when 1st-time server crashing happens I got that my MySQL server closed the connection since I got it from the logs:
db error Error: Connection lost: The server closed the connection. at Protocol.end (/home/ubuntu/CIAO_ERP/node_modules/mysql/lib/protocol/Protocol.js:112:13) at Socket.<anonymous> (/home/ubuntu/CIAO_ERP/node_modules/mysql/lib/Connection.js:94:28) at Socket.<anonymous> (/home/ubuntu/CIAO_ERP/node_modules/mysql/lib/Connection.js:526:10) at Socket.emit (events.js:215:7) at Socket.EventEmitter.emit (domain.js:475:20) at endReadableNT (_stream_readable.js:1184:12) at processTicksAndRejections (internal/process/task_queues.js:80:21) { fatal: true, code: 'PROTOCOL_CONNECTION_LOST' }
then I have used this solution :stackoverflow answer for handling 'PROTOCOL_CONNECTION_LOST'
then I handled that issue but still, a connection loss appears.
anyway, I'm attaching my connection handling and my server code segments to check whether if I did anything wrong!
SERVER.js
const express = require("express");
const cors = require("cors");
const { DBConnect } = require("./connection");
DBConnect();
const path = require("path");
const app = express();
app.use(express.json({ limit: "50mb", extended: true }));
app.use(
express.urlencoded({ limit: "50mb", extended: true, parameterLimit: 50000 })
);
app.use(cors());
app.use("/api/users", require("./routes/api/users"));
app.use("/api/invoiceSetting", require("./routes/api/InvoiceSetting"));
app.use("/api/auth", require("./routes/api/auth"));
app.use("/api/appSetting", require("./routes/api/AppSetting"));
app.use("/api/StakeHolder", require("./routes/api/StakeHolderSetting"));
app.use("/api/warehouse", require("./routes/api/WarehouseSetting"));
app.use("/api/production", require("./routes/api/ProductionSetting"));
app.use("/api/purchasing", require("./routes/api/Purchasing"));
app.use("/api/purchasingTemp", require("./routes/api/PurchasingTemp"));
//serve static assets if in production
app.use(express.static(path.join(__dirname, "../..", "build")));
const port = process.env.PORTADDRESS || 5000;
app.listen(port, () => console.log(`listening on http://localhost:${port}`)).on(
"error",
(err) => {
console.log(`catched error on listen & error is ${err}\n`);
}
);
here is my DB connection handling code
const mysql = require("mysql");
require("dotenv").config();
let portDB = process.env.PORT_DB;
console.log("PORT ENV VARIABLE:", portDB);
if (portDB === undefined) {
process.exit();
}
var db_config = {
host: process.env.HOST_NAME,
user: process.env.USER_NAME,
password: process.env.PASSWORD,
database: process.env.DATABASE,
multipleStatements: true,
port: portDB,
};
let db = mysql.createConnection(db_config);
const handleDisconnect = () => {
db = mysql.createConnection(db_config);
db.connect((err) => {
// The server is either down
if (err) {
// or restarting (takes a while sometimes).
console.log("error when connecting to db:", err);
console.log("& restarting");
setTimeout(handleDisconnect, 2000); // We introduce a delay before attempting to reconnect,
} // to avoid a hot loop, and to allow our node script to
else {
console.log("Connected Mysql DB!");
}
}); // process asynchronous requests in the meantime.
// If you're also serving http, display a 503 error.
db.on("error", (err) => {
console.log("db error", err);
if (err.code === "PROTOCOL_CONNECTION_LOST") {
console.log(`err.code : PROTOCOL_CONNECTION_LOST appeard\n`);
// Connection to the MySQL server is usually
handleDisconnect(); // lost due to either server restart, or a
} else {
// connnection idle timeout (the wait_timeout
console.log(`db.on("error") else called & err : ${err}\n`);
throw err; // server variable configures this)
}
});
};
module.exports = {
db: db,//this db is used to handling mysql queries
DBConnect: handleDisconnect,//this is used to start the db
};
I would be glad if any help me to solve this server crashing problem or whole AWS instance crashing problem I'm facing right now :(