Node.js Connect Mysql with Node app
Last Updated :
17 Oct, 2024
Node.js is a powerful platform for building server-side applications, and MySQL is a widely used relational database. Connecting these two can enable developers to build robust, data-driven applications.
In this article, we'll explore how to connect a Node.js application with a MySQL database, covering the necessary setup, configuration, and basic CRUD (Create, Read, Update, Delete) operations.
Prerequisites:
- Node.js: Download and install from nodejs.org.
- MySQL: Download and install from mysql.com.
- MySQL Workbench (optional): A GUI tool for MySQL, helpful for database management
Approach
To Connect MySQL with the Node App we will be using the mysql npm package. After installing, configure the database credentials and establish the connection. Connect the database, handle errors perform queries to interact with the MySQL database using the Node App.
Steps to Connect MySQL with Node App
Step 1: Open your terminal and create a new directory for your project.
mkdir node-mysql-app
cd node-mysql-app
Step 2: Initialize a new Node.js project:
npm init -y
Step 3: Install MySQL Package
Install the mysql
package, which is a Node.js driver for MySQL:
npm install mysql
Step 4: The updated dependencies in package.json file will look like
"dependencies": {
"mysql": "^2.18.1"
}
Step 5: Database Connection
Create a Javascript file named server.js in the root of the project folder. Code for creating connection is as given below:
JavaScript
// Filename - server.js
// importing mysql module
const mysql = require('mysql');
// configurations for creating mysql connection
const connection = mysql.createConnection({
host: 'localhost', // host for connection
port: 3306, // default port for mysql is 3306
database: 'test', // database from which we want to connect our node application
user: 'root', // username of the mysql connection
password: 'root' // password of the mysql connection
});
// executing connection
connection.connect(function(err) {
if (err) {
console.log("error occurred while connecting");
} else {
console.log("connection created with mysql successfully");
}
});
Run the file server.js with following command as:
node server.js

Conclusion
Connecting Node.js with MySQL is straightforward and powerful, enabling developers to build data-driven applications with ease. By following the steps outlined in this article, you can set up a basic Node.js application that interacts with a MySQL database, performing essential CRUD operations. This foundation can be expanded to suit more complex application requirements, including advanced querying, data validation, and secure authentication.
Similar Reads
How to Connect Node.js Application to MySQL ? To connect the Node App to the MySQL database we can utilize the mysql package from Node Package Manager. This module provides pre-defined methods to create connections, query execution and perform other database related operations. Approach to Connect Node App to MySQLFirst, initialize the node.js
2 min read
How to Use Connection Pooling with MySQL in Node.js? MySQL is one of the most preferred relational databases, While Node.js is another name for JavaScript runtime environment. While assessing a large number of connections in the database in a Node. In this regard, effectiveness in managing them is also a significant determinant when developing and mai
3 min read
Node.js MySQL CONCAT_WS() Function CONCAT_WS() function is a built-in function in MySQL that is used to concatenate a set of strings with a commonly given separator. Syntax: CONCAT_WS(separator, string_1, string_2, ...)Parameters: It takes two parameters as follows: separator: This separator will be used to concatenate strings.string
2 min read
Node.js MySQL Order By Clause Introduction: We use the SQL ORDER BY Clause to sort the data with respect to some column value in ascending or descending order. Syntax: SELECT * FROM users ORDER BY name; This will sort all rows of output in ascending order (by default) with respect to name column. SELECT address FROM users ORDER
2 min read
How to connect mongodb Server with Node.js ? mongodb.connect() method is the method of the MongoDB module of the Node.js which is used to connect the database with our Node.js Application. This is an asynchronous method of the MongoDB module.Syntax:mongodb.connect(path,callbackfunction)Parameters: This method accept two parameters as mentioned
1 min read
Node.js MySQL FIND_IN_SET() Function FIND_IN_SET() function is a built-in function in MySQL that is used to get the position of the first occurrence of value string in a list of strings separated by comma(','). Syntax: FIND_IN_SET(value, list_of_string)Parameters: It takes two parameters as follows: value: It is the value to be searche
2 min read