How To Connect MySQL Database With Express Js (Node.js)


In this tutorial, you will learn how to connect to the MySQL database server from a node.js application or how to connect xampp MySQL with node js. Learn how to access a MySQL database using Node.js

We’ll have a look at connecting with the MySQL module in the Node.js client for MySQL. I'll explain how to use the module to connect to a MySQL database node js connect PHPMyAdmin.

How To Connect MySQL Database With Express Js (Node.js)

Quick Start: How to Use MySQL in Node

First of all, we create a new project or an existing project. create a new project

If you’ve arrived here looking for a quick way to get up and running with MySQL in Node, we’ve got you covered!

Here’s how to use MySQL in Node in few easy steps:

Install the MySQL module open the command prompt and run this code.

npm install MySQL

We will use pool request in MySQL in the node.js project see below:

Pooling connections in accessing a MySQL database using Node.js

The MySQL driver for the node.js module connect MySQL database provides you with a built-in connection pooling feature in the express js. Suppose, you want to create a connection pool with 10 connections see below Learn how to access a MySQL database using Node.js:

const { createPool } = require("mysql");


const pool = createPool({
    host:'localhost',
    user:'root',
    password:'',
    database:'<dbname>',
    connectionLimit:10
});

pool.connect((err) => {
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

module.exports = pool;

Use my app.js connection node js connect PHPMyAdmin ( MySQL ) .

const express = require("express");
const pool = require("database");
const app = express()
const port = 3000

app.get('/', (req, res) => {
  res.send('Hello World! Welcome to phpcodingstuff.com')
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})

I hope it can help you...

Leave a Reply

Your privacy will not be published. Required fields are marked *

We'll share your Website Only Trusted.!!

close