aws linux server crashing after deployment - mysql

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 :(

Related

Node.Js- Error: Connection lost: The server closed the connection

Two weeks ago I deployed my first application as part of my new job (Woo Hoo! :)), but every night since I deployed it- the app crashes.
The server-side was built with Node.js, and I'm using XAMPP and MYSQL to run the DataBase.
So this is the error I continuously get:
I was searching for a solution, and found this code:
var connection;
function handleDisconnect() {
connection = mysql.createConnection(con); // Recreate the connection, since
// the old one cannot be reused.
connection.connect(function(err) { // The server is either down
if(err) { // or restarting (takes a while sometimes).
// console.log('error when connecting to db:', err);
setTimeout(handleDisconnect, 2000); // 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(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();
I pasted this code after the createConnection function:
var con = mysql.createConnection({
host: "EXAMPLE FOR STUCK OVER FLOW",
user: "EXAMPLE FOR STUCK OVER FLOW",
password: "EXAMPLE FOR STUCK OVER FLOW",
database: "EXAMPLE FOR STUCK OVER FLOW"
});
I will really appreciate for your advice!
Thanks!!!

Nodejs, mysql 'connection lost the server closed the connection'

I connected mysql to nodejs. After a certain period of time, you will get the error :
'Connection lost the server closed the connection'.
I need your help.
An error has occurred and we added the function handleDisconnect. However, once a disconnect is resolved, the second error occurs again from Connection lost the server closed the connection.
I wonder why it should be only once and not the second one.
ps: The description may not be smooth using a translator.
This is part of the app.js file
// connection to databases
var mysql_dbc = require('./config/db_con')();
var connection = mysql_dbc.init();
mysql_dbc.test_open(connection);
// Added Code
handleDisconnect(connection);
function handleDisconnect(client) {
client.on('error', function (error) {
if (!error.fatal) return;
if (error.code !== 'PROTOCOL_CONNECTION_LOST') throw err;
console.error('> Re-connecting lost MySQL connection: ' + error.stack);
mysql_dbc.test_open(connection);
});
};
You can try to use this code to handle server disconnect:
var mysql = require("mysql");
var configuration = {
host: "localhost",
user: "root",
password: "mysql",
database: "blog"
};
var connection;
function handleDisconnect() {
connection = mysql.createConnection(configuration);
connection.connect(function(err) {
if (err) {
console.log("error when connecting to db:", err);
setTimeout(handleDisconnect, 2000);
}else{
console.log("connection is successfull");
}
});
connection.on("error", function(err) {
console.log("db error", err);
if (err.code === "PROTOCOL_CONNECTION_LOST") {
handleDisconnect();
} else {
throw err;
}
});
}
handleDisconnect();
You may lose the connection to a MySQL server due to network problems,
the server timing you out, the server being restarted, or crashing.
All of these events are considered fatal errors.
Re-connecting a connection is done by establishing a new connection.
Once terminated, an existing connection object cannot be re-connected by design.
With Pool, disconnected connections will be removed from the pool freeing up space for a new connection to be created on the next getConnection call.

Server NodeJs crashes in idle time and how do I fix it?

My server api is on alwayse alwaysdata.
After x time the server crash.
events.js:183
throw er;
// Unhandled 'error' eventError: Connection lost: The server closed the connection.
at Protocol.end (/home/ec2-user/node_modules/mysql/lib/protocol/Protocol.js:112:13)
at Socket.<anonymous> (/home/ec2-user/node_modules/mysql/lib/Connection.js:97:28)
at Socket.<anonymous> (/home/ec2-user/node_modules/mysql/lib/Connection.js:502:10)
at emitNone (events.js:111:20)
at Socket.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1064:12)
at _combinedTickCallback (internal/process/next_tick.js:139:11)
at process._tickCallback (internal/process/next_tick.js:181:9)
I'm looking at whether this could not be related to a mysql error. but pre-existing posts do not help me. I think the server mysql cut the connection I do not know why.
here I establish the connection:
let con = mysql.createConnection({
host: "alwaysdata.net",
user: "user",
password: "",
database: "database"
});
try {
con.query(check, (err, customer) => {
if (err){
console.log("%s Error on check query",Date());
throw err;
}
try connection pool:
const mysql = require('mysql');
let pool = mysql.createPool(
{
connectionLimit : 100,
host : '172.17.0.1',
port : 3306,
user : 'test',
password : 'test',
database : 'test',
multipleStatements: true
}
);
...
pool.query(sql, params, function(err, rows) {
...
it works stably on my versions of mysql 5.7 and 8
I believe there are two ways you can handle this.
1) Force MySQL to keep the connection alive (not official, but I believe will do the trick).
2) Handle the mysql server disconnect from the Node's point of
view.
For both there is an excellent example here.
Server disconnects
You may lose the connection to a MySQL server due to network problems,
the server timing you out, the server being restarted, or crashing.
All of these events are considered fatal errors, and will have the
err.code = 'PROTOCOL_CONNECTION_LOST'. See the Error Handling section
for more information.
Re-connecting a connection is done by establishing a new connection.
Once terminated, an existing connection object cannot be re-connected
by design.
With Pool, disconnected connections will be removed from the pool
freeing up space for a new connection to be created on the next
getConnection call.
let connection=null;
function handleDisconnect() {
connection = mysql.createConnection({
host: "alwaysdata.net",
user: "user",
password: "",
database: "database"
}); // Recreate the connection, since
// the old one cannot be reused.
connection.connect(function (err) { // The server is either down
if (err) { // or restarting (takes a while sometimes).
console.log('error when connecting to db:', err);
setTimeout(handleDisconnect, 2000); // 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 (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();
setInterval(function () {
connection.query('SELECT 1');
}, 5000);
module.exports = connection;
you export the connection object and use this for the other connection queries.
I have called a Query every 5sec to keep the connection alive, i have tried all other approaches and this works like a charm.
Manish's answer worked for me!
I've been struggling with this for the past two days. I had a nodejs server with mysql db running on my localhost and after migrating to heroku with cleardb addon I came across several issues. I had this code in a config file:
const mysql = require('mysql');
const db = mysql.createConnection({
host: 'host',
database: 'database',
user: 'user',
password: 'password', });
module.exports = db;
I changed it to what Manish mentioned to handle the disconnect.

"Error: Connection lost: The server closed the connection.,uncaughtException: Connection lost: The server closed the connection"

I am getting the error after 8-12 hours of starting my node. I have set a cron for every hour to query my SQL database and keep my connection alive though this is happening.
"stack": ["Error: Connection lost: The server closed the connection.", " at Protocol.end (/path/to/my/file/node_modules/mysql/lib/protocol/Protocol.js:109:13)", " at Socket.<anonymous> (/path/to/my/file/node_modules/mysql/lib/Connection.js:102:28)", " at emitNone (events.js:72:20)", " at Socket.emit (events.js:166:7)", " at endReadableNT (_stream_readable.js:913:12)", " at nextTickCallbackWith2Args (node.js:442:9)", " at process._tickDomainCallback (node.js:397:17)"],
"level": "error",
"message": "uncaughtException: Connection lost: The server closed the connection.",
"timestamp": "2017-09-13T21:22:25.271Z"
Let me explain my code. I have a common mySQL connection code to pass with my all API's. My common database connection code is as below.
var mysql = require('mysql');
var db_connect = (function () {
function db_connect() {
mysqlConnConfig = {
host: "localhost",
user: "username",
password: "password",
database: "db_name"
};
}
db_connect.prototype.unitOfWork = function (sql) {
mysqlConn = mysql.createConnection(mysqlConnConfig);
try {
sql(mysqlConn);
} catch (ex) {
console.error(ex);
} finally {
mysqlConn.end();
}
};
return db_connect;
})();
exports.db_connect = db_connect;
So I am getting this function in all my API's:
db_connect.prototype.unitOfWork = function (sql)
I will query my database in all my API's with the above function as below.
var query1 = "SELECT * FROM table1";
sql.query(query1,function(error,response){
if(error){
console.log(error);
}
else{
console.log(response);
}
})
I had tried several times for more than 2 weeks to solve this issue as this is killing my node and I need to restart it everytime whenever it goes down.
Can anybody get me with the solution?
Put simply, what I need is how to use the below code and put it with my code structure(my common db connection file)?
var db_config = {
host: 'localhost',
user: 'root',
password: '',
database: 'example'
};
var connection;
function handleDisconnect() {
connection = mysql.createConnection(db_config); // Recreate the connection, since
// the old one cannot be reused.
connection.connect(function(err) { // The server is either down
if(err) { // or restarting (takes a while sometimes).
console.log('error when connecting to db:', err);
setTimeout(handleDisconnect, 2000); // 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(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();
Any help?

"Uncaught Error: Received packet in the wrong sequence" with devtools off - Electron + MySQL node driver + Webpack

When I set up a new project using Electron + Webpack + node MySQL my production build is throwing:
Uncaught Error: Received packet in the wrong sequence
The error goes away only if I keep: config.devtools = 'eval' in my production builds, apparently this will result in a larger file size and some performance issues which I would like to avoid.
Why my project / mysql module crashes with devtools set to ''?? I can hardly find similar reports, am I the only one having this issue?
webpack.config.js:
...
if (process.env.NODE_ENV === 'production') {
config.devtool = '' // <-------- mysql will throw Uncaught Error if I omit 'eval'
config.plugins.push(
new webpack.DefinePlugin({
'process.env.NODE_ENV': '"production"'
}),
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
}
})
)
}
home.js:
<script>
var mysql = require('mysql')
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'password',
database: 'EONIC'
})
connection.connect()
connection.query('SELECT * from products', function (err, rows, fields) {
if (err) throw err <---- here will the error happen
console.log(rows)
})
connection.end()
</script>
source of the error in mysql/lib/protocol/Protocol.js at line 272:
if (!sequence[packetName]) {
var err = new Error('Received packet in the wrong sequence.');
err.code = 'PROTOCOL_INCORRECT_PACKET_SEQUENCE';
err.fatal = true;
this._delegateError(err);
return;
}
It could have something to do with the mangle option in the default minimizer of Webpack in combination with the Mysql package for node.
I've faced the same and similar issues without really being able to pin point it.
There are a lot of questions out there related to this issue:
https://github.com/webpack/webpack/issues/3150
https://github.com/Bajdzis/vscode-database/issues/78
https://github.com/mysqljs/mysql/issues/1655
But the best solution I've found is this:
optimization: {
minimizer: [new TerserPlugin({ terserOptions: { mangle: false } })] // mangle false else mysql blow ups with "PROTOCOL_INCORRECT_PACKET_SEQUENCE"
},
It is of Rudijs in the mysql issue threat: https://github.com/mysqljs/mysql/issues/1655#issuecomment-484530654
Hope this helps, give me a shout!