Node.js MySQL Connection Step By Step

If you want to use the MySQL server with your node.js App then you have to learn the node.js MySQL connection. So, here I will tell you How to Connect Node.js to MySQL Database. Once you complete this tutorial, you will easily configure it in your application.

node.js mysql connection

Connect MySQL Database to Node.js App

NPM provides a MySQL module to create a database connection. But you will have to configure the following steps to use it with Node.js.

Important Notes –

If you want to create a database with the help of the MySQL command then you will have to install the MySQL package in your system.

If you are working with PHP & MySQL, you need not install MySQL. Your MySQL Module will work in Node.js by just starting  PHP Xampp/Wamp server.

1. Install MySQL Module

You have to install MySQL Module using the following command line

npm install mysql

2. Include MySQL Module

You have to also include the MySQL module in your Node.js app using the following line of code

var mysql= require('mysql')

If you forget to install & include the MySQL module,  then the SQL query will not work. So, you must always make sure that It must exist in your Node.js app.

3. Create a Database

To create a database, you will have to configure the following points –

  • First of all, Include the MySQL module
  • Create a connection with MySQL
  • Declare  host, user & password with the valid credentials
  • Write SQL query to create a database within the connection method

Query –

var mysql = require('mysql'); 
var conn = mysql.createConnection({ 
  host: "localhost",
  user: "yourusername", 
  password: "yourpassword" 
}); 
conn.connect(function(err) { 
  if (err) throw err; 
  conn.query("CREATE DATABASE nodeapp",
  function (err, result) { 
    if (err) throw err; 
    console.log("Database is created successfully");
     }); 
});

 

If you are working with PHP, You need not create a MySQL database connection using the above script. You can create a MySQL database in the localhost/PHPMyAdmin and use it for the Node.js MySQL Connection.

4. Create Database Connection

You have to create a database connection with the help of the following steps –

  • First, Include the MySQL Module and assign it to mysql
  • Create a connection and assign it to conn
  • Define host, user, password & database name with the valid connection details.
  • Access connect() method from the  conn
  • If the database is not connected successfully, then return an error message
  • At last export connection

Query –

var mysql = require('mysql'); 
var conn = mysql.createConnection({
    host: 'host_name', 
    user: 'database_username', 
    password: 'database_password',
    database: 'database_name' 
}); 
conn.connect(function(err) { 
  if (err) throw err;
  console.log('Database is connected successfully !'); 
}); 
module.exports = conn;

Now, you can use this connection query directly in your project if you want to develop your project with Node.js & MySQL. Even It can quickly execute all kinds of SQL queries.

How to Connect Node.js to MySQL Database

File Name – database.js

var mysql = require('mysql');
var conn = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password: '',
  database: 'codingstatus'
});
conn.connect(function(err) {
  if (err) throw err;
  console.log('Database is connected successfully !');
});
module.exports = conn;

 

Now, You can include the database.js file in the other file by using require('./database.js').

Tutorial Summary

Dear developers, I hope you have understood the above script, Now you are able to stabilize the Node.js MySQL database connection. So, Continue to visit my site, I will share more tutorials related to Node.js / Express as soon as possible.

If you have any doubts or questions. You can directly ask me through the below comment box.