I'm using NodeJS with express. I'm store the sessions to a MySQLStore (express-mysql-session)
My question is how can i handle connection error? When it can't connect i want to render a page to the users instead of the "Error: connect ECONNREFUSED 127.0.0.1:3306 at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1137:16) ....." message.
My code:
const MySQLStore = require('express-mysql-session')(session);
(....)
app.use(session({
secret: 'something',
resave: false,
saveUninitialized: false,
store: new MySQLStore({
host: something,
user: something,
password: something,
database: something
}),
cookie: {
maxAge: 7 * 24 * 60 * 60 * 1000
}
}));
With the simple mysqljs i could do it easily because when i use connection.query there is an (error, result) => { ... } where i can handle error but at this MySQLStore i have no clue how to handle connection error.
Solved it, an Express error handler middleware can catch this error too.
Just paste this by the end of the application just before app.listen.
app.use((error, req, res, next) => {
res.status(error.status || 500).render('error', {
msg: 'Please check back later!'
});
});
Be aware that the next parameter needed even if we don't use it because error-handling functions have four parameters in express. If you delete it, it will not work.
Related
I'm running my website with NodeJS, I'm using the Express framework.
Since MySQL is the only database type at my hosting, I went with it.
I created a db.js file:
const mysql = require('mysql');
const con = mysql.createConnection({
host: .........
user: ...........
password: ............
database: ............
});
con.connect(function(err) {
if (err) {
//throw err;
console.error(err.message);
} else {
console.log('Connected Ro MySQL!');
}
});
module.exports = con;
And I using it like:
const db = require(path.join(__dirname, 'db'));
db.query('select * from table where name = ?', request.params.id, (error, result) => {
if (error) {
response.send(error.message);
} else {
//My Render Stuffs Here
});
}
});
My website works fine however sometimes let's say once every 2 weeks there is a MySQL connection reset, I think the database not available for a minute or so for some reason, and then my website crashes, I have to manually restart NodeJS what is very annoying.
Error in the console:
node:events:355
throw er; // Unhandled 'error' event
^
Error: read ECONNRESET
at TCP.onStreamRead (node:internal/stream_base_commons:211:20)
[...]
errno: -4077,
code: 'ECONNRESET',
syscall: 'read',
fatal: true
}
How should I change my codes to prevent crashing? I replaced throw err; with the console.error(err.message); but it only works when I start the website, not when a connection reset happens at runtime.
Found the solution: Replace createConnection with createPool and totally remove con.connect since createPool doesn't need it. That's it, now it's not crashes when the db unavailable.
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.
I have created a mysql database on google cloud that I'd like to access from a separate node web application (also running on google cloud). I am testing the connection locally on my computer first, and when I run the following code locally I can successfully establish a connection to my database and see the data in it.
'use strict';
// [START app]
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const app = express();
const mysql = require('mysql');
var connection = mysql.createConnection({
host : 'Cloud SQL IP',
user : 'username',
password : 'password',
database : 'db_name'
});
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json())
// Make globals.js accessible
app.use(express.static(__dirname + '/'));
app.get('/', (req, res) => {
connection.connect();
connection.query('SELECT * FROM Users', function (error, results, fields) {
if (error) throw error;
console.log(results);
});
connection.end();
res.status(200).send('Hello World!');
});
app.get('/login', (req, res) => {
res.status(200).send();
});
// [START server]
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`App listening on port ${PORT}`);
console.log('Press Ctrl+C to quit.');
});
// [END app]
However when run this same code in my google app engine (for both debugging on port 8080 and fully deployed on https://myapp.appspot.com) I get the following timeout error:
{ Error: connect ETIMEDOUT
at Connection._handleConnectTimeout (/home/megan_cooper2900/project/node_modules/mysql/lib/Connection.js:419:13)
at Socket.g (events.js:292:16)
at emitNone (events.js:86:13)
at Socket.emit (events.js:185:7)
at Socket._onTimeout (net.js:338:8)
at ontimeout (timers.js:386:14)
at tryOnTimeout (timers.js:250:5)
at Timer.listOnTimeout (timers.js:214:5)
--------------------
at Protocol._enqueue (/home/megan_cooper2900/project/node_modules/mysql/lib/protocol/Protocol.js:145:48)
at Protocol.handshake (/home/megan_cooper2900/project/node_modules/mysql/lib/protocol/Protocol.js:52:23)
at Connection.connect (/home/megan_cooper2900/project/node_modules/mysql/lib/Connection.js:130:18)
at app.get (/home/megan_cooper2900/journeyma/app.js:31:13)
at Layer.handle [as handle_request] (/home/megan_cooper2900/project/node_modules/express/lib/router/layer.js:95:5)
at next (/home/megan_cooper2900/project/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/home/megan_cooper2900/project/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/megan_cooper2900/project/node_modules/express/lib/router/layer.js:95:5)
at /home/megan_cooper2900/project/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/home/megan_cooper2900/project/node_modules/express/lib/router/index.js:335:12)
errorno: 'ETIMEDOUT',
code: 'ETIMEDOUT',
syscall: 'connect',
fatal: true }
Why is this not working on the Google App Engine application?
In your connection configuration for mysql,host does not work on App Engine. You have to use socketPath . socketPath is the path to a unix domain socket to connect to. When used host and port are ignored. (transferred knowledge from using Loopback on App Engine flex. it had me banging my head for days lol). It's value is your Cloud SQL Instance connection name
so in your case, it should look like this: /cloudsql/my-project-12345:us-central1:mydatabase
var connection = mysql.createConnection({
socketPath : '/cloudsql/my-project-12345:us-central1:mydatabase',
user : 'username',
password : 'password',
database : 'db_name'
});
It's a similar process if you're using Postgres on GCloud which is answered here
I also did faced the same issue and I was using Kubernetes pods to access my CloudSQL instance. I got a fix by increasing the timeout in the configuration.
cloudSqlConfig: {
connectionLimit: 10,
host: 'your-host-ip',
user: process.env.DB_USERNAME,
password: process.env.DB_PASSKEY,
database: 'myDB',
connectTimeout: 20000,
waitForConnections: true,
queueLimit: 0
},
I need t o provide the MySQL connection for modules, so I provide the code like this:
var express = require('express');
var mysql = require('mysql')
var app = express();
var connection = mysql.createConnection({
host: '149.xxx.xx.x',
user: 'User',
password: 'Password',
database: 'DataBase',
port: 1443,
});
connection.connect(
function(error) {
console.log(error.code); // 'ECONNREFUSED'
console.log(error.fatal); // true
console.log(error.sql);
console.log(error.sqlMessage);
}
);
and after some time (about 1-2 minutes) I received an error from those console.logs:
ECONNRESET
true
undefined
undefined
I think maybe it's the timeout because of my "nonactivity", but when I'm trying to do the query the errors are the same.
Have any idea what is wrong? I checked it from DataGrip and the data are correct.
Ok, I found one small bug here. The database is MS SQL Server (microsoft). I'm trying now using the library called mssql to connect to that database, provide some code:
var config = {
server: "149.xxx.xx.x",
database: "Database",
user: "User",
password: "Password",
connectionTimeout: 300000,
requestTimeout: 300000,
pool: {
idleTimeoutMillis: 300000,
max: 100
}
};
function getEmp() {
var connection = new sql.Connection(config);
var req = new sql.Request(connection);
connection.connect(function (error) {
if(error) {
console.log(error);
return;
}
req.query('Procedure', function(err, data) {
if (err) {
console.log(err);
} else {
console.log(data);
}
connection.close();
});
});
}
getEmp();
and I receive the error:
{ ConnectionError: Failed to connect to 149.xxx.xx.x:1433 - connect ETIMEDOUT 149.xxx.xx.x:1433
at Connection.<anonymous> (/Users/phuzarski/Learning/NodeJS/srv-express/node_modules/mssql/lib/tedious.js:353:25)
at Connection.g (events.js:292:16)
at emitOne (events.js:96:13)
at Connection.emit (events.js:188:7)
at Connection.socketError (/Users/phuzarski/Learning/NodeJS/srv-express/node_modules/tedious/lib/connection.js:791:12)
at Socket.<anonymous> (/Users/phuzarski/Learning/NodeJS/srv-express/node_modules/tedious/lib/connection.js:33:15)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at emitErrorNT (net.js:1277:8)
at _combinedTickCallback (internal/process/next_tick.js:80:11)
name: 'ConnectionError',
message: 'Failed to connect to 149.xxx.xx.x:1433 - connect ETIMEDOUT 149.xxx.xx.x:1433',
code: 'ESOCKET' }
For sure the data are correct- DataGrip is connecting fine here.
I found at google the similar problem, the problem was Disabled TCP/IP. I checked this one and it's Enabled with this port 1443.
I wrote a connector to get the data from the mysql and when iam running the Mysql connector inside a route it is showing error and the browser is running into a infinite loop.
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.json());
//var mongoose = require('mongoose');
//Connect to Mongoose
//mongoose.connect('mongodg://localhost/malwares');
//var db = mongoose.connection()
//Connect to Mysql
var mysql = require('mysql');
var connection = mysql.createConnection({
host : '192.168.150.94',
user : 'root',
password : 'negalkgkgkal',
database : 'dbnamehere'
});
connection.connect();
//connection.query('SELECT * from detection', function(err, rows, fields) {
//if (!err)
//console.log('The solution is: ', rows);
//else
// console.log('Error while performing Query.');
//});
//connection.end();
//Mysql connection ends
app.get('/' , function(req ,res){
res.send('Goto /api/malware for detection results');
});
app.get('/malwares' , function(req , res){
console.log('Hello');
connection.query('SELECT * from rest', function(err, rows, fields) {
if (!err)
console.log('The solution is: ', rows);
else
console.log('Error while performing Query.');
});
});
connection.end()
app.listen(3000);
console.log('Listening on port 3000');
Error Log
Listening on port 3000
Hello
Error while performing Query.
And also the browser is not responding after this.
I even tried using node-inspector it gave me error like this protocol-enqueue-after-fatal-error-in-node-mysql
Any suggestions on how to solve this error?
P.S :: Database is there and also the table exists,for this purpose of error debugging i have queried another table which i commented in the code and it ran succesfully.
Error:
{ Error: Cannot enqueue Query after invoking quit.
at Protocol._validateEnqueue (/var/www/Rest/node_modules/mysql/lib/protocol/Protocol.js:202:16)
at Protocol._enqueue (/var/www/Rest/node_modules/mysql/lib/protocol/Protocol.js:135:13)
at Connection.query (/var/www/Rest/node_modules/mysql/lib/Connection.js:208:25)
at /var/www/Rest/app.js:39:13
at Layer.handle [as handle_request] (/var/www/Rest/node_modules/express/lib/router/layer.js:95:5)
at next (/var/www/Rest/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/var/www/Rest/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/var/www/Rest/node_modules/express/lib/router/layer.js:95:5)
at /var/www/Rest/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/var/www/Rest/node_modules/express/lib/router/index.js:335:12) code: 'PROTOCOL_ENQUEUE_AFTER_QUIT', fatal: false }
If I am not mistaken you don't need connection.connect() part if connection is defined in same file (like you have), you try commenting out that line and then run it.
I know, I encountered some problems because of that