Node JS Mysql Connection Example

About Us:

We are from free coder. We are a group of web developers who are passionate about web development. You can learn from us php, laravel, node js, vue js, react js and many other topic on web developement.
we are very happy to help you.
free coder

Today what you are going to learn:

Hello Brother,

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

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

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:

thank you very much for following up with me.

Tags

Recent Posts

Related Posts