I have a Node.js server that is hosting my webpage. I have recently set up a MySQL server and created a connection to the server. When accessing a certain page, it queries the SQL database.
My problem is that if I query the DB, it makes the connection just fine, but it will crash a little later when the server automatically closes the connection. I then tried to use con.end() after the query, but this crashes the second I access the DB. It throws the error below:
at Protocol._validateEnqueue (/home/pi/Documents/node_modules/mysql/lib/protocol/Protocol.js:203:16)
at Protocol._enqueue (/home/pi/Documents/node_modules/mysql/lib/protocol/Protocol.js:138:13)
at Connection.query (/home/pi/Documents/node_modules/mysql/lib/Connection.js:200:25)
at Handshake.con.connect (/home/pi/Documents/PageDB.js:26:9)
at Handshake.<anonymous> (/home/pi/Documents/node_modules/mysql/lib/Connection.js:502:10)
at Handshake._callback (/home/pi/Documents/node_modules/mysql/lib/Connection.js:468:16)
at Handshake.Sequence.end (/home/pi/Documents/node_modules/mysql/lib/protocol/sequences/Sequence.js:83:24)
at Handshake.Sequence.OkPacket (/home/pi/Documents/node_modules/mysql/lib/protocol/sequences/Sequence.js:92:8)
at Protocol._parsePacket (/home/pi/Documents/node_modules/mysql/lib/protocol/Protocol.js:278:23)
at Parser.write (/home/pi/Documents/node_modules/mysql/lib/protocol/Parser.js:76:12) code: 'PROTOCOL_ENQUEUE_AFTER_QUIT', fatal: false }
Close the database connection.
It seems to me that this is caused by the query executing after the con.end() runs. Can someone help me figure out a way to call the end function after the callback has returned with the SQL query data? Or otherwise, have my web server not crash when the connection to the DB is closed automatically? I'm open to either one. Thanks!
Code for the Node.js server is below:
//Create a connection to the db
const con = mysql.createConnection({
host: 'localhost',
user: 'test',
password: 'test',
database: 'test',
});
router.get('/products',(req,res)=>{
con.connect((err) => {
if(err){
console.log('Error relating to connection: ' + err);
return;
}
console.log('Connection established');
con.query('SELECT * FROM ProductList',(err,rows,fields)=>{
if(!err)
res.send(rows);
else
console.log(err);
})
});
con.end(function(err) {
if (err) {
return console.log('error:' + err.message);
}
console.log('Close the database connection.');
});
});
Your problem is that you're con.end is being called right after you connect method. As JS is asynchronous, so it will not wait for connect to end and then continue to end instead it will call connect and put the callback on the event queue and continue to next statement where you are closing your connection. So, what you should do is move your end statement inside the callback. Try using the following code
//Create a connection to the db
const con = mysql.createConnection({
host: 'localhost',
user: 'test',
password: 'test',
database: 'test',
});
router.get('/products', (req, res) => {
con.connect((err) => {
if (err) {
console.log('Error relating to connection: ' + err);
return;
}
console.log('Connection established');
con.query('SELECT * FROM ProductList', (err, rows, fields) => {
if (!err) {
res.send(rows);
con.end(function(err) {
if (err) {
return console.log('error:' + err.message);
}
console.log('Close the database connection.');
});
} else
console.log(err);
})
});
});
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.
Hello when making calls to the mysql server if the connection is not open/disconnected due to a long time of not using it then it will give me an error when I attempt to Re-Connect. I have looked this error up but the responses don't seem to help me in my situation. Here is the error: Cannot enqueue Handshake after already enqueuing a Handshake. Here is the code causing this:
var con = mysql.createConnection({
host: "",
user: "",
password: "",
database: ""
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
con.on('error', function(err) {
console.log(err);
console.log('MYSQL Disconnected');
})
if (con.state == 'disconnected') {
con.connect(function(err) {
if (err) throw err;
});
}
More code is below but is irrelevant due to it checking for authenticated state and querying
The difference I found between mysql.createConnection and mysql.createPool is when the former lost connection to MySQL (timeout), I need to restart the node server to re-establish connection. With create pool, the app only uses mysql connection when it's required and when MySQL timeout I don't need to restart the node.
Try this and see if there's any good:
dbConnectionInfo = {
host: "",
user: "",
password: "",
database: ""
};
var con = mysql.createPool(
dbConnectionInfo
);
con.on('connection', function (connection) {
console.log('DB Connection established');
connection.on('error', function (err) {
console.error(new Date(), 'MySQL error', err.code);
});
connection.on('close', function (err) {
console.error(new Date(), 'MySQL close', err);
});
});
This method I found from a post but I can't remember which one because there are a lot of mysl.createPool I've seen. You may find more methods here
I recently started learning Nodejs to connect to MySQL and i'm having connection issues. I tried using putty to connect and i'm able to do so without any issues
this is my example.route.js file
const express = require('express'),
router = express.Router(),
mysql = require('mysql');
var connection = mysql.createConnection({
host: <ipaddress>,
port: <default Port>,
user: 'root',
password: <given password>,
database: <db name>,
connectionTimeout: 30000
});
connection.connect((error) => {
if (error) {
console.log('error connecting: ' + error);
} else {
console.log('Connected to server');
}
});
connection.query('SELECT * FROM test', function(err, results, fields) {
console.log('results in table: ' + results);
console.log('fields in table: ' + fields); // -1
connection.release();
if (err) throw error;
});
connection.end();
module.exports = router;
i get the following error => Error: connect ECONNREFUSED (Will update with complete error in a few hours)
As i mentioned before i used PUTTY in order to make sure there was an issue when connecting but i was able to connect to the given database name, host with the same user and password.
Not sure if this helps is an ubuntu server with MySQL
Anyone has an idea of why i'm getting the connection error? I would appreciate it the help
Having an odd issue with node mysql. I am running a mysql container (docker) on my local machine with a standard setup. I am able to get past mysql.createConnection, but my code always fails at connection.connect. MySQL Error logs on the container show no attempted/failed connections, and I can connect via Workbench. Has anyone had this experience trying to connect to MySQL via node.
router.get('/page', function(req,res){
const msg = 'My Query from config, tested and confirmed on Workbench';
var connection = mysql.createConnection({
host : '127.0.0.1',
user : 'user',
password : 'pw',
database : 'db',
port : 3306,
debug : true
});
console.log('Connect to server', connection.state); //Doesn't get past this guy
connection.connect(function(err) {
if (err) {
console.log('error connecting: ' + err.stack);
return err;
}
else {
console.log('connected as id ' + connection.threadId);
}
});
middleware.logger.debug('Make Query');
connection.query(msg, function(err, rows, fields) {
if (!err) {
console.log('The solution is: ', rows);
res.json('We got some rows!');
}
else {
console.log('Error while performing Query.', err);
res.json(err);
}
});
console.log('End Connection');
connection.end();
});
Also, should note: I get the same error whether my docker container is running or not.
EDIT: Okay, want to amend my problem, this is actually an issue with express router. Expanded code above.
I'll keep the code simple, but basically, I have a Node.js server running Express. Express is connected to a MySQL database using pooling (and the mysql npm package). This is what my server looks like (I left out the boring requires and whatnot to keep this simple). This is the only routing that the server handles.
server.all('/test', function (req, res, next) {
pool.getConnection(function (err, conn) {
if (err) {
console.log(err);
}
else {
conn.query("select * from spwp_appusers where id=46", function (err, rows) {
conn.release();
if (err) {
console.log(err);
}
else {
res.send(200);
}
});
}
});
next();
});
However, when I run this code, the server tries to execute res.send(200); but breaks and I get the following error:
/usr/lib/node_modules/mysql/lib/protocol/Parser.js:77
throw err; // Rethrow non-MySQL errors
^
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:335:11)
Does anyone know what is happening? Why can't I send the response? Even when I use a callback, I get this error.
I think the problem is with next. try changing next(); inside getConnection()
above error show in my case mismatch my mysql user,password,db name so please check your mysql credential.
var pool = mysql.createPool({
host: 'localhost',
user: 'root',
password: '',
database: 'dbname', multipleStatements: true
});