Node With Mysql
Node With Mysql
Shahid
Founder of Codeforgeek. Technologist. Published Author. Engineer. Content Creator. Teaching Everything I learn!
Node.js and MySQL is one of the necessary binding needed for any web application. MySQL is
one of the most popular open source database in world and ef cient as well. Almost every
popular programming language like Java or PHP provides driver to access and perform
operations with MySQL.
In this tutorial i am trying to cover code for learning and code for production. So if you know this
already and looking for ready made code for production. Click here to jump there directly.
https://codeforgeek.com/nodejs-mysql-tutorial/ 1/26
16/01/2020 Node.js MySQL Tutorial - Codeforgeek
Introduction:
Node.js is rich with number of popular packages registered at package registry called NPM. Most
of them are not so reliable to use for production but there are some on which we can rely upon.
For MySQL there is one popular driver called node-mysql.
In this tutorial, I am going to cover the following points related to Node.js and MySQL.
If you are new to Node and Express then you won’t regret taking our Node course. Its
FREE!
---node_modules
-----+ mysql
-----+ express
---index.js
---package.json
package.json
https://codeforgeek.com/nodejs-mysql-tutorial/ 2/26
16/01/2020 Node.js MySQL Tutorial - Codeforgeek
{
"name": "node-mysql",
"version": "0.0.1",
"dependencies": {
"express": "^4.10.6",
"mysql": "^2.5.4"
}
}
VISIT SITE
npm install
Here is sample code which connects to Database and perform SQL query.
connection.connect();
connection.end();
Make sure you have started MySQL on default port and changed the parameter in above code
then run this code using
node file_name.js
https://codeforgeek.com/nodejs-mysql-tutorial/ 3/26
16/01/2020 Node.js MySQL Tutorial - Codeforgeek
Above code is just for learning purpose and not for production payload. In production scenario is
different, there may be thousands of concurrent users which turns into tons of MySQL queries.
Above code won’t run for concurrent users and here is a proof. Let’s modify our code little bit and
add Express routes in that, here it is.
connection.connect(function(err){
if(!err) {
console.log("Database is connected ... nn");
} else {
console.log("Error connecting database ... nn");
}
});
app.get("/",function(req,res){
connection.query('SELECT * from user LIMIT 2', function(err, rows, fields) {
connection.end();
if (!err)
console.log('The solution is: ', rows);
else
console.log('Error while performing Query.');
});
});
app.listen(3000);
https://codeforgeek.com/nodejs-mysql-tutorial/ 4/26
16/01/2020 Node.js MySQL Tutorial - Codeforgeek
node test.js
https://codeforgeek.com/nodejs-mysql-tutorial/ 5/26
16/01/2020 Node.js MySQL Tutorial - Codeforgeek
In above code, we are allowing it to run for standalone connection i.e one connection at a time
but reality is bit different. You may get 100 or 1000 connection at one particular time and if your
server is not powerful enough to serve those request then at least it should put them in queue.
test.js
function handle_database(req,res) {
pool.getConnection(function(err,connection){
if (err) {
res.json({"code" : 100, "status" : "Error in connection database"});
return;
https://codeforgeek.com/nodejs-mysql-tutorial/ 6/26
16/01/2020 Node.js MySQL Tutorial - Codeforgeek
}
connection.on('error', function(err) {
res.json({"code" : 100, "status" : "Error in connection database"});
return;
});
});
}
app.get("/",function(req,res){-
handle_database(req,res);
});
app.listen(3000);
node test.js
and re 10 concurrent users for 1 minute using siege by using this command.
https://codeforgeek.com/nodejs-mysql-tutorial/ 7/26
16/01/2020 Node.js MySQL Tutorial - Codeforgeek
Here is output.
**UPDATE**
You can directly use pool.query() which internally will acquire connection and release it when
query is executed. In my personal code review experience, majority of the developers often
forget to release the acquired connection which in turns creates bottleneck and database load.
test.js
function handle_database(req,res) {
// connection will be acquired automatically
pool.query("select * from user",function(err,rows){
if(err) {
return res.json({'error': true, 'message': 'Error occurred'+err});
}
app.get("/",function(req,res){-
handle_database(req,res);
});
app.listen(3000);
I have used this function in a production environment with heavy payload and it works like
charm.
Executing Queries
function addRow(data) {
let insertQuery = 'INSERT INTO ?? (??,??) VALUES (?,?)';
let query = mysql.format(insertQuery,["todo","user","notes",data.user,data.value]);
pool.query(query,(err, response) => {
if(err) {
console.error(err);
return;
}
// rows added
console.log(response.insertId);
});
}
setTimeout(() => {
// call the function
addRow({
"user": "Shahid",
"value": "Just adding a note"
});
},5000);
function queryRow(userName) {
let selectQuery = 'SELECT * FROM ?? WHERE ?? = ?';
let query = mysql.format(selectQuery,["todo","user", userName]);
// query = SELECT * FROM `todo` where `user` = 'shahid'
pool.query(query,(err, data) => {
if(err) {
https://codeforgeek.com/nodejs-mysql-tutorial/ 10/26
16/01/2020 Node.js MySQL Tutorial - Codeforgeek
console.error(err);
return;
}
// rows fetch
console.log(data);
});
}
setTimeout(() => {
// call the function
// select rows
queryRow('shahid');
},5000);
Ad
VISIT SITE
If you would like to add multiple rows in the single query, you can pass an array in the values. Like
this.
password : '',
database : 'todolist',
debug : false
});
// update rows
function updateRow(data) {
https://codeforgeek.com/nodejs-mysql-tutorial/ 11/26
16/01/2020 Node.js MySQL Tutorial - Codeforgeek
p ( ) {
let updateQuery = "UPDATE ?? SET ?? = ? WHERE ?? = ?";
let query = mysql.format(updateQuery,["todo","notes",data.value,"user",data.user]);
// query = UPDATE `todo` SET `notes`='Hello' WHERE `name`='shahid'
pool.query(query,(err, response) => {
if(err) {
console.error(err);
return;
}
// rows updated
console.log(response.affectedRows);
});
}
setTimeout(() => {
// call the function
// update row
updateRow({
"user": "Shahid",
"value": "Just updating a note"
});
},5000);
function deleteRow(userName) {
let deleteQuery = "DELETE from ?? where ?? = ?";
let query = mysql.format(deleteQuery, ["todo", "user", userName]);
// query = DELETE from `todo` where `user`='shahid';
pool.query(query,(err, response) => {
if(err) {
console.error(err);
return;
}
// rows deleted
console.log(response.affectedRows);
});
}
https://codeforgeek.com/nodejs-mysql-tutorial/ 12/26
16/01/2020 Node.js MySQL Tutorial - Codeforgeek
setTimeout(() => {
// call the function
// delete row
deleteRow('shahid');
},5000);
You can also call a stored procedure using Node.js. If you don’t have stored procedure created in
MySQL, you can refer to the code below to do the same.
DELIMITER $$
CREATE PROCEDURE `getAllTodo`()
BEGIN
SELECT * FROM todo;
END$$
DELIMITER ;
Here is the code to call the stored procedure from the code.
function callSP(spName) {
let spQuery = 'CALL ??';
let query = mysql.format(spQuery,[spName]);
// CALL `getAllTodo`
pool.query(query,(err, result) => {
https://codeforgeek.com/nodejs-mysql-tutorial/ 13/26
16/01/2020 Node.js MySQL Tutorial - Codeforgeek
if(err) {
console.error(err);
return;
}
// rows from SP
console.log(result);
});
}
setTimeout(() => {
// call the function
// call sp
callSP('getAllTodo')
},5000);
Final comments :
Siege is a really powerful tool for the testing server under pressure. We have created 100
connection limit in code, so you might be wondering that after 100 concurrent connection code
will break. Well, let me answer it via code. Fire 1000 concurrent user for 1 minute and let’s see
how our code reacts.
If your MySQL server is con gured to handle such traf c at one socket then it will run and our
code will manage the scheduling of concurrent connection. It will serve 100 connection time but
rest 900 will be in the queue. So the code will not break.
Conclusion :
MySQL is one of a widely used database engine in the world and with Node it really works very
well. Node-MySQL pooling and event-based debugging are really powerful and easy to code.
Further reading:
Node-mysql Github
https://codeforgeek.com/nodejs-mysql-tutorial/ 14/26
16/01/2020 Node.js MySQL Tutorial - Codeforgeek
Pro MERN Stack: Full Stack Web App Development with Mongo, Express, React, and Node
Sponsored
2,3 BHK In North BLR For Less Than ₹ 36 L. Near SEZ, Aerospace Park.
Brigade Group
LOG IN WITH
OR SIGN UP WITH DISQUS ?
Name
t h dl f ti ( ){
https://codeforgeek.com/nodejs-mysql-tutorial/ 15/26
16/01/2020 Node.js MySQL Tutorial - Codeforgeek
see more
△ ▽ • Reply • Share ›
https://github.com/sequeliz...
△ ▽ • Reply • Share ›
i am now wondering why the mariaDB people felt like they needed their own
node-interface.
I took it a step further and put the createConnection and connect within the app.get function
and did some stress testing using an external RDS mysql. I found that straight
createConnection vs createPool connection method results in almost no transaction rate
difference (390trans/sec on a siege -c200 -t30s -d1
That leads me to really question the benefit of that added pool code complexity. I get the
idea of reusing a pooled cached connection but not sure where the real benefit is?
I tried but was unable to even break the straight app.get {createConnection / connect /
query / end} type connection without pooled connections.
△ ▽ • Reply • Share ›
Hope it help.s
△ ▽ • Reply • Share ›
connection.query doesnt work when i set a sleep() function as it: (Any idea?)
connection.query('UPDATE......')
sleepFor(2000);
https://codeforgeek.com/nodejs-mysql-tutorial/ 18/26
16/01/2020 Node.js MySQL Tutorial - Codeforgeek
function sleepFor( sleepDuration ){
var now = new Date().getTime();
while(new Date().getTime() < now + sleepDuration){ /* do nothing */ }
}
△ ▽ • Reply • Share ›
connection.query doesnt work when i set a sleep() function as it: (Any idea?)
connection.query('UPDATE......')
sleepFor(2000);
ak • 3 years ago
i am facing problem with node and mysql
whenever i fetch data mysql which is having more than 30k rows and display the result as
json .
node is not performing well.
i have to get the mysql result in array , so there is will be for loop which is blocking event
△ ▽ • Reply • Share ›
So my point is, there's no sense of creating pooled mysql connections, since each server
will only use once at a time, right?
Actually I came to this conclussion after running the following test with ab:
ab -c 1000 -t 30 localhost:7777/test
Knowing this isn't it better to have just one direct connection for each nodejs server?
https://codeforgeek.com/nodejs-mysql-tutorial/ 19/26
16/01/2020 Node.js MySQL Tutorial - Codeforgeek
Knowing this, isn t it better to have just one direct connection for each nodejs server?
The main problem with your code, is that you are doing connection.end();
You can (and must) leave it open for future requests, that's the reason your siege is failing...
△ ▽ • Reply • Share ›
This is the awesome question so far and let me answer it with my understanding.
First, about the non-concurrent server, I believe you meant single thread ? That's
true but let's get to the point of threading a little bit later.
Regarding MySQL pool, what is happening is Node does accept a lot of connection
at once and generate thread inside for the various different ops, in this case, MySQL
queries. So you have to say 1000 connections and all of them want to query 1000
SELECT queries.
If I don't use Pool and just one connection, then I have to do this.
This will block your event loop, we want to execute 1000 queries in parallel. But,
MySQL does execute them in some queuing manner which is not our concern at
this moment. With pool, I can create 100 connections at a time, and a minute
MySQL completes one SQL query, I am releasing it to Pool so that same can be
reused (Performing connection does take time and HTTP request).
Hope I answered this part. Regarding threading, libev (event loop of Node) performs
the internal thread management by giving programmer one single thread. So for
each I/O, Network call there are different threads (or maybe it reuse some, not
sure).
△ ▽ • Reply • Share ›
if(err){
connection.release();
log.error('Connecting to database')
context.fail(null);
}
https://codeforgeek.com/nodejs-mysql-tutorial/ 20/26
16/01/2020 Node.js MySQL Tutorial - Codeforgeek
Thank you so much sensei.
△ ▽ • Reply • Share ›
if(err){
log.info('Resolving the query')
context.fail(err);
}
log.info('Connection succefull')
t t d( ll)
see more
△ ▽ • Reply • Share ›
C:\Users\Cres\NodeJS\DatabaseTest>node test
{ [Error: ER_BAD_DB_ERROR: Unknown database 'nodejs']
Sponsored
https://codeforgeek.com/nodejs-mysql-tutorial/ 22/26
16/01/2020 Node.js MySQL Tutorial - Codeforgeek
CodeFund has
paid out over
$170K to open
source projects in
2019. Do you
qualify? 🤔
ethical ad by CodeFund
Looking for a
Node.js developer?
Hire Now
Subscribe to our
Newsletter
Your name
https://codeforgeek.com/nodejs-mysql-tutorial/ 23/26
16/01/2020 Node.js MySQL Tutorial - Codeforgeek
Submit
report this ad
Related Articles
https://codeforgeek.com/nodejs-mysql-tutorial/ 24/26
16/01/2020 Node.js MySQL Tutorial - Codeforgeek
Name
https://codeforgeek.com/nodejs-mysql-tutorial/ 25/26
16/01/2020 Node.js MySQL Tutorial - Codeforgeek
Submit
Follow us
Top Articles
Top Courses
Links
- About Us
- Services
- Contact Us
- Terms Services
- Privacy & Policy
Links
- Advertise with us
- Best Programming Tutorial
- Resources
- Start Here
Built with love using Node.js. Checkout our Services page for details.
https://codeforgeek.com/nodejs-mysql-tutorial/ 26/26