Menu Close

How to MySQL Create Database using Node.js?

4.5
(12)

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;
  console.log("Connected!");
  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” – https://codeamend.com/blog/connecting-to-the-mysql-database-using-nodejs/

View More Details about the MySQL package – https://www.npmjs.com/package/mysql

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?

Share

Posted in MySQL, NodeJs, NPM

Leave a Reply

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

You can also read...