Open In App

How to connect Node.js application to MySQL ?

Last Updated : 31 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Node.js is an open-source and cross-platform runtime environment built on Chrome’s V8 engine that enables us to use JavaScript outside the browser. Node.js helps us to use build server-side applications using JavaScript. In this article, we will discuss how to connect the Node.js application to MySQL. For connecting the node.js with MySQL database we need a third-party mysql module.

Approach:

  • First, initialize the node.js project in the particular folder on your machine.
  • Download the mysql module in the project folder.
  • After this create a connection to the database with the createconnection() method of the MySQL module.

The above approach is discussed below:

Step 1: Create a NodeJS Project and initialize it using the following command:

npm init

Step 2: Install the mysql modules using the following command:

npm install mysql

File Structure: Our file structure will look like the following:

Mysql database Structure:

 

Javascript




// Importing module
const mysql = require('mysql')
 
const connection = mysql.createConnection({
    host: "localhost",
    user: "root",
    password: "Aayush",
    database: "aayush"
})
 
// Connecting to database
connection.connect(function (err) {
    if (err) {
        console.log("Error in the connection")
        console.log(err)
    }
    else {
        console.log(`Database Connected`)
        connection.query(`SHOW DATABASES`,
            function (err, result) {
                if (err)
                    console.log(`Error executing the query - ${err}`)
                else
                    console.log("Result: ", result)
            })
    }
})


Run the index.js file using the below command:

node index.js

Console Output:


Previous Article
Next Article

Similar Reads

Node.js Connect Mysql with Node app
In this article, we will learn how to connect the Mysql database to the NodeJs application. Before we go deep into the coding part, let’s get a brief introduction about these technologies: NodeJs: An open-source platform for executing javascript code on the server side. Also a javascript runtime built on Chrome's V8 JavaScript engine. It can be dow
3 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 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 multiple MySQL databases on a single webpage ?
This article explains how to connect multiple MySQL databases into a single webpage. It is useful to access data from multiple databases. There are two methods to connect multiple MySQL databases into a single webpage which are: Using MySQLi (Improved version of MySQL)Using PDO (PHP Data Objects) Syntax: MySQLi Procedural syntax: $link = mysqli_con
2 min read
Connect PHP to MySQL
What is MySQL? MySQL is an open-source relational database management system (RDBMS). It is the most popular database system used with PHP. Structured Query Language (SQL). The data in a MySQL database are stored in tables that consist of columns and rows. MySQL is a database system that runs on a server. MySQL is ideal for both small and large app
3 min read
Nodejs - Connect MongoDB with Node app using MongooseJS
Connect the MongoDB database using MongooseJS to our NodeJS application Before we dive into looking how the mongoDB connects with a nodejs app using mongooseJS, lets get a brief intro to these technologies which are ruling the web development domain of today. Node: Node.js (Node) is an open source development platform for executing JavaScript code
3 min read
How to connect to Telnet server from Node.js ?
In this article, we will see how to connect the Telnet server with the Node.js application. Telnet: The telnet is an application protocol over the Transmission Control Protocol (TCP) used on the internet or local area network to provide a bidirectional interactive text-oriented communication facility using a virtual terminal connection. Telnet is u
3 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