Node.js MySQL Create Database

Websolutionstuff | Sep-22-2021 | Categories : MySQL Node.js

In this tutorial we will see Node.js MySQL Create Database. For any kind data store or run query then we need database like MySQL, MongoDB, PostgreSQL but one of the most popular databases is MySQL.

we can create database in MySQL using Node.js in using mysql module or connect MySQL with node.js.

You can download MySQL database : https://www.mysql.com/downloads/.

Step 1 : Install MySQL Driver

First you have to need MySQL and running on your computer, you can access it by using Node.js.

To access a MySQL database with Node.js, you need a MySQL driver. now we will use the "mysql" module, downloaded from NPM.

 

 

To download and install the "mysql" module in application using below command.

npm install mysql

Now you have downloaded and installed a mysql database driver.

Node.js can use this module to manipulate the MySQL database :

var mysql = require('mysql');

 

Step 2 : Connect Database

Create db_connection.js file your application. and copy below code in that file.

var mysql = require('mysql');

var con = mysql.createConnection({
  host: "localhost",
  user: "your_username",
  password: "your_password"
});

con.connect(function(err) {
  if (err) throw err;
  console.log("Connected..!!");
});

 

Step 3 : Create Database with Connection

Create a database in MySQL, use the "CREATE DATABASE" statement. So, copy below code and paste in your file.

 

 

Database Name : node_mysql_demo

var mysql = require('mysql');

var con = mysql.createConnection({
  host: "localhost",
  user: "your_username",
  password: "your_password"
});

con.connect(function(err) {
  if (err) throw err;
  console.log("Connected..!!");
  con.query("CREATE DATABASE node_mysql_demo", function (err, result) {
    if (err) throw err;
    console.log("Database Created..!!");
  });
});

 

Step 4 : Run db_connection.js file

run db_connection.js using below code :

node db_connection.js

 


You might also like :

Recommended Post
Featured Post
How To Create Dependent Dropdown In Laravel
How To Create Dependent Dropdo...

In this article, we will see how to create a dependent dropdown list in laravel using ajax. Many times we have requ...

Read More

Jul-05-2020

Laravel 10 AJAX CRUD Operations Example
Laravel 10 AJAX CRUD Operation...

In this article, we will see the laravel 10 ajax crud operations example. Here, we will learn about ajax crud operation...

Read More

Feb-27-2023

Laravel 9 Livewire Pagination Example
Laravel 9 Livewire Pagination...

In this article, we will see laravel 9 livewire pagination example. we will learn how to create a laravel live...

Read More

Sep-29-2022

Google Autocomplete Address In Laravel 8
Google Autocomplete Address In...

In this example we will see how to google autocomplete address in laravel 8. In laravel 8 google autocomplete address tu...

Read More

Aug-16-2021