Menu Close

How to MySQL Create Database using Node.js?

Codeamend

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

Posted in MySQL, NodeJs, NPM

You can also read...