The Wayback Machine - https://web.archive.org/web/20240916071110/https://www.geeksforgeeks.org/node-js-connect-mysql-with-node-app/
Open In App

Node.js Connect Mysql with Node app

Last Updated : 04 Jun, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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

Important Points

  • NodeJs: An open-source platform for executing javascript code on the server side. Also a javascript runtime built on Chrome’s V8 JavaScript engine.
  • Mysql: An open-source Relational Database Management System (RDBMS) that uses Structured Query Language (SQL). It is the most popular language for adding, accessing and managing content in a database. Here we will use the Mysql as a database for our node application.
  • Node -v: It will show node version in our system.
  • Mysql -v: It will show Mysql version in our system.

Create a New Node.js Project

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

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:

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 the components using Connect() in React Redux
In React Redux, you use the connect() function to connect your components to the Redux store. This function takes two parameters: mapStateToProps and mapDispatchToProps. mapStateToProps maps the store's state to the component's props, while mapDispatchToProps maps dispatch actions to props. By using connect( ), your components can access the store'
3 min read
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 project in the particular folder on your machine.D
2 min read
Connect MongoDB with Node App using MongooseJS
The process of integrating MongoDB, a NoSQL database, with a Node.js application using MongooseJS, a MongoDB object modelling tool designed to work in an asynchronous environment. Prerequisites:NodejsnpmMongoDBMongooseJSSteps to connect MongoDB with Node AppFollowing are the steps to connect MongoDB with a Node.js application using MongooseJS: Step
4 min read
How to connect Express application to MongoDB and MySQL?
Express JS is a JavaScript framework that is used for building the backend of web and mobile applications. It works on top of Node JS which is helpful for managing servers and routes. Connecting an Express JS application to MongoDB and MySQL requires installing Node.js in your computer system. In this article, we will see how we can connect the Exp
5 min read
How to Display Flash Messages using connect-flash Module in Node.js ?
Connect-flash module for Node.js allows the developers to send a message whenever a user is redirecting to a specified web-page. For example, whenever, a user successfully logged in to his/her account, a message is flashed(displayed) indicating his/her success in the authentication.Prerequisites Before starting with the application, you must have t
3 min read
Node.js socket.connect() Method
The socket.connect() method is an inbuilt application programming interface of class Socket within dgram module which is used to connect the particular server to a particular port and address. Syntax: const socket.connect(port[, address][, callback]) Parameters: This method accepts the following parameters: port: This parameter holds the port numbe
3 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 above and described below: Path/URL: The Path
2 min read
Node.js tls.connect() Method
The tls.connect() method is an inbuilt application programming interface of class TLS within tls module which is used to create a tls.TLSSocket object. Syntax: const tls.connect(port[, host][, options][, callback]) Parameters: This method accept four parameters as mentioned above and described below: port: It is the port number.host: It is the name
3 min read
Node.js http2.connect() Method
The http2.connect() is an inbuilt application programming interface of class http2 within the http2 module which is used to return a ClientHttp2Session instance. Syntax: const http2.connect(authority[, options][, listener]) Parameters: This method takes the following argument as a parameter: authority: It is the URL representing a remote HTTP/2 ser
3 min read
How to Connect Node.js to Woocommerce API ?
WooCommerce is one of the most popular e-commerce platforms available, powering over 30% of all online stores. It is built on top of WordPress and provides a powerful API for developers to interact with their store data programmatically. If you are building a Node.js application that interacts with a WooCommerce store, you can use the WooCommerce A
4 min read
How to Connect to a MongoDB Database Using the Node.js Driver ?
MongoDB is a popular, open-source, NoSQL (non-relational) database that provides high performance, high availability, and easy scalability. Unlike traditional relational databases, MongoDB stores a JSON-like format called BSON (Binary JSON). In this article, we connect the MongoDB database to your backend application built by Node.js. Setup for Dat
4 min read
How to Connect to a MongoDB Database Using Node.js
MongoDB is a NoSQL database used to store large amounts of data without any traditional relational database table. To connect to a MongoDB database using NodeJS we use the MongoDB library "mongoose". Steps to Connect to a MongoDB Database Using NodeJSStep 1: Create a NodeJS App: First create a NodeJS app using the following command: npm init -ycd f
4 min read
How to Connect Node.js To MongoDB Atlas Using Mongoose?
MongoDB Atlas is a cloud-based database service that offers robust features and scalability for managing our data. Here we will use Express.js for the server framework and Mongoose for interacting with MongoDB. And also we use the Ejs for our front end to render the simple HTML form. In this tutorial, we will go through the process of how to connec
6 min read
How to Connect Mongodb Authentication by Node.js?
MongoDB is a popular NoSQL database that provides high performance, high availability, and easy scalability. In many applications, you need to connect to a MongoDB database with authentication to ensure data security. This article will guide you through the process of connecting to a MongoDB database with authentication using Node.js. These are the
3 min read
How to Connect MongoDB Database in a Node.js Applications ?
Connecting a MongoDB database to a Node.js application is crucial for utilizing MongoDB's robust NoSQL features in server-side development. This involves installing the MongoDB Node.js driver, configuring your app for database connectivity, and managing database operations with asynchronous methods and effective error handling. Prerequisites:Node j
2 min read
How to Connect SQLite3 Database using Node.js ?
Connecting SQLite3 database with Node.js involves a few straightforward steps to set up and interact with the database. SQLite is a self-contained, serverless, zero-configuration, transactional SQL database engine, making it ideal for small to medium-sized applications. Here’s how you can connect and work with SQLite3 using Nodejs: ApproachTo conne
2 min read
How to Connect to Telnet Server from Node.js ?
Connecting to a Telnet server from Node.js involves creating a client that can communicate using the Telnet protocol. Telnet is a protocol used to establish a connection to a remote host to execute commands and transfer data. In Node.js, you can use various packages to facilitate the connection to a Telnet server. One of the most popular packages f
4 min read
How to Connect with Username/Password to MongoDB using native node.js Driver?
MongoDB Atlas is a cloud-based database service that offers robust features and scalability for managing our data. Here we will use Express.js for the server framework and Mongoose for interacting with MongoDB. And also we use the Ejs for our front end to render the simple HTML form. In this tutorial, we will go through the process of how to connec
5 min read
How to connect Node with React ?
In this article, we will see how Node JS and React JS are an important part of developing full-stack web applications, where React is used for the front end and Node for the back end. React JS is an open-source JavaScript library popularly used to create user interfaces for single-page applications (SPAs). It boasts a large and active developer com
3 min read
How to Connect Node to a MongoDB Database ?
To connect node to a MongoDB database we can use mongoose library from npm, It is an Object Data Modeling Library that provides predefined methods for database connection, creating schema and models. MongoDB is a NoSQL database used to store large amounts of data without any traditional relational database table. Instead of rows & columns, Mong
3 min read
Build a Grocery Store Web App using PHP with MySQL
In this article, we are going to build a Grocery Store Web Application using PHP with MySQL. In this application, we can add grocery items by their name, quantity, status (pending, bought, not available), and date. We can view, delete and update those items. There will be a date filtering feature where we can view the grocery items according to the
7 min read
How to make a Todo App using PHP & MySQL ?
To create a Todo App using PHP and MySQL, you'll first need to set up a MySQL database to store the tasks. Then, use PHP to create a web interface where users can add, edit, and delete tasks. PHP will handle the backend logic, such as connecting to the database and performing CRUD operations. Finally, use HTML and CSS to design the frontend interfa
4 min read
Online FIR Web App using PHP with MySQL
In this article, we are going to build an Online FIR Web Application using PHP with MySQL. In this application, we can file cases from anywhere by their name, complaint, status (pending or solved), and date of incidence, date of registration. Only the admin can view, delete and update status. A complete list of all cases can be seen by only the adm
10 min read
Introduction to Xamarin | A Software for Mobile App Development and App Creation
The entire world is now surrounded by billions and trillions of mobile Tech which is inevitable. The major share of the development of mobile apps is taken by the Google's Android, Apple's iOS, and Microsoft's Windows. Every new learner or newbie in Mobile Development Domain finds himself in the dilemma of choosing the platform to start with. They
9 min read
Deploying Your Web App using Azure App Service
Pre-requisite: Introduction to Microsoft Azure | A cloud computing service The majority of Computer Science students start with Web Development or Machine Learning. Now after learning how to create a website, we should learn to deploy it online. By deploying our website, we can share it with others, perform alpha or beta testing, perform live chang
4 min read
Progressive Web App - A Combination of Native and Web App
Web is a wonderful platform. Its omnipresent across devices and operating systems, its user-centered security model, and the fact that neither its specification nor its implementation is maintained by a single company or organization make the web a unique and promising platform to develop software or applications on. In addition to that with its in
9 min read
Difference between Mobile App Testing and Web App Testing
Mobile App Testing: Mobile app testing refers to process of testing application software simply for controlling and handling mobile devices. It is used to test mobile app for its functionality, usability, compatibility, etc. Web App Testing: Web app testing refers to process of testing application software hosted on web to ensure quality, functiona
3 min read
Difference between app.get() and app.post() in Express.js.
In ExpressJS, app.get() and app.post() are two different methods used to handle HTTP requests but for different purposes. app.get() in ExpressJS:app.get() is used to handle incoming GET requests. GET requests are commonly used for fetching data from the server. For example, when you open a website in your browser, you are making a GET request to re
2 min read
Difference between app.use() and app.get() in Express.js
Express.js, a popular web application framework for Node.js, provides a powerful set of methods for routing HTTP requests. Among these methods, app.use() and app.get() are two fundamental functions that serve different purposes. Understanding their differences is essential for building efficient and well-structured Express applications. Prerequisit
4 min read
Create a New React App - npm create-react-app
The create react app command allows us to create a development environment and install all necessary libraries for creating single-page applications using React. The CRA tool is used to set up a new React project quickly, providing a standard structure and configuration. It includes a development server, build scripts, and support for modern JavaSc
3 min read