Menu Close

How to MySQL Create Database using Node.js?


NodeJs is an open-source run-time environment that organizes everything you would like to execute a program written in JavaScript. It’s used for running scripts on the server to render content before it’s delivered to an internet browser.

NPM stands for Node Package Manager, which is an application and repository for developing and sharing JavaScript code.

Follow the below steps to connect the MySQL database

Install MySQL using npm install

C:\users\codeamend>npm install mysql 

After install the MySQL module, you can include the module in any application.

var mysql = require('mysql'); 

Creating a Database

Once you are installed the MySQL package, Then you need to run your phpmyadmin after that you can create the new database.

var mysql = require('mysql');

var con = mysql.createConnection({
  host: "localhost",
  user: "username",
  password: "password"

con.connect(function(err) {
  if (err) throw err;
  con.query("CREATE DATABASE codeamend", function (err, result) {
    if (err) throw err;
    console.log("Database created and named as codeamend");

Also read “Connecting to the MySQL database using NodeJs” –

View More Details about the MySQL package –

How useful was this post?

Click on a star to rate it!

Average rating 4.5 / 5. Vote count: 12

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?


Posted in MySQL, NodeJs, NPM

Leave a Reply

Your email address will not be published. Required fields are marked *

You can also read...