DEV Community

Robert Look
Robert Look

Posted on

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

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)

npm install MySQL
Enter fullscreen mode Exit fullscreen mode

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;

Enter fullscreen mode Exit fullscreen mode

Original source: https://www.phpcodingstuff.com/blog/how-to-connect-mysql-database-with-node-js.html

Top comments (0)