AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

Node.js mysql connection pool example


<< Previous Page
Nodejs tutorial
Next Page >>
Nodejs tutorial

Node.js Tutorial » Node.js mysql connection pool example

How to create a mysql connection pool?

To access a MySQL database with Node.js, you will need a MySQL driver. This tutorial will use "mysql" module, what can be downloaded from NPM.

To download and install the "mysql" module, open the control terminal and run the following:

E:\Users\Your_Name>npm install mysql

Now you have downloaded and installed a mysql database driver.

Once installed this Node.js module can be used to manipulate the MySQL database:

var mysql = require('mysql');

 

Connecting to MySQL database from node.js

Now we are creating a connection using username and password from your MySQL database.

Then, we use the next example code to perform an SQL query:

Example:

var mysql = require('mysql');
var conn = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '',
database: 'todoapp'
});
conn.connect(function(err) {
if (err) {
return console.error('error: ' + err.message);
}

console.log('Connected to the MySQL server.');
});

Save the code above in a file called "demo_test_db_connection.js" and run the file:

Run "demo_test_db_connection.js"
var mysql = require('mysql');

Output: Connected to the MySQL server.

Related subjects:
node.js http module node.js Send Email upload files

 

Tags: node js mysql orm, node js mysql connection pool example, node js mysql web application, node js mysql query, nodejs mysql connection pool, node js mysql crud, mysql npm,
node js mysql query parameters
Can I use node js with mysql?
Which database is best for Node JS?
Does SQL work with node js?
How do I create a node js database in mysql?

 

Node js MySQL connection pool example

Connection pool is similar to a cache in which we store frequently accessed data.

The purpose of this connection pool is to reuse database connections instead of creating a new connection each time there is a data request.

Example:

const mysql = require('mysql');

const pool = mysql.createPool({
connectionLimit : 60, //important
host : 'localhost',
user : 'your_username',
password : 'your_pasword',
database : 'your_db_name',
debug : false
});

pool.query("SELECT * FROM TABLE_NAME",(err, data) => {
if(err) {
console.error(err);
return;
}
// rows fetch
console.log(data);
});

 

Nodejs Query a Database

Nodejs "Query" a Database is used to read or write a MySQL database. This is also named "to query" the database.

Example: Include fs module, move the file to the current folder:
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
con.query(sql, function (err, result) {
if (err) throw err;
console.log("Result: " + result);
});
});

 



node js mysql orm, node js mysql connection pool example, node js mysql web application, node js mysql query, nodejs mysql connection pool, node js mysql crud, mysql npm,
node js mysql query parameters
Can I use node js with mysql?
Which database is best for Node JS?
Does SQL work with node js?
How do I create a node js database in mysql?

Node.js mysql connection pool example - nodejs

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


0
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


Upload Image, display, edit and delete i...
Login Form with Image background...
How to Create an Image with Transparent ...
Portfolio Gallery Website with filtering...
Simple pagination script PHP MySQLi...
Center Image in div...
Image Hover Overlay Fade...
Sticky image / element / div on scroll...
Responsive images...
Create rounded image in HTML CSS...
Add border around image...
Position Text Over an Image HTML CSS res...
Create a Slideshow Images Gallery...
Create a Sticky Sidebar...
Search bar using CSS HTML...
Shrink Navigation Menu on Scroll...
How to Create Accordion HTML Templates...
Dropdown menu in the navigation bar...
Responsive Top Navigation Bar with mobil...
Split horizontal navigation bar...