ItSolutionStuff.com

Node JS Mysql Connection Example

By Hardik Savani • March 25, 2021
MySql Node JS

Hello,

This is a short guide on node js mysql connection example. you will learn how to connect mysql database in node js. we will help you to give example of how to use mysql in node js. step by step explain mysql connection in node js.

In this example, i will give you simple example of how to connect mysql database using node js. i am not going to explain you in more details, but give you simple example, so let's see example.

Install MySQL

you have to install mysql using bellow npm command:

npm install mysql

server.js : Mysql Database Connection

var mysql = require('mysql');

var connection = mysql.createConnection({

host: "localhost",

database : 'laravel8_blog',

user: "root",

password: "root"

});

connection.connect(function(err) {

if (err) throw err;

console.log("Connected!");

});

connection.query('SELECT * FROM events', function (error, results, fields) {

if (error)

throw error;

results.forEach(result => {

console.log(result);

});

});

connection.end();

now you can run project by following command:

node server.js

you will see layout as like bellow:

I hope it can help you...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Node JS Convert Image File to Base64 String Example

Read Now →

Node JS Resize Image Before Upload using Multer Sharp

Read Now →

Multiple File Upload in Node JS using Multer Example

Read Now →

Node js Express Multiple Image Upload using Multer Example

Read Now →

File Upload in Node JS using Multer Example

Read Now →

Node js Express Image Upload using Multer Example

Read Now →

How to add 1 day to the date column in MySQL?

Read Now →

PHP AngularJS MySQL Pagination Example

Read Now →

How to Upgrade Node.js Version in Ubuntu?

Read Now →